一、文件查找
简介:
which :命令查找
find: 文件查找,针对文件名
locate:文件查找,依赖数据库
二、命令查找
命令文件查找:which /whereis
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
三、任意文件查找
1.locate查找文件,依赖于数据库,新建文件无法查找需更新数据库(updatedb)
2.find [path…] [options] [expression] [action]
命令 路径 选项 表达式 动作
(1)按文件名:
查找hosts文件
[root@localhost ~]# find /etc -name "hosts"
/etc/hosts
/etc/avahi/hosts
[root@localhost ~]# find /etc -iname "hos*" //-i忽略大小写
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
......
(2)按文件大小:
查找etc文件下大于5M的文件
[root@localhost ~]# find /etc/ -size +5M
/etc/udev/hwdb.bin
(3)指定查找的目录深度
需要指明具体深度
[root@localhost ~]# find / -maxdepth 3 -a -name "ifcfg*"
/usr/sbin/ifcfg
(4)按文件类型:
f普通文件 b块设备文件 d目录 p管道 l链接
[root@localhost ~]# find /tmp -type f
/tmp/bitrock_installer_24706.log
/tmp/yum_save_tx.2020-12-10.08-39.1ROXq6.yumtx
/tmp/yum_save_tx.2020-12-07.08-47.mKAR6q.yumtx
/tmp/yum_save_tx.2020-12-08.08-45.8hpJlw.yumtx......
[root@localhost ~]# find /dev -type b
/dev/dm-2
/dev/md0
/dev/dm-1
/dev/dm-0
/dev/sr0
......
(5)按文件属主属组:
查找用户和组,前提是所查找的用户和组已存在。
[root@localhost ~]# find /home -user yonghu
/home/yonghu
/home/yonghu/.mozilla
/home/yonghu/.mozilla/extensions
/home/yonghu/.mozilla/plugins
......
[root@localhost ~]# find /home -group yonghu
/home/yonghu
/home/yonghu/.mozilla
/home/yonghu/.mozilla/extensions
/home/yonghu/.mozilla/plugins
......
(6)按文件权限
[root@localhost ~]# find . -perm 777
./桌面/aha.txt
./桌面/file11
./桌面/file111
./etc/mtab
./etc/fonts/conf.d/65-0-lohit-bengali.conf
......
四、文件找到后的处理
- 显示文件名 print
[root@localhost ~]# find . -perm 715 -print
./桌面/yu111.txt
- 显示详细信息 ls
[root@localhost ~]# find . -perm 715 -ls
17638631 0 -rwx--xr-x 1 root root 0 11月 10 17:17 ./u111.txt
- 找到后删除 delete
[root@localhost ~]# ls /root/桌面/
aha.txt file222 yu111.txt
[root@localhost ~]# find /root/ -name "file222" -delete
[root@localhost ~]# ls /root/桌面/
aha.txt yu111.txt
- 找到后复制 cp
[root@localhost ~]# find /etc -name "ifcfg*" //查找文件
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# mkdir -v /root/桌面/abbbb //创建目录
mkdir: 已创建目录 "/root/桌面/abbbb"
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /root/桌面/abbbb \; //将查找到的文件复制 {}填充寻找的结果
< cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? yes
"/etc/sysconfig/network-scripts/ifcfg-lo" -> "/root/桌面/abbbb/ifcfg-lo"
< cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? yes
"/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/root/桌面/abbbb/ifcfg-ens33"
[root@localhost ~]# ls /root/桌面/abbbb/ //查看验证
ifcfg-ens33 ifcfg-lo