在linux中,当我们需要搜索需要的文件时,可以使用which命令,也可以使用whereis,还可以使用locate工具,但更常用的是find命令。find命令是一个用来搜索符合特定条件的文件的命令工具。下面将介绍几种find命令的用法。
1、按名称筛选
1
2
3
4
5
6
|
[root@server02 ~] # find / -name "test1*"
/root/test1 /root/test12 /tmp/test1 .txt
/tmp/test12 .txt
/tmp/test123 .txt
|
2、按类型筛选
1
2
3
|
[root@server02 ~] # find / -type d -name "test1*" //d表示目录
/root/test1 /root/test12 |
3、按大小筛选
1
2
3
4
5
6
7
8
|
[root@server02 ~] # ls -lh /tmp | grep "test1"
-rw-r--r--. 1 root root 1.2K 5月 30 09:09 test123.txt -rw-r--r--. 1 root root 7.8K 5月 30 09:08 test12.txt -rw-r--r--. 1 root root 3.6M 5月 30 09:12 test1.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)等文件信息。
1
2
3
4
5
6
7
8
9
10
|
[root@server02 ~] # stat /tmp/test1.txt
File: '/tmp/test1.txt'
Size: 3689322 Blocks: 7208 IO Block: 4096 regular file
Device: 803h /2051d Inode: 16958226 Links: 1
Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0 Access: 2017-05-30 09:11:39.189757015 +0800 Modify: 2017-05-30 09:12:07.369705343 +0800 Change: 2017-05-30 09:12:07.369705343 +0800 Birth: -
|
通过atime、mtime、ctime筛选的最小单位为“天”。而通过mmin、cmin、amin筛选的最小单位为“分钟”。
1
2
3
4
5
6
7
8
|
[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筛选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@server02 tmp] # ll -i
total 4136 16777289 -rw-r--r--. 2 root root 0 May 30 08:50 1-hd.txt 16777289 -rw-r--r--. 2 root root 0 May 30 08:50 1.txt 33595402 drwxr-xr-x. 3 root root 18 May 30 09:24 test
16958225 lrwxrwxrwx. 1 root root 4 May 30 08:43 test - ln -> test
16958226 -rw-r--r--. 1 root root 3689322 May 30 09:12 test1.txt 16958227 -rw-r--r--. 1 root root 7917 May 30 09:08 test12.txt 16958228 -rw-r--r--. 1 root root 1131 May 30 09:09 test123.txt 16958239 -rw-r--r--. 2 root root 263523 May 30 09:11 txt 16958239 -rw-r--r--. 2 root root 263523 May 30 09:11 txt_hd [root@server02 tmp] # find /tmp -inum 16777289
/tmp/1 .txt
/tmp/1-hd .txt
[root@server02 tmp] # find /tmp -inum 16958239
/tmp/txt /tmp/txt_hd |
以上5种筛选方式同时使用时是同时满足的关系。表示筛选出来的文件要符合多个条件。如果多个条件中只需要符合其中任意一个,可以使用“-o”参数。
1
2
3
4
5
6
7
8
9
10
11
|
[root@server02 tmp] # find /tmp -amin +15 -name "test"
/tmp/test/test [root@server02 tmp] # 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 ...... |
本文转自Grodd51CTO博客,原文链接:http://blog.51cto.com/juispan/1933980,如需转载请自行联系原作者