在find查找文件也是可以使用正则表达式的。用-regex 选项来使用正则表达式匹配,但是你知道的是它的这个pattern是匹配整个文件路径的,而不是仅仅是文件名哦。而且默认情况下,-regex pattern 使用的是Emacs中那种类型的正则表达式,我也不太熟悉,我一般喜欢使用egrep方式的正则,所以就加上 -regextype "posix-egrep" 来指定我要使用的正则表达式类型。 PS: -name选项中是不能使用正则的,但是可以有*等通配符。
比如:
1 2 |
find /home/test/ -regextype "posix-egrep" -regex "/home/test/.*\.(sh|pl|py)$" #查找/home/test/目录下的所有.sh, .pl, .py结尾的文件(脚本)。 |
我平时用得最多几个选项解释如下(copy from manual page)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
-name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (`*', `?', and `[]') match a `.' at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being spe‐ cial, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell. -regextype type Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-extended. -regex pattern File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'. The regular expressions understood by find are by default Emacs Regular Expres‐ sions, but this can be changed with the -regextype option. |
1 2 |
-iregex pattern Like -regex, but the match is case insensitive. |
这也是老板需要统计测试系统中个中脚本及配置文件的代码量到底有多少行,因为这个需求,了解了一下find的正则,如下是我写来做这件事情了个的一个脚本例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/bin/bash XVS_DIR='/root/vmm_tree/validation/xvs/' SCRIPT_LIST_LOG='/root/jay/script_list.log' CONFIG_LIST_LOG='/root/jay/config_list.log' total_count=0 script_count=0 config_count=0 find $XVS_DIR -regextype "posix-egrep" -regex "/root/vmm_tree/validation/xvs/.*\.(sh|pl|py|exp)$" > $SCRIPT_LIST_LOG while read line do count=$(cat $line | wc -l) script_count=$(($script_count + $count)) done < $SCRIPT_LIST_LOG echo "total line count in scripts:$script_count" #echo "--------separator-----above is for counting scripts---below is for counting config files---" find $XVS_DIR -type f -regextype "posix-egrep" -regex "/root/vmm_tree/validation/xvs/.*/(xvs|tet).*[^\.(sh|pl|py|exp|pdf)]$" > $CONFIG_LIST_LOG find $XVS_DIR -type f -name "Makefile" >> $CONFIG_LIST_LOG while read line do count=$(cat $line | wc -l) config_count=$(($config_count + $count)) done < $CONFIG_LIST_LOG echo "total line count in config:$config_count" total_count=$(($script_count + $config_count)) echo "we have $total_count lines code in our XVS." |
3 Comments