find命令
命令格式:
1 find pathname -options [-print -exec -ok ...]
命令功能:
用于在文件树种查找文件,并作出相应的处理
1 pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。 2 -print: find命令将匹配的文件输出到标准输出。 3 -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {} ;注意{}和;之间的空格。 4 -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令 在执行每一个命令之前,都会给出提示,让用户来确定是否执行
命令选项:
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, -n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-mtime选项相似。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size n[c]: 查找文件长度为n块的文件,带有c时表示文件长度以字节计。
-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
另外,下面三个的区别:
-amin n 查找系统中最后N分钟访问的文件
-atime n 查找系统中最后n*24小时访问的文件
-cmin n 查找系统中最后N分钟被改变文件状态的文件
-ctime n 查找系统中最后n*24小时被改变文件状态的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-mtime n 查找系统中最后n*24小时被改变文件数据的文件
使用实例:
实例1:查找指定时间内修改过的文件
1 [xxx@localhost Marvin]$ find . -mtime -2 2 . 3 ./img 4 ./img/img_downloader.sh~ 5 ./img/img_downloader.sh 6 ./img/img_downloader.sh.bak
实例2:根据关键字查找
1 [xxx@localhost Marvin]$ find . -name "*.sh" 2 ./interactive.sh 3 ./word_freq.sh 4 ./zishell.sh 5 ./img/img_downloader.sh 6 ./hello.sh 7 ./remove-duplicates/remove-duplicates.sh 8 ./args.sh 9 ./filestat.sh 10 ./automate_expect.sh 11 ./checkword.sh 12 ./log.sh
实例3:按照目录或文件的权限来查找文件
1 [xxx@localhost Marvin]$ find . -perm 766 2 ./DESKTOP-UF14UGF.txt
实例4:按类型查找
1 [xxx@localhost Marvin]$ find . -type l 2 ./args
实例5:查找当前所有目录并排序
1 [xxx@localhost Marvin]$ find . -type d | sort 2 . 3 ./batfile 4 ./cut 5 ./grep 6 ./img 7 ./img/imag 8 ./loopback 9 ./remove-duplicates 10 ./test 11 ./worktest
实例6:按大小查找文件
1 #查找大于100k的文件 2 [xxx@localhost Marvin]$ find . -size +100000c 3 ./junk.data 4 ./worktest/dmi.txt 5 ./loopback/loopbackfile.img
实例7:ls -l命令放在find命令的-exec选项中
1 [xxx@localhost Marvin]$ find . -maxdepth 1 -type f -exec ls -l {} \; 2 -rw-rw-r--. 1 xxx xxx 50 2月 14 17:29 ./sort.txt 3 -rw-rw-r--. 1 xxx xxx 806 2月 14 14:42 ./timing.log 4 -rw-rw-r--. 1 xxx xxx 14 2月 14 14:33 ./file.txt 5 -rw-rw-r--. 1 xxx xxx 29 2月 14 17:37 ./sort1.txt 6 -rwxrwxr-x. 1 xxx xxx 106 2月 14 18:44 ./interactive.sh 7 -rwxrwxr-x. 1 xxx xxx 241 2月 16 16:01 ./word_freq.sh 8 -rwxrwxr-x. 1 xxx xxx 37 2月 13 11:35 ./zishell.sh
实例8:找到修改时间大于6天的文件删除
1 [xxx@localhost Marvin]$ find . -mtime +6 2 ./zishell.sh 3 ./test.log 4 ./test.txt 5 ./args.sh 6 ./log.sh 7 [xxx@localhost Marvin]$ find . -mtime +6 -exec rm {} \; 8 [xxx@localhost Marvin]$ find . -mtime +6
实例9:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
1 [xxx@localhost Marvin]$ find . -mtime +5 2 ./sort.txt 3 ./timing.log 4 ./file.txt 5 ./sort1.txt 6 ./interactive.sh 7 ./inputdata 8 ./junk.data 9 ./remove-duplicates/other 10 ./output.session 11 ./automate_expect.sh 12 ./checkword.sh 13 [xxx@localhost Marvin]$ find . -mtime +5 -ok rm {} \; 14 < rm ... ./sort.txt > ? nn 15 < rm ... ./timing.log > ? n 16 < rm ... ./file.txt > ? n 17 < rm ... ./sort1.txt > ? y 18 < rm ... ./interactive.sh > ? n 19 < rm ... ./inputdata > ? n 20 < rm ... ./junk.data > ? y 21 < rm ... ./remove-duplicates/other > ? n 22 < rm ... ./output.session > ? n 23 < rm ... ./automate_expect.sh > ? n 24 < rm ... ./checkword.sh > ? n 25 [xxx@localhost Marvin]$ find . -mtime +5 26 ./sort.txt 27 ./timing.log 28 ./file.txt 29 ./interactive.sh 30 ./inputdata 31 ./remove-duplicates/other 32 ./output.session 33 ./automate_expect.sh 34 ./checkword.sh
实例10:-exec中使用grep命令
1 [root@localhost Marvin]# find /etc -name "passwd" -exec grep "root" {} \; 2 root:x:0:0:root:/root:/bin/bash 3 operator:x:11:0:operator:/root:/sbin/nologin
实例11:查找文件移动到指定目录
1 [root@localhost Marvin]# find . -name "*.log" 2 ./timing.log 3 [root@localhost Marvin]# find . -name "*.log" -exec mv {} ../ \; 4 [root@localhost Marvin]# ls *.log 5 ls: 无法访问*.log: 没有那个文件或目录 6 [root@localhost Marvin]# ls ../*.log 7 ../timing.log
实例12:用exec选项执行cp命令
1 [root@localhost Marvin]# find . -name "*.log" 2 ./test.log 3 [root@localhost Marvin]# find . -name "*.log" -exec cp {} test2.log \; 4 [root@localhost Marvin]# find . -name "*.log" 5 ./test.log 6 ./test2.log
实例13: 查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
1 [root@localhost Marvin]# find . -type f -print | xargs file 2 ./cut/file: FORTRAN program 3 ./cut/file2: ASCII text 4 ./sort.txt: ASCII text 5 ./file.txt: ASCII text 6 ./interactive.sh: Bourne-Again shell script text executable 7 ./word_freq.sh: Bourne-Again shell script text executable
实例14:在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中
1 [root@localhost Marvin]# find / -name "core" -print | xargs echo "" >/tmp/core.log 2 [root@localhost Marvin]# ll /tmp/core* 3 -rw-r--r--. 1 root root 460 2月 20 20:19 /tmp/core.log
实例15:在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限
1 [root@localhost grep]# ll 2 总用量 12 3 -rwxrwxrwx. 1 luqinliang luqinliang 40 2月 16 08:55 file 4 -rwxrwxrwx. 1 luqinliang luqinliang 23 2月 16 09:11 file2 5 -rwxrwxrwx. 1 luqinliang luqinliang 11 2月 16 09:34 pat_file 6 [root@localhost grep]# find . -perm -7 | xargs chmod o-w 7 [root@localhost grep]# ll 8 总用量 12 9 -rwxrwxr-x. 1 luqinliang luqinliang 40 2月 16 08:55 file 10 -rwxrwxr-x. 1 luqinliang luqinliang 23 2月 16 09:11 file2 11 -rwxrwxr-x. 1 luqinliang luqinliang 11 2月 16 09:34 pat_file
实例16:使用xargs执行mv
1 find . -name "*.log" | xargs -i mv {} test4
实例17:-p参数会提示让你确认是否执行后面的命令,y执行,n不执行
1 root@localhost Marvin]# find . -name "*.log" -print | xargs -p -i mv {} ../ 2 mv ./test1.log ../ ?...y 3 [root@localhost Marvin]# ls *.log 4 ls: 无法访问*.log: 没有那个文件或目录 5 [root@localhost Marvin]# ls ../test1.log 6 ../test1.log