Find命令用法

Linux find命令用來在指定目錄下查找文件。

使用格式

 find  [指定查找目錄]  [查找規(guī)則]  [查找完后執(zhí)行的action]

舉例

  • 指定查找目錄
[root@VM-Sonar-104 ssh]# find /etc/ssh  /usr/local/sonarqube/bin/linux-x86-64/
/etc/ssh
/etc/ssh/ssh_config
/etc/ssh/ssh_host_key
/etc/ssh/sshd_config
/etc/ssh/ssh_host_dsa_key
/etc/ssh/ssh_host_dsa_key.pub
/etc/ssh/ssh_host_key.pub
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/moduli
/etc/ssh/ssh_host_rsa_key
/usr/local/sonarqube/bin/linux-x86-64/
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh
/usr/local/sonarqube/bin/linux-x86-64/SonarQube.pid
/usr/local/sonarqube/bin/linux-x86-64/wrapper
/usr/local/sonarqube/bin/linux-x86-64/lib
/usr/local/sonarqube/bin/linux-x86-64/lib/libwrapper.so

這里要注意的是目錄之間要用空格分開

  • 指定查找規(guī)則

a.根據(jù)文件名查找,精確查找

[root@VM-Sonar-104 ssh]# find /etc/ssh -name ssh_config
/etc/ssh/ssh_config

b.根據(jù)文件名查找,但不區(qū)分大小寫

[root@VM-Sonar-104 ssh]# find /etc/ssh -iname ssh_config
/etc/ssh/ssh_config

c.根據(jù)文件所屬用戶和所屬組來查找文件

[root@VM-Sonar-104 ssh]# find /etc/ssh -user root
/etc/ssh
/etc/ssh/ssh_config
/etc/ssh/ssh_host_key
/etc/ssh/sshd_config
/etc/ssh/ssh_host_dsa_key
/etc/ssh/ssh_host_dsa_key.pub
/etc/ssh/ssh_host_key.pub
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/moduli
/etc/ssh/ssh_host_rsa_key

d.根據(jù)文件時間戳的相關(guān)屬性來查找文件
我們可以使用stat命令來查看一個文件的時間信息如下:

[root@VM-Sonar-104 ssh]# stat /etc/ssh
  File: `/etc/ssh'
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d  Inode: 12059408    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-21 15:00:04.353831471 +0800
Modify: 2017-12-01 14:19:49.535425401 +0800
Change: 2017-12-01 14:19:49.535425401 +0800

查找五天內(nèi)沒有訪問過的文件

[root@VM-Sonar-104 ssh]# find ./ -atime +5

查找五天內(nèi)訪問過的文件

[root@VM-Sonar-104 ssh]# find ./ -atime -5

時間戳:


e.根據(jù)文件類型來查找文件,加參數(shù)-type

[root@VM-Sonar-104 ssh]# find /usr/test -type d
[root@VM-Sonar-104 ssh]# find /usr/test  -d
//f     普通文件
//d     目錄文件
//l     鏈接文件
//b     塊設(shè)備文件
//c     字符設(shè)備文件
//p     管道文件
//s     socket文件

f.根據(jù)大小來查找文件-size

[root@VM-Sonar-104 ssh]#find  /tmp  -size   2M //查找在/tmp目錄下等于2M的文件

g.根據(jù)文件權(quán)限查找文件-perm

[root@VM-Sonar-104 ssh]#find  /tmp  -perm  755  //查找在/tmp目錄下權(quán)限是755的文件
  • 查找完執(zhí)行的action
[root@VM-Sonar-104 ssh]# find ./ -atime -5 -print //-print默認情況下的動作
[root@VM-Sonar-104 ssh]# find ./ -atime -5 -ls //-ls查找到后用ls顯示出來

刪除5天之前的文件和目錄,執(zhí)行命令時先詢問

[root@VM-Sonar-104 ssh]# find /Users/test -mtime +5 -name "*" -ok rm -rf {} \;//-ok [commend]查找后執(zhí)行命令的時候詢問用戶是否要執(zhí)行

刪除5天之前的文件和目錄,執(zhí)行命令時不用詢問

[root@VM-Sonar-104 ssh]# find /Users/test -mtime +5 -name "*" -exec rm -rf {} \; //-exec查找后執(zhí)行命令的時候不詢問用戶直接執(zhí)行

注:用{}來替代查找到的文件,;表示使用-exec的結(jié)束符,是固定格式。

實踐
我們在用jenkins做持續(xù)集成時,要刪除checkout 的.svn文件時,就可以用find命令來處理。具體命令如下:

find ${WORKSPACE} -type d -name ".svn"|xargs rm -rf

注:xargs可以把從 stdin 接受到的輸入,用空白符分隔開,然后依次作為參數(shù)去調(diào)用xargs后面的命令。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容