find文件查找
在目录层次结构中搜索文件。find命令可以根据名称、属性、创建时间、大小、所属用户等方式查找文件。
文章目录
- find文件查找
- find命令介绍
- find指令
- 根据名称查找一个文件
- 参数
- 根据文件大小查找
- 参数
- 按照时间进行查找
- 参数
find命令介绍
格式:find 搜索路径 参数 参数相关匹配值 指令(-print)
find指令
- -print (输出结果,默认)
- -ls 类似于
ls -lhi
根据名称查找一个文件
我记得,Linux系统中有一个文件叫redhat-release,我怎查找到它的位置。
[root@localhost ~]# find / -name "redhat-release"
参数
- -name : 指定搜索的名称
- -i : 忽略大小写
[root@localhost ~]# find /root/ -iname "abc*"
- 通配符
- *:匹配所有(可以匹配空字符)
[root@localhost ~]# find /etc/ -name "ifcfg-*"
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth1
# 查询以eth0开头的文件
[root@localhost ~]# find / -name "eth0*"
/root/eth0xxx
# 查询以eth0结尾的文件
[root@localhost ~]# find / -name "*eth0"
/etc/sysconfig/network-scripts/ifcfg-eth0
/root/xxxeth0
# 查询文件名称包含eth0的文件
[root@localhost ~]# find / -name "*eth0*"
- [] : 匹配中括号中的任意一个字符
[root@localhost ~]# touch abc{1..9}
[root@localhost ~]# find /root/ -name "abc[359]"
/root/abc3
/root/abc5
/root/abc9
[root@localhost ~]# find /root/ -name "oldboy[tsdgh]"
- [^] : 匹配除掉中括号中的内容之外的跟前面匹配的上的所有文件
[root@localhost ~]# find /root/ -name "abc[^359]"
/root/abc1
/root/abc2
/root/abc4
/root/abc6
/root/abc7
/root/abc8
[root@localhost ~]# ls
abc1 abc8 oldboyc oldboyj oldboyq oldboyx
abc2 abc9 oldboyd oldboyk oldboyr oldboyy
abc3 anaconda-ks.cfg oldboye oldboyl oldboys oldboyz
abc4 dir
- ! : 对整个搜索结果进行取反
[root@localhost ~]# find /root/ -name "abc[^359]"
/root/abc1
/root/abc2
/root/abc4
/root/abc6
/root/abc7
/root/abc8
[root@localhost ~]# find /root/ -name "abc[359]"
/root/abc3
/root/abc5
/root/abc9
[root@localhost ~]# find /root/ ! -name "abc[359]"
- ?:匹配任意一个字符(该字符不能为空)
[root@localhost ~]# find /root/ -name "abc*"
/root/abc1
/root/abc2
/root/abc3
/root/abc4
/root/abc5
/root/abc6
/root/abc7
/root/abc8
/root/abc9
/root/abc10
/root/abc11
/root/abc12
/root/abc13
/root/abc14
/root/abc15
/root/abc16
/root/abc17
/root/abc18
/root/abc19
/root/abc20
[root@localhost ~]# find /root/ -name "abc?"
/root/abc1
/root/abc2
/root/abc3
/root/abc4
/root/abc5
/root/abc6
/root/abc7
/root/abc8
/root/abc9
[root@localhost ~]#
根据文件大小查找
根据文件的体积查询出我们所需要的文件,find会自动四舍五入
参数
- -a :并且(默认使用-a)
[root@localhost ~]# find /var/log -size +2k -size -10k
/var/log
/var/log/secure
/var/log/vmware-vgauthsvc.log.0
/var/log/vmware-vmsvc.log
/var/log/cron
/var/log/vmware-network.3.log
[root@localhost ~]# find /var/log -size +2k -a -size -10k
- -o : 或者
[root@localhost ~]# find /var/log -size -2k -o -size +10k
- -size
# b(默认) c w K M G
-size n (单位)
+n : 大于n个单位
-n : 小于n个单位
n : 等于n个单位
# 注:find查找同时打印出隐藏文件
# 注:n必须是整数,不能是小数
[root@localhost log]# find /root/ -size 8.8k
- +n : 大于n个单位的文件
[root@localhost ~]# find /root/ -size +1k
- -n : 小于n个单位的文件
[root@localhost ~]# find /root/ -size -1k
- n : 等于n个单位的文件但是不精确
[root@localhost ~]# ls -lh
total 640K
-rw-r--r-- 1 root root 632K Mar 9 10:31 123.log
-rw-------. 1 root root 1.8K Mar 3 10:51 anaconda-ks.cfg
drwxr-xr-x. 3 root root 17 Mar 4 10:51 dir
-rw-r--r-- 1 root root 0 Mar 9 09:28 eth0xxx
-rw-r--r-- 1 root root 0 Mar 9 09:28 xxxeth0
-rw-r--r-- 1 root root 0 Mar 9 09:28 xxxeth0xxx
-rw-r--r--. 1 root root 2.3K Mar 4 10:46 系统优化.md
[root@localhost ~]# find /root/ -size 2k -ls
- c : 字节
[root@localhost log]# find /var/log -size 674c
/var/log/vmware-network.5.log
/var/log/vmware-network.4.log
/var/log/vmware-network.2.log
[root@localhost log]# find /var/log/ -size -300c -ls
按照时间进行查找
按照文件的创建时间、修改时间以及访问时间进行筛选查找。
参数
- -ctime: 文件变更时间(修改了位置(mv)、所属组、所属用户)
[root@localhost test]# for i in 1 2 3 4 5 6 7 8 9;do date -s "2021-03-0$i 11:30:30";touch 2021-03-0$i.txt; done && ntpdate ntp.aliyun.com
[root@localhost test]#
[root@localhost test]# # 1、查询文件变更时间在3天以前
[root@localhost test]# # 2、查询文件变更时间在3天以内
[root@localhost test]# # 3、查询文件变更时间在3天这个时候的
[root@localhost test]#
[root@localhost test]#
[root@localhost test]# find ./ -ctime +3
./2021-03-01.txt
./2021-03-02.txt
./2021-03-03.txt
./2021-03-04.txt
./2021-03-05.txt
[root@localhost test]# find ./ -ctime -3
./
./2021-03-07.txt
./2021-03-08.txt
./2021-03-09.txt
[root@localhost test]# find ./ -ctime 3
./2021-03-06.txt
[root@localhost test]#
[root@localhost test]# mkdir backup
[root@localhost test]# mv 2021-03-01.txt backup/
[root@localhost test]# stat backup/2021-03-01.txt
File: ‘backup/2021-03-01.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 17307574 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-03-01 11:30:30.000000000 +0800
Modify: 2021-03-01 11:30:30.000000000 +0800
Change: 2021-03-09 11:58:06.369682903 +0800
Birth: -
[root@localhost test]# find ./ -ctime ^C
[root@localhost test]# mv 2021-03-01.txt backup/^C
[root@localhost test]#
[root@localhost test]#
[root@localhost test]# find ./ -ctime -3
./
./2021-03-07.txt
./2021-03-08.txt
./2021-03-09.txt
./backup
./backup/2021-03-01.txt
[root@localhost test]#
- atime : 访问时间(cat)
[root@localhost test]# rm -rf ./* && for i in 1 2 3 4 5 6 7 8 9;do date -s "2021-03-0$i 11:30:30";touch 2021-03-0$i.txt; done
Mon Mar 1 11:30:30 CST 2021
Tue Mar 2 11:30:30 CST 2021
Wed Mar 3 11:30:30 CST 2021
Thu Mar 4 11:30:30 CST 2021
Fri Mar 5 11:30:30 CST 2021
Sat Mar 6 11:30:30 CST 2021
Sun Mar 7 11:30:30 CST 2021
Mon Mar 8 11:30:30 CST 2021
Tue Mar 9 11:30:30 CST 2021
[root@localhost test]# ntpdate ntp.aliyun.com
9 Mar 12:03:08 ntpdate[22696]: step time server 203.107.6.88 offset 1938.313235 sec
[root@localhost test]#
[root@localhost test]#
[root@localhost test]#
[root@localhost test]#
[root@localhost test]#
[root@localhost test]#
[root@localhost test]#
[root@localhost test]# find ./ -atime +3
./2021-03-01.txt
./2021-03-02.txt
./2021-03-03.txt
./2021-03-04.txt
./2021-03-05.txt
[root@localhost test]# find ./ -atime -3
./
./2021-03-07.txt
./2021-03-08.txt
./2021-03-09.txt
[root@localhost test]# find ./ -atime 3
./2021-03-06.txt
[root@localhost test]# cat 2021-03-03.txt
[root@localhost test]# find ./ -atime +3
./2021-03-01.txt
./2021-03-02.txt
./2021-03-04.txt
./2021-03-05.txt
[root@localhost test]# find ./ -atime -3
./
./2021-03-03.txt
./2021-03-07.txt
./2021-03-08.txt
./2021-03-09.txt
[root@localhost test]# stat 2021-03-03.txt
File: ‘2021-03-03.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 17307576 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-03-09 12:03:59.906083902 +0800
Modify: 2021-03-03 11:30:30.000000000 +0800
Change: 2021-03-03 11:30:30.000000000 +0800
Birth: -
[root@localhost test]#
- -mtime: 内容修改时间(包含创建时间)
# 正好3天前,这一天创建的文件
[root@localhost test]# find /root/test/ -mtime 3
/root/test/2021-03-06.txt
# 正好3天以内,创建的文件
[root@localhost test]# find /root/test/ -mtime -3
/root/test/
/root/test/2021-03-07.txt
/root/test/2021-03-08.txt
# 正好3天以前创建的文件
[root@localhost test]# find /root/test/ -mtime +3
/root/test/
/root/test/2021-03-05.txt
/root/test/2021-03-04.txt
[root@localhost test]#