文章目录
-
-
-
-
- e.g.01 find /tmp -name core -type f -print | xargs /bin/rm -f
- e.g.02 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
- e.g.03 find . -type f -exec file '{}' \;
- e.g.04 find / \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \( -size +100M -fprintf /root/big.txt '%-10s %p\n' \)
- e.g.05 find $HOME -mtime 0
- e.g.06 find /sbin /usr/sbin -executable \! -readable -print
- e.g.07 find . -perm 664
- e.g.08 find . -perm -664
- e.g.09 find . -perm /222
- e.g.10 find . -perm /220 find . -perm /u+w,g+w find . -perm /u=w,g=w
- e.g.11 find . -perm -220 find . -perm -g+w,u+w
- e.g.12 find . -perm -444 -perm /222 \! -perm /111 find . -perm -a+r -perm /a+w \! -perm /a+x
- e.g.13 cd /source-dir find . -name .snapshot -prune -o \( \! -name '*~' -print0 \) | cpio -pmd0 /dest-dir
- e.g.14 find repo/ \( -exec test -d '{}'/.svn \; -or -exec test -d {}/.git \; -or -exec test -d {}/CVS \; \) -print -prune
- e.g.15 find /tmp -type f,d,l
- 扩展e.g.13,使用find命令的时候如何忽略指定的文件夹
-
-
-
前言:本文的例子均来源于man手册第一章节的find,man 1 find查看
e.g.01 find /tmp -name core -type f -print | xargs /bin/rm -f
手册原文:
find /tmp -name core -type f -print | xargs /bin/rm -f
Find files named core in or below the directory /tmp and delete them. Note that this will work incorrectly if there are any filenames containing newlines, single or double quotes, or spaces.
效果:
删除/tmp
路径下面名字为core
的普通文件,但是文件不能够有换行符、单引号、双引号和空格符,否则会出错。
e.g.02 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
手册原文:
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
Find files named core in or below the directory /tmp and delete them,
processing filenames in such a way that file or directory names containing single or double quotes,
spaces or newlines are correctly handled.
The -name test comes before the -type test in order to avoid having to call stat(2) on every file.
效果:
删除/tmp
路径下面名字为core
的普通文件,和上面的区别就是文件可以有换行符、单引号、双引号和空格符,因为用字符\0
隔开了。
e.g.03 find . -type f -exec file ‘{}’ ;
手册原文:
find . -type f -exec file '{}' \;
Runs `file' on every file in or below the current directory.
Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation.
The semicolon is similarly protected by the use of a backslash, though single quotes could have been used in that case also.
效果:
对当前目录下每个文件执行file
命令。这里的{}在执行命令的时候会被替换成找到的文件名,这里的;是作为exec选项的参数,不然exec命令就不完整了。(注意: ‘{}’ ;加单引号和转移符号都是为了避免shell错误解释)
e.g.04 find / ( -perm -4000 -fprintf /root/suid.txt ‘%#m %u %p\n’ ) , ( -size +100M -fprintf /root/big.txt ‘%-10s %p\n’ )
手册原文:
find / \( -perm -4000 -fprintf /root/suid.txt '%#m %u %p\n' \) , \
\( -size +100M -fprintf /root/big.txt '%-10s %p\n' \)
Traverse the filesystem just once,
listing setuid files and directories into /root/suid.txt and large files into /root/big.txt.
效果:
递归查找根目录下具有SUID(Set User ID)权限的文件,并将结果输出到/root/suid.txt文件中;大于100M的文件,将结果输出到/root/big.txt文件中。然后前者的输出格式是这个%#m %u %p\n
,后者输出的格式%-10s %p\n
是这个。第一行末尾 \
只是一行的连接符,别看错了。
e.g.05 find $HOME -mtime 0
手册原文:
find $HOME -mtime 0
Search for files in your home directory which have been modified in the last twenty-four hours.
This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded.
That means that to match -mtime 0, a file will have to have a modification in the past