使用find和xargs
find:查找具有某一特性的文件(文件权限,文件属主,文件长度,文件类型)
find命令的一般格式为: find pathname -options [-print -exec -ok]
参数:
pathname find命令所查找的路径。用.来表示当前目录,用/来表示系统根目录
-print find命令所匹配的文件输出到标准输出
-exec find命令对匹配的文件执行该参数所给出的shell命令
find命令选项
- -name: 按照文件名查找文件
- -perm: 安装文件权限查找文件
- -prune: 使用这一个选项可以使find命令不在当前指定的目录中查找,如果同时使用了-depth选项,-prune选项将会被find命令忽略
- -user : 按照文件属主来查找文件
- -group: 安装文件所属的组来查找文件
1.使用name选项
注意:
- ~代表你的$HOME目录,指当前登录的用户在/home目录下属于自己的那个家目录
- .代表当前目录及子目录,./也是代表当前目录及子目录
- /代表整个文件系统的根目录,最顶层开始的地方,下面包括所有的目录和文件
1 [root@localhost ~]# cd ~ 2 [root@localhost ~]# pwd 3 /root 4 [root@localhost ~]# cd . 5 [root@localhost ~]# pwd 6 /root 7 [root@localhost ~]# cd ./ 8 [root@localhost ~]# pwd 9 /root 10 [root@localhost ~]# cd / 11 [root@localhost /]# pwd 12 / 13 [root@localhost /]#

实例一:在$home目录下查找所有的“*.log”文件
1 [root@localhost /]# find ~ -name "*.log" -print 2 /root/qq.log 3 /root/123.log 4 /root/install.log
实例二:在当前目录及子目录中查找所有的“*.log”文件
[root@localhost /]# find . -name "*.log" -print ./usr/lib/rpm/rpm.log ./usr/share/texmf/tex/fontinst/misc/glyphbox.log ... [root@localhost /]# find ./ -name "*.log" -print ./usr/lib/rpm/rpm.log ./usr/share/texmf/tex/fontinst/misc/glyphbox.log ...
实例三:在当前目录下及子目录下查找一个以大写字母开头的文件
1 [root@localhost ~]# ll 2 总用量 21584 3 drwxr-xr-x. 2 root root 4096 8月 28 02:17 123.log 4 -rw-r--r--. 1 root root 0 10月 20 22:39 a.html 5 -rw-------. 1 root root 2454 8月 10 21:36 anaconda-ks.cfg 6 -rw-------. 1 root root 6007 10月 25 10:43 histfile 7 -rw-r--r--. 1 root root 0 10月 30 12:40 HUA 8 -rw--w-rw-. 1 root users 52211 8月 10 21:36 install.log 9 -rw-r--r--. 1 root root 14312 8月 10 21:34 install.log.syslog 10 -rwxrwxrwx. 1 root root 0 10月 28 12:29 myfile 11 -rw-r--r--. 1 root root 8073290 10月 22 09:39 phpMyAdmin-4.0.10.10-all-languages.tar.gz 12 -rw-r--r--. 1 root root 13865121 10月 21 14:29 phpwind_UTF8_8.7.zip 13 drwxr-xr-x. 2 root root 4096 8月 12 02:43 qq 14 -rw----r--. 1 root root 52211 8月 12 22:29 qq.log 15 drwxr-xr-x. 2 root root 4096 8月 12 02:49 test1 16 drwxr-xr-x. 2 root root 4096 8月 12 02:49 test2 17 -rw-r--r--. 1 root root 0 8月 12 22:28 zxc 18 [root@localhost ~]# find . -name "[A-Z]*" -print 19 ./HUA
实例四:在/etc目录下查找文件名以host开头的文件
1 [root@localhost ~]# find /etc -name "host*" -print 2 /etc/host.conf 3 /etc/sysconfig/networking/profiles/default/hosts 4 /etc/hosts 5 /etc/hosts.deny 6 /etc/avahi/hosts 7 /etc/hosts.allow 8 [root@localhost ~]#
实例五:查找$HOME目录下的文件
1 [root@localhost ~]# find ~ -name "*" -print 2 3 /root 4 /root/phpwind_UTF8_8.7.zip 5 /root/install.log.syslog 6 /root/.bash_logout 7 /root/.cshrc 8 ... 9 [root@localhost ~]# find . -print 10 . 11 ./phpwind_UTF8_8.7.zip 12 ./install.log.syslog 13 ./.bash_logout 14 ./.cshrc 15 ./anaconda-ks.cfg
2.使用perm选项
实例一;在当前目录下查找文件权限为755的文件,既文件属主可以读,写,执行,其他用户可以读,执行的文件
在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-005相当于555,

