1. find
linux中,find命令一般用來按特定條件查找文件,生產環境中也常用其來過濾文件
名稱 find - 搜索目錄層次結構中的文件 格式 find 【目錄】 {【選項】 【參數】}... 常見選項 -type 指定要查找的文件類型(常用的有 1.f普通文件 2.d目錄文件) -name 指定要查找的文件名(文件名稱可用通配符模糊匹配) ! 取反,在選項名稱前加!可取反選項匹配的內容 -exec 對查找到的內容執行相關操作,命令后要加 "\;" 為特定格式(如:find . -type f ! -name "love" -exec rm {} \;) -and 與匹配,find多條件查找時默認使用與匹配 -or 或匹配 -maxdepth 查找深度,1個目錄則為1層深度 擴展: | 通過管道把find查找的內容傳下去以完成一些不為人知的事情 xargs 將管道接收過來的內容進行整合成想要的形式以和其他命令配合使用
2. 簡單用法
1.查看當前目錄的所有文件 find . 2.查看/test/目錄下的常規文件 find /test/ -type f 3.查看/test/目錄下名為love的目錄文件 find /test/ -type d -name love 4.查看/test/目錄下除了love文件的其他常規文件 find /test/ -type f ! -name love 5.查看/test/目錄下以.log結尾的文件 find /test/ -name *.log 6.查看/test/目錄下以.log結尾的文件和love文件 find /test -name *.log -or -name love 7.查看/test/目錄下2層目錄內的文件 find /test -maxdepth 2 8.查找到/test/目錄下以.log結尾的文件並刪除 find /test -type f -name *.log$ -exec rm {} \; 或: find /test -type f -name *.log | xargs rm
3. find命令實用實例
將15天以前的日志文件刪除掉
find . -type f -name "*.log$" -mtime +15 | xargs rm -f