grep: 文件內容過濾
find: 文件查找,針對文件名
1、命令文件
# which ls //從PATH環境變量 (echo $PATH)
# whereis vim
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin
2、任意文件
A. locate (查詢的數據庫: /var/lib/mlocate/mlocate.db)
計劃任務:天天自動更新數據庫 /etc/cron.daily/mlocate.cron
手動更新數據庫:updatedb
# locate ifcfg-eth0
# locate ifcfg-enp0s25
locate
updatedb後才能查找到 很是麻煩 不建議使用
若是沒有 locate 使用YUM直接安裝是不行的。 要查一下在哪一個包裏 yum provides locate -→ mlocate → 直接YUM mlocate便可
B. find
find [options] [path...] [expression] [action]
===expression=== 熟用*通配符
按文件名:
[root@tianyun ~]# find /etc -name "ifcfg-eth0" name 名字 後面-print 已省略
[root@tianyun ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小寫
[root@tianyun ~]# find /etc -iname "ifcfg-eth*"
按文件大小:
[root@tianyun ~]# find /etc -size +5M //大於5M
[root@tianyun ~]# find /etc -size 5M
[root@tianyun ~]# find /etc -size -5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls找到的處理動做 不是平時用的ls
ll - h 查看大小
指定查找的目錄深度:
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-eth0" maxdepth 3 最大3層 a要知足2個條件 而且
按時間找(atime,mtime,ctime):
[root@tianyun ~]# find /etc -mtime +5 //修改時間超過5天
[root@tianyun ~]# find /etc -mtime 5 //修改時間等於5天
[root@tianyun ~]# find /etc -mtime -5 //修改時間5天之內
按文件類型:
[root@tianyun ~]# find /dev -type f //f普通
[root@tianyun ~]# find /dev -type d //d目錄
[root@tianyun ~]# find /dev -type l //l連接
[root@tianyun ~]# find /dev -type b //b塊設備
[root@tianyun ~]# find /dev -type c //c字符設備
[root@tianyun ~]# find /dev -type s //s套接字
[root@tianyun ~]# find /dev -type p //p管道文件
按文件權限:
[root@tianyun ~]# find . -perm 644 -ls .是當前目錄 精確查找644 *通常都是精確
[root@tianyun ~]# find . -perm -644 -ls -是包含到意思
帶不帶- 本身對比一下 查看。 帶-表示只要6就能夠
[root@tianyun ~]# find . -perm -600 -ls
[root@tianyun ~]# find . -perm -222 -ls //全局可寫
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky
==找到後處理的動做 ACTIONS: (默認動做-print)==
-print
-ls
-delete
-exec 後面跟自定義的shell命令
-ok 後面跟自定義的shell命令
[root@tianyun ~]# find /etc -name "ifcfg*" 後能夠加|xargs -h
[root@tianyun ~]# find /etc -name "ifcfg*" -print
[root@tianyun ~]# find /etc -name "ifcfg*" -lsshell
exec命令用於調用並執行指令的命令
查找帶root帶文件 複製到tmp下
find /etc -name 「root*」 -exec cp -rf {} /tmp \; 複製到當前文件下 /tmp換成.
[root@tianyun ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; exec爲執行一條shell命令 {}爲前面的東西\; 格式
[root@tianyun ~]# find /etc -name "ifcfg*" -delete
擴展知識:find結合xargs
[root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf !!!!!!!!!!!!!重點 找到以後刪除處理xargs 參數傳遞處理找出後刪除
[root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp
案例1: 分別找出file5 和除了file5的文件
[root@tianyun ~]# mkdir dir1
[root@tianyun ~]# touch dir1/file{1..20}
[root@tianyun ~]# find /root/dir1 -name "file5"
[root@tianyun ~]# find /root/dir1 ! -name "file5" !爲取反
[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" 便是file5又是file9
/root/dir1/file5
/root/dir1/file9
擴展 不經常使用
[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" -ls
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@tianyun ~]# find /root/dir1 -name "file5" -ls -o -name "file9" -ls
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@tianyun ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls \爲轉譯 不加會報錯
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9
[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’數據庫
2010-11-27 星期六 晴朗當你在命令行執行:
$find . -name 'core' -type f -exec rm {} /;
時,find -exec 命令會對每一個匹配的文件執行一個單獨的rm操做(execute a separate rm for each one), 正如你手動敲入下面命令:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...可是使用這種方式,若是有100個文件匹配了,那麼就須要啓100個進程,一個進程處理一個rm命令。通常來講,其越多進程,意味着越耗性能。咱們能夠換個思路,咱們將要刪除文件看成參數傳遞給rm不就能夠了嗎?也就是說:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...改爲:
rm ./bin/core ./source/shopping_cart/core ./backups/core可是前提是後面的命令必須支持多參數。相有些命令,好比unzip,就不支持輸入多個jar包,因此必須用-exec。
xargs,顧名思義,是對參數進行處理的命令。它的任務就是將輸入行轉換成下一個命令的參數列表。所以上面的find -exec命令能夠改寫成:
find . -name 'core' -type f -print | xargs rmWith this approach, xargs bundles together as many filename arguments as possible for submission to each invocation of rm that's needed, in compliance with the OS's maximum allowed size for an argument list. This means xargs is guaranteed not only to handle all the arguments, but also to use the smallest possible number of processes in doing so. For example, if each command can handle 100 arguments, and there are 110 filenames to process, there will be two invocations of the command, respectively handling 100 and 10 arguments.
其中操做系統容許的最大參數長度由以下命令獲得:
forrest@ubuntu:~$ getconf ARG_MAX
2097152這意味着xargs保證不會由於參數過多而掛掉。因此目前看來惟一須要保證的就是後面的命令支持多參數。好比前面說過的unzip,就不支持多參數,若是你使用xargs xxx.jar
相比之下,也不難看出各自的缺點
一、exec 每處理一個文件或者目錄,它都須要啓動一次命令,效率很差;
二、exec 格式麻煩,必須用 {} 作文件的代位符,必須用 \; 做爲命令的結束符,書寫不便。
三、xargs 不能操做文件名有空格的文件;
綜上,若是要使用的命令支持一次處理多個文件,而且也知道這些文件裏沒有帶空格的文件,
那麼使用 xargs比較方便; 不然,就要用 exec了。express