Linux-文件查找命令
目录
find 命令
根据文件名查找文件
# 模糊搜索文件:
-name:
'*.conf'
'conf.*'
'*.conf*'
# 查找/etc目录下名称为 ifcfg-eth0 的文件
[root@wqh ~]# find /etc -name "ifcfg-eth0"
# -i 忽略大小写
[root@wqh ~]# find /etc -iname "ifcfg-eth0"
# 查找/etc目录下名称中包含 ifcfg-eth 的所有文件
[root@wqh ~]# find /etc/ -name "ifcfg-eth*"
[root@wqh ~]# find /etc -iname "ifcfg-eth*"
# 查找名称中包含eth 的文件
[root@wqh opt]# find /opt/ -name '*eth*'
[root@wqh opt]# find /opt/ -iname '*eth*'
# 查找名称既不是 file5 也不是 file9 的文件
find /root/dir ! (-name 'file5' -o -name 'file9' )
根据文件的大小查找
## -size的用法
-size N[cwbkMG]
`b' block(512字节)
`c' bytes 字节
`w' words 单词(2字节)
`k' kb
`M' MB
`G' GB
+7k # 大于7k
-7k # 小于7k
7k # 等于7k(大致等于,不太准确)
# 查找大于5M的文件
[root@wqh ~]# find /etc/ -size +5M
/etc/udev/hwdb.bin
# 查找等于5M的文件
[root@wqh ~]# find /etc/ -size 5M
# 查找小于5M的文件
[root@wqh ~]# find /etc/ -size -5M
## 动作,查看
[root@wqh ~]# find /etc/ -size +5M -ls
## 动作,删除
[root@wqh ~]# find /tmp/ -size +5M -delete
## 查找小于5M的文件,且名称中包含 .sh
[root@wqh ~]# find /etc/ -size -5M -name '*.sh'
## 需求:在/etc 找到 .sh 或 .db 结尾的 小于5M的文件
[root@wqh ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.db'
## 需求:在/etc/ 找到 大于3M 小于5M 名字以.sh结尾或.conf结尾
[root@wqh ~]# find /etc/ \( -size +3M -a -size -6M \) -a \( -name '*.sh' -o -name '*.conf' \)
## 需求:在/etc 目录下找到小于5M 并且 文件名不是以 .sh 结尾 或者 不是以 .db结尾
[root@wqh ~]# find /etc/ -size -5M ! \( -name '*.sh' -o -name '*.db' \)
根据文件类型查找
# 各参数代表的文件类型
f:普通文件
d:目录
l:软连接文件
s:安全套接字文件
p:管道文件
b:块设备
c:字符设备
find / -type f
find / -type d
find / -type b
# 使用find命令的动作显示详细信息
find / -type f -ls
# 使用xargs命令显示详细信息(更常用)
find / -type f|head -5|xargs ls -l
根据日期查找
## 根据三种时间查找文件
-atime:access,访问时间
-ctime:change,修改元数据时间
-mtime:modify,修改block内容时间
-7 # 查找最近7天的文件
+7 # 查找7天之前的文件,不包含今天
7 # 查找第7天的文件,不包含今天
## 找七天之前的文件(不包含今天)
[root@wqh ~]# find /opt/ -mtime +7 -name '*.txt'
## 找最近七天的文件
[root@wqh ~]# find /opt/ -mtime -7 -name '*.txt'
## 找第七天的(不包含今天)
[root@wqh ~]# find /opt/ -mtime 7 -name '*.txt'
## 企业需求:只保留N天的备份,其余的都删除
[root@wqh ~]# find /opt/ ! -mtime -7 -name '*.txt' -delete
[root@wqh ~]# find /opt/ ! -mtime -30 -name '*.txt' -delete
find-根据用户查找文件
## 根据用户查找文件的选项
-user:查找属主是X个用户
-group:查找属组是X个组
-nouser:查找没有用户
-nogroup:查找没有组
[root@wqh opt]# find ./ -user wqh|xargs ls -l
-rw-r--r--. 1 wqh qiandao 0 Apr 16 00:36 ./file1
-rw-r--r--. 1 wqh wqh 0 Apr 16 00:36 ./file3
[root@wqh opt]# find ./ -user wqh -group qiandao
./file1
[root@wqh opt]# find ./ -user wqh -o -group qiandao
./file1
./file3
./file4
find-根据深度查找
## -maxdepth选项的用法
-maxdepth level:寻找指定目录下的level层
# 查找/etc/目录下2层,名字为'*.conf'的文件
[root@wqh ~]# find /etc/ -maxdepth 2 -type f -name '*.conf'
find-动作选项
## find命令内置动作选项
-print:打印出查找的内容,find默认参数
-ls:查看文件详细信息
-delete:删除文件
-exec:执行
语法: -exec \;
-ok:执行并询问
语法: -ok \;
# 查看文件详细信息
[root@wqh ~]# find /opt/ ! -perm /222 -ls
# 删除文件,也可以用xargs
[root@wqh opt]# find /opt/ -type d ! -name 'opt' -delete
[root@wqh opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr
# 拷贝找到的文件到/tmp下,也可以用xargs命令
[root@wqh opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/
[root@wqh opt]# find /opt/ -mtime +5 |xargs -i cp {} /tmp/
[root@wqh opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/
[root@wqh opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ \;
[root@wqh opt]# find /opt/ -name '*.txt' -ok cp {} /tmp/ \;
< cp ... /opt/2020-04-01_file.txt > ? y
< cp ... /opt/2020-04-02_file.txt > ? y
< cp ... /opt/2020-04-03_file.txt > ? y
< cp ... /opt/2020-04-04_file.txt > ? y
......
find-结合xargs
xargs命令配合管道符使用,可以将管道符左边的输出结果,作为数据流交给xargs后的命令,依靠xargs命令,可以将查找到的文件内容做批量处理,
可以使用文件管理命令删除、拷贝、移动这些文件,也可以使用sed命令去修改获取的文件中的内容。
简单一点想,就是将| xargs前获得的结果,当做文件交给后方命令。
#拷贝
find / -type f |xargs cp -t /tmp
#查看
find / -type f |xargs ls -l
#替换
find / -type f |xargs sed -i 's###g'
#移动
find / -type f |xargs mv -t /tmp
#删除
find / -type f |xargs rm -fr
find-条件语句
# find命令中的且、或、非选项
-a:and,且
-o:or,或
!:not,非
locate 命令
locate命令和slocate命令都用来查找文件或目录。 locate命令其实是find -name的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/locatedb,这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用
updatedb命令,手动更新数据库。
# 若首次安装需要先更新一下数据库
[root@wqh06 ~]# updatedb
[root@wqh06 ~]# locate user01 | head -5
/home/user01
/home/user01/.bash_history
/home/user01/.bash_logout
/home/user01/.bash_profile
/home/user01/.bashrc
# 模糊搜索locate数据库中的内容, `*` 只能放置在首部或尾部
[root@wqh06 ~]# locate user01*
/root/user012
/root/user012333
[root@wqh06 ~]# locate *012333
/root/user012333
which 命令
which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。
[root@wqh06 ~]# which pwd
/bin/pwd
[root@wqh06 ~]# which adduser
/usr/sbin/adduser
whereis 命令
whereis命令用来定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。 whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。
[root@wqh06 ~]# whereis man
man: /usr/bin/man /usr/share/man /usr/share/man/man1/man.1.gz
[root@wqh06 ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
记录成长过程

浙公网安备 33010602011771号