find命令

find [option]... [查找路径] [查找条件] [处理动作]

比如:find / -type d -name sysconfig

查找路径:默认为当前路径

查找条件:指定的查找标准,可以根据文件名、大小、属主属组、类型等进行;默认为找出指定路径下的所有文件;

处理动作:对符合条件的文件做指定操作;默认输出至屏幕

-type   类型:目录文件f

-mtime  时间:+10天代表10天以前  -10天代表10天以内

-mmin   分钟:-60代表一小时以内

-size   大小:k,M

-o      各参数之间的与或关系

-exec   搜索结果执行后一命令

-name   文件名,支持模糊名称

[root@24centos7-01 tmp]# find /usr -name ls

/usr/bin/ls

[root@24centos7-01 tmp]# find / -type d -name sysconfig

/run/initramfs/state/etc/sysconfig

/etc/sysconfig

 

查找文件示例

·        根据时间查找

[root@24centos7-01 tmp]# stat tooth.txt

  File: 'tooth.txt'

  Size: 5           Blocks: 8          IO Block: 4096   regular file

Device: 803h/2051d Inode: 16783948    Links: 1

Access: (0644/-rw-r--r--) Uid: (    0/    root)  Gid: (    0/    root)

Access: 2017-10-23 22:09:33.380613716 +0800

Modify: 2017-10-23 22:12:47.861123460 +0800

Change: 2017-10-23 22:12:47.861123460 +0800

 Birth: -

 

--atime最后访问时间,指文件被读取而更新的时间

--mtime内容修改时间,即文件的内容发生变化

--ctime状态修改时间,指文件的属性或者权限发生变化而更新的时间

 

[root@24centos7-01 tmp]# find /tmp/ -type f -mtime -2

/tmp/test.txt

/tmp/tooth.txt

[root@24centos7-01 tmp]# find /tmp/ -type f -ctime -2

/tmp/test.txt

/tmp/tooth.txt

[root@24centos7-01 tmp]# find /tmp/ -type f -atime -2

/tmp/test.txt

/tmp/tooth.txt

 

--一小时以内

[root@24centos7-01 tmp]# find . -type f -mmin -60

./test/test.txt

./test.txt

./tooth.txt

 

--参数或关系-o

[root@24centos7-01 tmp]# find /tmp/ -type f -size -10k -o-mmin -60;

/tmp/

/tmp/.bash_history

/tmp/test

/tmp/test/test.txt.bak

/tmp/test.txt.bak

/tmp/tooth.txt.bak

 

--查找出一小时以内的文件并列举其信息

--find . -type f -mmin -60 -exec ls -l {} \;

[root@24centos7-01 tmp]# find . -type f -mmin -60 -exec ls -l {} \;

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test/test.txt

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test.txt

-rw-r--r-- 1 root root 5 Oct 23 22:12 ./tooth.txt

 

--将查找出来的文件改名

[root@24centos7-01 tmp]# find . -type f -mmin -60 -exec ls -l {} \;

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test/test.txt

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test.txt

-rw-r--r-- 1 root root 5 Oct 23 22:12 ./tooth.txt

[root@24centos7-01 tmp]# find . -type f -mmin -60 -exec mv {} {}.bak \;

[root@24centos7-01 tmp]# find . -type f -mmin -60 -exec ls -l {} \;

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test/test.txt.bak

-rw-r--r-- 2 root root 0 Oct 23 22:09 ./test.txt.bak

-rw-r--r-- 1 root root 5 Oct 23 22:12 ./tooth.txt.bak

 

 

·        根据iNode号查找

[root@24centos7-01 tmp]# ln test.txt /tmp/test/test.txt

[root@24centos7-01 tmp]# ls -i /tmp/test

16783946 test.txt

[root@24centos7-01 tmp]# find -inum 16783946

./test/test.txt

./test.txt

·        按文件大小查找

[root@24centos7-01 tmp]# find /root/ -type f -size -10k-exec ls -lh {} \;

-rw-r--r--. 1 root root 18 Dec 29  2013 /root/.bash_logout

-rw-r--r--. 1 root root 176 Dec 29  2013 /root/.bash_profile

-rw-r--r--. 1 root root 176 Dec 29  2013 /root/.bashrc

-rw-r--r--. 1 root root 100 Dec 29  2013 /root/.cshrc

-rw-r--r--. 1 root root 129 Dec 29  2013 /root/.tcshrc

-rw-------. 1 root root 1.4K Oct 13 05:50/root/anaconda-ks.cfg

-rw-r--r-- 1 root root 343 Oct 17 21:25/root/.ssh/known_hosts

-rw------- 1 root root 1.7K Oct 17 21:59/root/.ssh/id_rsa

-rw-r--r-- 1 root root 399 Oct 17 21:59/root/.ssh/id_rsa.pub

-rw-r--r-- 1 root root 0 Oct 20 21:18/root/showtime/showtime.txt

-rw-r--r-- 1 root root 0 Oct 20 21:15/root/showtime/showtime/showtime.txt

-rw-r--r-- 1 root root 0 Oct 20 21:37 /root/test/1.txt

-rw-r--r-- 1 root root 0 Oct 20 21:37 /root/test/2.txt

-rw-r--r-- 1 root root 0 Oct 20 21:37 /root/test/3.txt

-rw------- 1 root root 6.9K Oct 20 22:14 /root/.viminfo

-rw------- 1 root root 54 Oct 21 20:11 /root/.lesshst

 

[root@24centos7-01 tmp]# find /root/ -type f -size +10k-exec ls -lh {} \;

-rw-------. 1 root root 12K Oct 23 21:27/root/.bash_history

文件名后缀

·        .txt文件文件

·        .zip压缩文件

·        .tar.gz打包文件

·        .conf配置文件

·        .exe可执行文件