linux 文件查找

linux一切皆文件,那么自然需要文件查找功能。在众多目录中找到自己想要的文件还是一件不容易的事情。

命令文件:
which:which是用来查找命令文件的指令,检索速度还可以。但是范围固定,范围是$PATH。

$ PATH:/usr/local/sbin、/usr/local/bin、/usr/sbin、/usr/bin、/root/bin

我们正常输入的指令比如:ls 、 vim等,并没有输入目录,这就是因为哪些指令在$ PATH中,能够被查找到,所以不用填写。如果一个命令的命令文件不在$ PATH中,那就必须要写上命令的目录。

可以在profile文件中中设置PATH

whereis:也是查找命令文件的,并且会连带着MAN文档也被查找出来。

任意文件:
locate :模糊查询,只要有包含要搜索的字符就会被查找出来。
/etc/updatedb.conf
这个文件,限制搜索文件的类型和路径。

因为用了自带的数据库所以查询较为迅速,但是新建的文件有时不会再数据库中,就无法搜索到,所以要更新数据库。
updatedb
更新后就可以查到了。

find :没有任何限制的查询。

按照名字查找:

[root@localhost /]# find / -name "abcde.txt"
/tmp/test-gsc/deep-dark/abcde.txt

dd if=/dev/zero of=/test/date4M bs=1M count =4

[root@localhost deep-dark]# dd if=/dev/zero of=./12345.txt bs=3M count=5
5+0 records in
5+0 records out
15728640 bytes (16 MB) copied, 0.577469 s, 27.2 MB/s
[root@localhost deep-dark]# ll 12345.txt 
-rw-r--r--. 1 root root 15728640 Oct 19 15:35 12345.txt
[root@localhost deep-dark]# ls -lh 12345.txt 
-rw-r--r--. 1 root root 15M Oct 19 15:35 12345.txt

zero文件是用来填写文件,补空的。一般都用这个文件来做扩大,和null文件正好相反,null是任何内容进来都会消失。
将文件变为任意的大小。bs为块大小,count为块数量,相乘才是真正的大小。12345.txt文件就是按照这样制作,算出15M。

按大小查找:
按大小查找是必须正正好好才行,设置15M就必须为15M,+表示大于15M,-表示小于15M。

[root@localhost deep-dark]# find /tmp/test-gsc/ -size 15M
/tmp/test-gsc/deep-dark/12345.txt
[root@localhost deep-dark]# 

指定深度查找目录:
比如现在一个目录这么深:
/tmp/test-gsc/deep-dark/a/b/c/d/e/f/g/h/i/j/k/l/jincheng.sh

aaaaa.txt文件在 a/目录中

find / -maxdepth 3 -a -name “aaaaa.txt”

从根开始 查找3层 名字为 aaaaa.txt的文件 -a表示and.

[root@localhost test-gsc]# find / -maxdepth 3 -a -name "aaaaa.txt"
[root@localhost test-gsc]# find / -maxdepth 4 -a -name "aaaaa.txt"
/tmp/test-gsc/deep-dark/aaaaa.txt
[root@localhost test-gsc]# find /tmp/ -maxdepth 3 -a -name "aaaaa.txt"
/tmp/test-gsc/deep-dark/aaaaa.txt
[root@localhost test-gsc]# find / -mindepth 3 -a -name "aaaaa.txt"
/tmp/test-gsc/deep-dark/aaaaa.txt

数字是从根开始,根下第N层。
从根开始查找查到根下第三层,也就是deep-dark目录时为止,找不到,因为文件在更深处。
从根开始查找,到根下第四层,也就是a/目录那一层,找到了。
从tmp目录开始,到tmp下第三层,能找到。证明是按照后面写的目录为起点。
mindepth :查找最小深度。从根下第三层开始查。

按照时间查找:(文件的修改时间)
-1:一天以内,+1:超过1天;:1等于1天。

[root@localhost test-gsc]# find /tmp/test-gsc/ -mtime -1

按文件属主、属组找:
user用户 group 属组 -a and不写默认为-a表示与运算 -o 表示或运算

[root@localhost test-gsc]# find /tmp/test-gsc/ -group g
[root@localhost test-gsc]# find /tmp/test-gsc/ -user g
[root@localhost test-gsc]# find /tmp/test-gsc/ -user g -a -group hr

按文件类型查找:

[root@localhost test-gsc]# find /tmp/test-gsc/deep-dark/ -type f

f 普通文件 l链接文件 b块设备文件 c字符设备文件 s套接字文件 p管道文件 d目录文件

按文件权限查找:

[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls            //-644表示最少有644权限,超过不管

**找到后的动作:**默认为-print 打印输出

-print
-ls
-delete
-exec            后面跟自定义的shell命令
-ok               后面跟自定义的shell命令
[root@localhost test-gsc]# find /tmp/test-gsc/deep-dark/ -name "file*" -exec cp -vrf {} yasuo/ \;/tmp/test-gsc/deep-dark/file1’ -> ‘yasuo/file1’
‘/tmp/test-gsc/deep-dark/file2’ -> ‘yasuo/file2’
‘/tmp/test-gsc/deep-dark/file3’ -> ‘yasuo/file3’
‘/tmp/test-gsc/deep-dark/file4’ -> ‘yasuo/file4’
‘/tmp/test-gsc/deep-dark/file5’ -> ‘yasuo/file5’

-exec 后跟的是命令,所以一定要有命令的结束符 “\ ;” 没有会失败

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值