在linux中,当我们需要搜索需要的文件时,可以使用which命令,也可以适用whereis,还可以使用locate工具,但更常用的是find命令。find命令是一个用来搜索符合特定条件的文件的命令工具。下面将介绍几种find命令的用法。
1、按名称筛选
- [root@server02~]#find/-name"test1*"
- /root/test1
- /root/test12
- /tmp/test1.txt
- /tmp/test12.txt
- /tmp/test123.txt
2、按类型筛选
- [root@server02~]#find/-typed-name"test1*"//d表示目录
- /root/test1
- /root/test12
3、按大小筛选
- [root@server02~]#ls-lh/tmp|grep"test1"
- -rw-r--r--.1rootroot1.2K5月3009:09test123.txt
- -rw-r--r--.1rootroot7.8K5月3009:08test12.txt
- -rw-r--r--.1rootroot3.6M5月3009:12test1.txt
- [root@server02~]#find/tmp-size-5k-name"test1*"//小于5K
- /tmp/test123.txt
- [root@server02~]#find/tmp-size+1M-name"test1*"//大于1M
- /tmp/test1.txt
4、按时间筛选
通过stat命令能查看文件的atime(access time)、mtime(modify time)、ctime(change time)等文件信息。
- [root@server02~]#stat/tmp/test1.txt
- File:'/tmp/test1.txt'
- Size:3689322 Blocks:7208IOBlock:4096regularfile
- Device:803h/2051d Inode:16958226Links:1
- Access:(0644/-rw-r--r--)Uid:(0/root)Gid:(0/root)
- Context:unconfined_u:object_r:user_tmp_t:s0
- Access:2017-05-3009:11:39.189757015+0800
- Modify:2017-05-3009:12:07.369705343+0800
- Change:2017-05-3009:12:07.369705343+0800
- Birth:-
通过atime、mtime、ctime筛选的最小单位为“天”。而通过mmin、cmin、amin筛选的最小单位为“分钟”。
- [root@server02~]#find/tmp-atime-1-name"test"//atime在1天以内的
- /tmp/test
- /tmp/test/test
- [root@server02~]#find/tmp-atime+1-name"test"//atime在1天之前的
- [root@server02~]#
- [root@server02~]#touch/tmp/test
- [root@server02~]#find/tmp-amin+10-name"test"//atime在10分钟前的
- /tmp/test/test
5、按inode筛选
- [root@server02tmp]#ll-i
- total4136
- 16777289-rw-r--r--.2rootroot0May3008:501-hd.txt
- 16777289-rw-r--r--.2rootroot0May3008:501.txt
- 33595402drwxr-xr-x.3rootroot18May3009:24test
- 16958225lrwxrwxrwx.1rootroot4May3008:43test-ln->test
- 16958226-rw-r--r--.1rootroot3689322May3009:12test1.txt
- 16958227-rw-r--r--.1rootroot7917May3009:08test12.txt
- 16958228-rw-r--r--.1rootroot1131May3009:09test123.txt
- 16958239-rw-r--r--.2rootroot263523May3009:11txt
- 16958239-rw-r--r--.2rootroot263523May3009:11txt_hd
- [root@server02tmp]#find/tmp-inum16777289
- /tmp/1.txt
- /tmp/1-hd.txt
- [root@server02tmp]#find/tmp-inum16958239
- /tmp/txt
- /tmp/txt_hd
以上5种筛选方式同时使用时是同时满足的关系。表示筛选出来的文件要符合多个条件。如果多个条件中只需要符合其中任意一个,可以使用“-o”参数。
- [root@server02tmp]#find/tmp-amin+15-name"test"
- /tmp/test/test
- [root@server02tmp]#find/tmp-amin+15-o-name"test"
- /tmp/.font-unix
- /tmp/.X11-unix
- /tmp/.ICE-unix
- /tmp/.Test-unix
- /tmp/.XIM-unix
- /tmp/test
- /tmp/test/test
- ......