奥尔覅几22 发表于 2018-8-25 12:44:50

shell编程学习之find

  find   查找命令
  1 打印当前列表
  # find . -print
  .
  ./install.log.bak
  ./.bash_logout
  ./.bash_history
  ./install.log.syslog.bak
  ./.tcshrc
  ./.rnd
  2 查找/root下面以bak后缀,并打印出来
  # find /root/ -name '*.bak' -print
  /root/install.log.bak
  /root/install.log.syslog.bak
  /root/install.log.bak.bak
  /root/sendEmail-v1.56.tar.gz.bak.bak
  /root/sendEmail-v1.56.tar.gz.bak
  /root/install.log.syslog.bak.bak
  3 查找/root下面以bak和txt后缀,并打印出来
  # find /root/ \( -name '*.bak' -o -name '*.txt' \) -print
  /root/install.log.bak
  /root/2.txt
  /root/1.txt
  /root/install.log.syslog.bak
  /root/install.log.bak.bak
  /root/sendEmail-v1.56.tar.gz.bak.bak
  /root/3.txt
  /root/4.txt
  /root/sendEmail-v1.56.tar.gz.bak
  /root/install.log.syslog.bak.bak
  4 查找/root除了txt后缀,并打印出来
  # find /root ! -name '*.txt' -print
  /root
  /root/install.log.bak
  /root/.bash_logout
  /root/.bash_history
  /root/install.log.syslog.bak
  /root/.tcshrc
  /root/.rnd
  /root/.cshrc
  /root/install.log.bak.bak
  /root/.bashrc
  5 查找/root下面所有的目录
  # find /root/ -type d -print
  /root/
  /root/test1
  /root/test
  6查找/root下面普通文件
  # find /root/ -type f -print
  /root/install.log.bak
  /root/.bash_logout
  /root/2.txt
  /root/1.txt
  /root/.bash_history
  /root/install.log.syslog.bak
  /root/.tcshrc
  /root/.rnd
  /root/.cshrc
  /root/install.log.bak.bak
  /root/.bashrc
  /root/install.log
  7 查找/root下面链接文件
  # find /root/ -type l -print
  /root/test-test
  8 查找/root最近两天被访问的文件
  # find /root/ -atime -2 -print
  /root/
  /root/test-test
  /root/2.txt
  /root/1.txt
  /root/.bash_history
  /root/test1
  /root/.bashrc
  /root/3.txt
  /root/.bash_profile
  /root/4.txt
  9 查找/root最近两天被修改的文件
  # find /root/ -mtime -2 -print
  /root/
  /root/test-test
  /root/2.txt
  /root/1.txt
  /root/.bash_history
  /root/test1
  /root/3.txt
  /root/4.txt
  /root/test
  10 查找/root最后两天改变的时间的文件
  # find /root/ -ctime +2 -print
  /root/install.log.bak
  /root/.bash_logout
  /root/install.log.syslog.bak
  /root/.tcshrc
  /root/.rnd
  /root/.cshrc
  /root/install.log.bak.bak
  /root/.bashrc
  /root/install.log
  11 查找/root下权限为777的文件
  # find /root/ -perm 777 -print
  /root/test-test
  /root/2.txt
  /root/1.txt
  12查找/root下用户是test的文件
  # chown test:test test.sh
  # ll|grep test.sh
  -rw-r--r-- 1 test test   218 12-23 03:38 test.sh
  # find /root/ -user test -print
  /root/test.sh
  13查找/root文件名为2.txt并删除其文件
  # find /root/ -name '2.txt'               #已查找到
  /root/2.txt
  # find /root/ -name '2.txt' -delete位      #删除
  # find /root/ -name '2.txt'                  #再次查找已没有
  #
  14查找/root下一天前的以bak后缀的文件,并复制到/root/find-test文件夹
  # find /root/ -mtime +2 -name '*.bak'   #查找是否有bak后缀的文件
  /root/install.log.bak
  /root/install.log.syslog.bak
  /root/install.log.bak.bak
  /root/sendEmail-v1.56.tar.gz.bak.bak
  /root/sendEmail-v1.56.tar.gz.bak
  /root/install.log.syslog.bak.bak
  # find /root/ -mtime +2 -name '*.bak' -exec cp {} /root/find-test/\; #查找复制
  # cd find-test/      #进文件夹查看
  # ls
  install.log.bak      install.log.syslog.bak      sendEmail-v1.56.tar.gz.bak
  install.log.bak.bakinstall.log.syslog.bak.baksendEmail-v1.56.tar.gz.bak.bak
  #此命令可以用来做备份非常有用,只要更换命令就很强大
  15多匹配查找/root下面以txt后缀的文件
  # find /root/ -name '.txt' -print
  /root/b.txt
  /root/a.txt
  # find /root/ -name '.txt' -print
  /root/b2.txt
  /root/a1.txt
  16 查找/root下大于3K的文件夹
  # find /root/-size +3k -print
  /root/
  /root/test
  17 查找/root下面但排除/root/test文件夹里面并以txt后缀的文件
  # find /root/ -path '/root/test/' -prune -o -name '*.txt' -print
  /root/ABCD.txt
  /root/b.txt
  /root/2.txt
  /root/b2.txt
  /root/a.txt
  /root/test/1.txt
  /root/abc.txt
  /root/a1.txt
  18 查找/root下面但排除多个文件夹里面并以txxt后缀的文件
  # find /root/ \( -path '/root/test' -o -path '/root/test1/' \) -prune -o -name '*.txt' -print
  /root/ABCD.txt
  /root/b.txt
  /root/2.txt
  /root/test1/9.txt
  /root/b2.txt
  /root/a.txt
  /root/abc.txt
  /root/a1.txt
  19查找/root下面普通文件并排序
  # find /root/ -type f|sort
  /root/2.txt
  /root/a1.txt
  /root/ABCD.txt
  /root/abc.txt
  /root/a.txt
  /root/b2.txt
  /root/b.txt
  /root/test1/9.txt
  /root/test/1.txt
  20查看硬件设备
  # find /dev/cdrom -print
  /dev/cdrom
  21 以上只是find基础,要想find发挥的更好,需要xargs的配合使用,请看shell编程学习之xargs

页: [1]
查看完整版本: shell编程学习之find