Linux下的常用查找命令
locate -i, 忽略大小寫 find 根據(jù)文件名或正則表達式搜索 -name 條件限制 -a 與條件 -o 或條件 -not 非條件(此參數(shù)的功能類似于否定參數(shù)‘!’) <code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-a</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找以t開頭并且以.text結(jié)尾的文件</span> find <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找以t開頭或以.text結(jié)尾的文件</span> find <span class="hljs-attribute" style="box-sizing: border-box;">-not</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"t*"</span> <span class="hljs-attribute" style="box-sizing: border-box;">-o</span> <span class="hljs-attribute" style="box-sizing: border-box;">-name</span> <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找不以t開頭或以.text結(jié)尾的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul> 設定find命令在子目錄中遍歷的深度參數(shù): -maxdepth -mindepth 根據(jù)文件類型搜索:
<code class="hljs fsharp has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">l</span> //查找此目錄及子目錄以下的所有符號文件</span> find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> //普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul> 根據(jù)文件時間進行查找: linux文件系統(tǒng)中的每個文件都有三種時間戳: 1. 訪問時間(atime——access time):就是上次訪問這個文件的時間。 2. 改變時間(ctime——change time):就是文件的inode改變的時間(什么是inode?)當你往一個文件中添加數(shù)據(jù)或者刪除數(shù)據(jù)的時候,修改文件所有者的時候,鏈接改變的時候,文件的ctime就會發(fā)生改變。 3. 修改時間(mtime——modification time):就是文件的內(nèi)容上一次發(fā)生改變的時候的時間。 <code class="hljs fsharp has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> -7 #查找7天內(nèi)被訪問的文件</span> find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> 7 #查找恰好在7天前被訪問的文件</span> find -<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">type</span> <span class="hljs-title" style="box-sizing: border-box;">f</span> -<span class="hljs-title" style="box-sizing: border-box;">atime</span> +7 #查找超過7天沒被訪問的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul> -atime,-ctime,-mtime可作為find的時間參數(shù),單位是天。還有基于“分鐘”的,-amin,-mmin -cmin。這些值通常還帶有+或-:+表示大于,-表示小于。 -newer參數(shù),我們可以指定一個用于比較時間戳的參考文件,然后找出比參考文件更新的所有文件。 <code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-newer</span> test4 <span class="hljs-comment" style="box-sizing: border-box;">//查找當前目錄及子目錄下比test4更新的文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li></ul> 基于文件大小的查找 -size參數(shù) <code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-subst" style="box-sizing: border-box;">+</span><span class="hljs-number" style="box-sizing: border-box;">45</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小比45大的普通文件</span> find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-number" style="box-sizing: border-box;">45</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小為45的普通文件</span> find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-size</span> <span class="hljs-subst" style="box-sizing: border-box;">-</span><span class="hljs-number" style="box-sizing: border-box;">45</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找文件大小比45小的普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">3</span></li></ul> 基于文件權限和文件所有人的查找 -perm -user <code class="hljs lasso has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-perm</span> <span class="hljs-number" style="box-sizing: border-box;">664</span> <span class="hljs-comment" style="box-sizing: border-box;">//查找權限為664的普通文件</span> find <span class="hljs-attribute" style="box-sizing: border-box;">-type</span> f <span class="hljs-attribute" style="box-sizing: border-box;">-user</span> lee <span class="hljs-comment" style="box-sizing: border-box;">//查找所有人為lee的普通文件</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul> 結(jié)合find執(zhí)行命令或動作 -exec <code class="hljs mel has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.c"</span> -<span class="hljs-keyword" style="box-sizing: border-box;">exec</span> cat> <span class="hljs-keyword" style="box-sizing: border-box;">file</span>.txt \; #將所有.c文件拼接起來寫入單個文件<span class="hljs-keyword" style="box-sizing: border-box;">file</span>.txt中 find -type f -mtime +<span class="hljs-number" style="box-sizing: border-box;">10</span> -name <span class="hljs-string" style="box-sizing: border-box;">"*.text"</span> -<span class="hljs-keyword" style="box-sizing: border-box;">exec</span> cp /<span class="hljs-number" style="box-sizing: border-box;">5</span>_15 #將<span class="hljs-number" style="box-sizing: border-box;">10</span>天前的.<span class="hljs-keyword" style="box-sizing: border-box;">text</span>文件復制到/tmp下</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">2</span></li></ul> find與xargs結(jié)合使用 xargs參數(shù) 將標準輸入轉(zhuǎn)換成命令行參數(shù) <code class="hljs bash has-numbering" style="display: block; padding: 0px; box-sizing: border-box; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-family:Courier New;font-size:18px;color:#ff0000;">find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.txt"</span> -print0 |xargs -<span class="hljs-number" style="box-sizing: border-box;">0</span> rm <span class="hljs-operator" style="box-sizing: border-box;">-f</span> <span class="hljs-comment" style="box-sizing: border-box;">#將匹配到的文件刪除,xargs -0將 \0作為輸入定界符。</span> find -type f -name <span class="hljs-string" style="box-sizing: border-box;">"*.c"</span> -print0 | xargs -<span class="hljs-number" style="box-sizing: border-box;">0</span> <span class="hljs-operator" style="box-sizing: border-box;">-l</span> <span class="hljs-comment" style="box-sizing: border-box;">#統(tǒng)計所有c程序文件的行數(shù)</span></span></code> 更多必看IT干貨資源盡在大數(shù)據(jù)架構師專家,公眾號 xinsz08 ,聯(lián)系微信czq100w,加入各城市IT運維安全總?cè)?,與大牛同臺交流學習。 |
|