本文最后更新于2015年11月7日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!
=Start=
=find命令的-o/-regex选项=
标题虽然比较拗口,但功能比较实用,之前有用过,但是没有总结过,因此,在这里用一篇文章记录一下。
#需求:查找当前目录下所有的JPG和PNG类型的图片文件
#方法一
find -maxdepth 2 -type f \( -iname \*.jpg -o -iname \*.png \) #-maxdepth选项需要放在-type选项之前
find -maxdepth 2 -type f -mtime -7 \( '*.jpg' -o -name '*.png' \)
#方法二
find -maxdepth 2 -iregex '.*\.\(jpg\|png\)$'
#方法三
find -maxdepth 2 -regextype posix-extended -regex '.*\.(jpg|png)$'
参考链接:
=find命令的-path/-prune选项=
#looks for files we can write to that don't belong to us(注意其中-path选项之后的写法,带有通配符)
if [ "$thorough" = "1" ]; then
grfilesall=`find / -writable -not -user \`whoami\` -type f -not -path "/proc/*" -exec ls -al {} \; 2>/dev/null`
if [ "$grfilesall" ]; then
echo -e "\e[00;31mFiles not owned by user but writable by group:\e[00m\n$grfilesall" |tee -a $report 2>/dev/null
echo -e "\n" |tee -a $report 2>/dev/null
else
:
fi
fi
#list all world-writable files excluding /proc(『非』的另一种写法 -not)
if [ "$thorough" = "1" ]; then
wwfiles=`find / ! -path "*/proc/*" -perm -2 -type f -print 2>/dev/null`
if [ "$wwfiles" ]; then
echo -e "\e[00;31mWorld-writable files (excluding /proc):\e[00m\n$wwfiles" |tee -a $report 2>/dev/null
echo -e "\n" |tee -a $report 2>/dev/null
else
:
fi
else
:
fi
-path pattern
File name matches shell pattern pattern. The metacharacters do not treat `/' or `.' specially; so, for example,
find . -path "./sr*sc"
will print an entry for a directory called `./src/misc' (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to skip the directory `src/emacs' and all files and directories under it, and print the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path name here if the relevant start point is also an absolute path. This means that this command will never match any thing:
find bar -path /foo/bar/myfile -print
The predicate -path is also supported by HP-UX find and will be in a forthcoming version of the POSIX standard.
# -path 选项后面接的是一个"pattern",即模式匹配串,它的作用只是在最后判定是否打印(但已经进行了搜索操作);如果一开始就不想要搜索某个目录,请使用 -prune 选项,直接跳过搜索该目录,速度会更快,效率更高
-prune
True; if the file is a directory, do not descend into it. If -depth is given, false; no effect. Because -delete implies -depth, you cannot usefully use -prune and -delete together.
=EOF=