1 [root@localhost ~]# find . -perm 755 -print 2 ./test2 3 ./qq 4 ./test1 5 ./123.log 6 [root@localhost ~]# ll 7 总用量 21584 8 drwxr-xr-x. 2 root root 4096 8月 28 02:17 123.log 9 -rw-r--r--. 1 root root 0 10月 20 22:39 a.html 10 -rw-------. 1 root root 2454 8月 10 21:36 anaconda-ks.cfg 11 -rw-------. 1 root root 6007 10月 25 10:43 histfile 12 -rw-r--r--. 1 root root 0 10月 30 12:40 HUA 13 -rw--w-rw-. 1 root users 52211 8月 10 21:36 install.log 14 -rw-r--r--. 1 root root 14312 8月 10 21:34 install.log.syslog 15 -rwxrwxrwx. 1 root root 0 10月 28 12:29 myfile 16 -rw-r--r--. 1 root root 8073290 10月 22 09:39 phpMyAdmin-4.0.10.10-all-languages.tar.gz 17 -rw-r--r--. 1 root root 13865121 10月 21 14:29 phpwind_UTF8_8.7.zip 18 drwxr-xr-x. 2 root root 4096 8月 12 02:43 qq 19 -rw----r--. 1 root root 52211 8月 12 22:29 qq.log 20 drwxr-xr-x. 2 root root 4096 8月 12 02:49 test1 21 drwxr-xr-x. 2 root root 4096 8月 12 02:49 test2 22 -rw-r--r--. 1 root root 0 8月 12 22:28 zxc 23 [root@localhost ~]#
实例二:在当前目录下查找所有用户都可以读,写,执行的文件
1 [root@localhost ~]# find . -perm -007 -print 2 ./myfile 3 [root@localhost ~]# find . -perm 777 -print 4 ./myfile 5 [root@localhost ~]#
3.使用user和nouser
实例一:在$home目录中查找文件属主为root的文件
1 [root@localhost ~]# find ~ -user root -print 2 /root 3 /root/phpwind_UTF8_8.7.zip 4 /root/install.log.syslog 5 /root/.bash_logout 6 /root/.cshrc 7 /root/anaconda-ks.cfg 8 /root/.bash_history 9 /root/test2 10 /root/qq 11 /root/qq/log 12 /root/qq.log 13 /root/histfile 14 /root/.mysql_history 15 /root/myfile 16 /root/HUA 17 /root/test1 18 /root/a.html 19 /root/.bash_profile 20 /root/123.log 21 /root/.tcshrc 22 /root/install.log 23 /root/phpMyAdmin-4.0.10.10-all-languages.tar.gz 24 /root/zxc 25 /root/.bashrc 26 [root@localhost ~]#
实例二:为了查找属主帐户已经被删除的文件,可以使用-nouser选项。在/home目录下查找所有的这类文件
1 [root@localhost ~]# find /home -nouser -print
4.使用group和nogroup选项
实例一:在/apps目录下查找属于accts用户组的文件
1 [root@localhost ~]# find /apps -group accts -print
要查找没有有效所属用户组的所有文件,可以使用nogroup选项。下面的find命令从文件系统的根目录处查找这样的文件:
find / -nogroup -print
5.按照更改时间来查找文件
如果希望按照更改时间来查找文件,可以使用mtime,atime或ctime选项。如果系统突然没有可用空间了,很有可能某一个文件的长度在此期间增长迅速,这时就可以用mtime选项来查找这样的文件。
用减号-来限定更改时间在距今n日以内的文件,而用加号+来限定更改时间在距今n日以前的文件。
希望在系统根目录下查找更改时间在5日以内的文件,可以用:
find / -mtime -5 -print
为了在/var/adm目录下查找更改时间在3日以前的文件,可以用:
find /var/adm -mtime +3 -print
6.使用type选项
实例一:在/etc目录下查找所有的目录
1 [root@localhost ~]# find /etc -type d -print 2 /etc 3 /etc/libibverbs.d 4 /etc/selinux 5 ......
实例二:在当前目录下查找除目录以外的所有类型文件
1 [root@localhost ~]# find . ! -type d -print 2 ./phpwind_UTF8_8.7.zip 3 ./install.log.syslog 4 ./.bash_logout 5 ......
实例三:在/etc目录下查找所有的符号链接文件
1 [root@localhost ~]# find /etc -type I -print
7.使用size选项
可以按照文件长度来查找文件,这里所指的文件长度既可以用块(block)来计量,也可以用字节来计量。以字节计量文件长度的表达形式为N c;以块计量文件长度只用数字表示即可。
在按照文件长度查找文件时,一般使用这种以字节表示的文件长度,在查看文件系统的大小,因为这时使用块来计量更容易转换。
实例一:在当前目录下查找文件长度大于1m字节的文件
1 [root@localhost ~]# find . -size +1000000c -print 2 ./phpwind_UTF8_8.7.zip 3 ./phpMyAdmin-4.0.10.10-all-languages.tar.gz 4 [root@localhost ~]#
实例二:在/home/apache目录下查找文件长度恰好为100字节的文件
1 [root@localhost ~]# find /home/apache -size 100c -print
实例三:在当前目录下查找长度超过10块的文件(一共等于512字节)
1 [root@localhost ~]# find . -size +10 -print 2 ./phpwind_UTF8_8.7.zip 3 ./install.log.syslog 4 ./.bash_history 5 ./qq/log 6 ./qq.log 7 ./histfile 8 ./install.log 9 ./phpMyAdmin-4.0.10.10-all-languages.tar.gz 10 [root@localhost ~]#
8.使用exec或ok来执行shell命令
exec选项后面跟所要执行的命令,然后是一对{},一个空格和一个\,最后一个分号
实例一:在当前目录查找普通文件并将其输出
1 [root@localhost ~]# find . -type f -exec ls -l {} \; 2 -rw-r--r--. 1 root root 13865121 10月 21 14:29 ./phpwind_UTF8_8.7.zip 3 -rw-r--r--. 1 root root 14312 8月 10 21:34 ./install.log.syslog 4 -rw-r--r--. 1 root root 18 5月 20 2009 ./.bash_logout 5 -rw-r--r--. 1 root root 100 9月 23 2004 ./.cshrc 6 -rw-------. 1 root root 2454 8月 10 21:36 ./anaconda-ks.cfg 7 -rw-------. 1 root root 7702 10月 30 20:10 ./.bash_history 8 -rw----r--. 1 root root 52211 8月 12 02:31 ./qq/log 9 -rw----r--. 1 root root 52211 8月 12 22:29 ./qq.log 10 -rw-------. 1 root root 6007 10月 25 10:43 ./histfile 11 -rw-------. 1 root root 28 10月 20 16:37 ./.mysql_history 12 -rwxrwxrwx. 1 root root 0 10月 28 12:29 ./myfile 13 -rw-r--r--. 1 root root 0 10月 30 12:40 ./HUA 14 -rw-r--r--. 1 root root 0 10月 20 22:39 ./a.html 15 -rw-r--r--. 1 root root 176 5月 20 2009 ./.bash_profile 16 -rw-r--r--. 1 root root 129 12月 4 2004 ./.tcshrc 17 -rw--w-rw-. 1 root users 52211 8月 10 21:36 ./install.log 18 -rw-r--r--. 1 root root 8073290 10月 22 09:39 ./phpMyAdmin-4.0.10.10-all-languages.tar.gz 19 -rw-r--r--. 1 root root 0 8月 12 22:28 ./zxc 20 -rw-r--r--. 1 root root 176 9月 23 2004 ./.bashrc


浙公网安备 33010602011771号