2019-03-21笔记—查找命令(find为主)

系统中查找文件或者目录的命令

  1. which(which只能用来查找PATH环境变量中出现的路径下的可执行文件)
[root@linux2019 ~]# which tar
/usr/bin/tar
  1. whereis(通过预先生成的一个文件列表库去查找跟给出的文件名相关的文件)
[root@linux2019 ~]# whereis tar
tar: /usr/bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz
  1. locate (和whereis类似,也是通过查找预先生成的文件列表库来告诉用户要查找的文件在哪里,后边直接跟文件名,通过mlocate安装包yum安装,安装完成后需要更新db库(updatedb),这个命令搜索不够精确,使用频率低)
[root@linux2019 ~]# locate netstat
/usr/bin/netstat
/usr/share/man/de/man8/netstat.8.gz
/usr/share/man/fr/man8/netstat.8.gz
/usr/share/man/man8/netstat.8.gz
/usr/share/man/pt/man8/netstat.8.gz
  1. find (语法 : find [路径] [参数])
  • -atime +n/-n : 访问或执行时间大于/小于n天的文件
  • -ctime +n/-n : 写入、更改inode属性(例如更改所有者、权限或者链接)时间大于/小于n天的文件
  • -mtime +n/-n : 写入时间大于/小于n天的文件
  • -mmin +n/-n : 更改时间大于/小于n分钟的文件
[root@linux2019 ~]# find /tmp/ -atime +10
/tmp/.font-unix
/tmp/.Test-unix
/tmp/.XIM-unix
/tmp/.ICE-unix
/tmp/.X11-unix
[root@linux2019 ~]# find /tmp/ -mmin -1
/tmp/hsperfdata_zabbix/9108
  • -name filename 直接查找该文件名的文件
[root@linux2019 ~]# find /data/ -name mysql
/data/backup/2019-03-17/mysql
/data/backup/2019-03-17_inc1/mysql
/data/backup/2019-03-17_inc2/mysql
/data/backup/2019-03-17_inc3/mysql
/data/mysql.bak/mysql
/data/mysql
/data/mysql/mysql
  • -type filetype 通过文件类型查找。文件类型有 f, b, c, d, l, s 等
[root@linux2019 ~]# find /tmp/   -type s
/tmp/blog.socket
/tmp/mysql.sock
  • -size [+n/-n] 根据大小查找(单位:c k M G),其中字节不是b,而是c
[root@linux2019 ~]# find /data/ -size +50M
/data/backup/2019-03-17/ibdata1
/data/mysql.bak/ibdata1
/data/mysql/ibdata1
[root@linux2019 ~]# find /data/ -size +50M| xargs du -sh 
76M	/data/backup/2019-03-17/ibdata1
76M	/data/mysql.bak/ibdata1
76M	/data/mysql/ibdata1
  • -perm [+/-]权限 #使用情况很少,了解即可
  • -iname #忽略大小写文件名查找
  • -maxdepth n #指定查找目录深度
[root@linux2019 ~]# find . -maxdepth 2 -iname "*nginx*.md" 
./linux2019/4.30-编译安装Nginx.md
./linux2019/4.31-yum安装Nginx.md
./linux2019/4.32-Nginx虚拟主机.md
./linux2019/4.38-Nginx访问日志.md
./linux2019/4.40-Nginx日志切割.md
./linux2019/4.41-Nginx静态文件过期.md
./linux2019/4.42-Nginx防盗链.md
./linux2019/4.48-Nginx反向代理.md
./linux2019/4.50-Nginx负载均衡.md
./linux2019/4.51-Nginx配置ssl.md
./linux2019/4.60-Nginx代理Tomcat.md
./linux2019/5.22-zabbix监控Nginx.md
  • -o 执行多个条件查找 #查找所有以.txt和.pdf结尾的文件
[root@linux2019 ~]# find . \( -name "*.sh" -o -name "*.pdf" \)
./shell/case.sh
./shell/for1.sh
./shell/for2.sh
./shell/while1.sh
./shell/while2.sh
./shell/break.sh
./shell/continue.sh
./shell/break2.sh
./shell/continue2.sh
./shell/exit.sh
./shell/function1.sh
./shell/function2.sh
./shell/function3.sh
  • 基于正则表达式匹配文件路径
find  . -regextype "posix-egrep"  -regex  ".*(\.txt|\.pdf)$"
  • 基于正则表达式匹配文件路径(忽略大小写)
find  . -regextype "posix-egrep"  -iregex  ".*(\.txt|\.pdf)$"
  • ! #取反查找
[root@linux2019 ~]# find /home ! -name "*.txt"
/home
/home/php-fpm
/home/php-fpm/.bash_logout
/home/php-fpm/.bash_profile
/home/php-fpm/.bashrc
/home/php-fpm/.pki
/home/php-fpm/.pki/nssdb
/home/ftpuser
/home/ftpuser/.bash_logout
/home/ftpuser/.bash_profile
/home/ftpuser/.bashrc
  • 查找后再处理–删除
# find . -type f -name "*.gz" -mtime +30 -exec rm -f {} \;
# find . -type f -name "*.gz" -mtime +30 -ok rm {} \;
# find . -type f -name "*.gz" -mtime +30 |xargs rm -f
# -ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
  • 查找后再处理–改名
# find . -name "*.txt" -exec mv {} {}.bak \;
# find . -name "*.txt"|xargs -i mv {} {}.bak

猜你喜欢

转载自blog.csdn.net/ai_benwoniu/article/details/88726753