檔案查找
locate:非實時,根據資料庫進行查找,模糊比對。
updatedb手動生成資料庫。
find:最常用的查找指令,功能強大,非常消耗系統資源。
格式:find+查找路徑+查找标準+查找到後的處理動作
查找路徑:預設為目前目錄
查找标準:預設為指定目錄下的所有檔案
處理動作:預設為顯示
-print:顯示
-ls:類似ls -l的形式顯示每一個檔案的詳細資訊
-ok COMMAND {}\; : 每一個操作需要使用者确認
-exec COMMAND {}\ :不需要使用者确認
例如:find /tmp -perm -020 -exec mv {} {}.new \; 花括号中是引用找到的檔案
比對标準:-name 精确比對
-iname 不區分大小寫
-regex 基于正規表達式查找
-user 根據屬主查找
-group 根據屬組查找
-uid 根據uid查找
-gid 根據gid查找
(如果檔案的屬主屬組被删除,則檔案屬性顯示被删屬主屬組的ID号)
-nouser 查找沒有屬主的檔案
-nogroup 查找沒有屬組的檔案
-type 根據檔案類型查找
-perm MODE精确權限比對
/MODE 九位權限中有任何一位比對都行
-MODE 檔案的權限包含查找的權限,例如 766 包含622
+MODE
-size 根據檔案的大小查找,預設為位元組
([+|-]Num[k,M,G],如果查找的檔案大小為10k,則顯示9k到10k的檔案,其他機關也是)
組合條件: -a 與
-o 或
-not 非
(不指定組合條件,預設為-a)
xargs
xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null]
[-d delimiter] [--delimiter delimiter] [-I replace-str] [-i[replace-
str]] [--replace[=replace-str]] [-l[max-lines]] [-L max-lines]
[--max-lines[=max-lines]] [-n max-args] [--max-args=max-args] [-s max-
chars] [--max-chars=max-chars] [-P max-procs] [--max-procs=max-procs]
[--interactive] [--verbose] [--exit] [--no-run-if-empty]
[--arg-file=file] [--show-limits] [--version] [--help] [command
[initial-arguments]]
最經典應用模式: somecommand |xargs -item command
不帶command ,預設的使用echo 輸出
用途:
1.構造參數清單并運作指令,即将接收的參數傳遞給後面的command 指令執行
2.将多行輸入轉換為單行 (特殊功效)
優點:
1. 将輸入參數整理後,去除<newline>換行符,以一個清單形式處理
2. 避免參數過長引發的問題,使用xargs -n 參數适當控制,對于經常産生大量輸出的指令如find、locate和grep來說非常有用
XARGS 一般是和管道一起使用:
XXcomand | xargs -x comand initial-args
-x 代表選項
選項:
-p 操作具有可互動性,每次執行comand都互動式提示使用者選擇
-i -i 選項告訴 xargs 可以使用{}代替傳遞過來的參數, 建議使用-I,其符合POSIX标準
-I
格式: xargs -I rep-str comand rep-srt rep-str 為代替傳遞給xargs參數, 可以使 {} $ @ 等符号 ,其主要作用是當xargs command 後有多個參數時,調整參數位置。例如:
find . -name "*.txt " |xargs -I {} cp {} /tmp
-t 啟用指令行輸出模式:其先回顯要運作的指令,然後執行指令,列印出指令結果,跟蹤與調試xargs的利器,也是研究xargs運作原理的好辦法;
-r
如果沒有要處理的參數傳遞給xargsxargs 預設是帶 空參數運作一次,如果你希望無參數時,停止 xargs,直接退出,使用 -r 選項即可,其可以防止xargs 後面指令帶空參數運作報錯。If the standard input does not contain any nonblanks, do not run the command, exit
-s size
設定每次構造Command行的長度總大小,包括 command +init-param +傳遞參數,Size 參數必須是正整數
-L num
從标準輸入一次讀取num行送給Command指令 ,-l和-L功能一樣,不建議使用。
-n
xargs 的-n選項設定每次送給command指令的參數個數,參數以空白字元或<newline>換行符分割
-L 和 -n 标志是互相排斥的;最後指定的标志生效。
-x 如果有任何 Command 行大于 -s Size 标志指定的位元組數,停止運作 xargs 指令,-L -I -n 預設打開-x參數
常見的經典用法:
find . -name "*.txt" |xargs rm {}
幫助了解各個參數的執行個體
-t 參數,打開調試功能,顯示每次所組的指令,在調試中非常有用,可以看出xargs的執行原理
-L num 參數控制每次輸入的行數,如下是控制每次輸入2行
[cpp] view plain copy
[[email protected] ~/tmp/dir]#ls -l
total 44
-rwx------ 1 root root 1026 Sep 27 05:28 data.txt
-rw-r--r-- 1 root root 1047 Sep 27 05:28 d.txt
-rwx------ 1 root root 2009 Sep 27 05:28 env2.txt
-rwx------ 1 root root 2009 Sep 27 05:28 env.txt
-rwx------ 1 root root 1998 Sep 27 05:28 export2.txt
-rwx------ 1 root root 1998 Sep 27 05:28 export.txt
-rwx------ 1 root root 28 Sep 27 05:28 fuck.txt
-rwx------ 1 root root 5373 Sep 27 05:28 set.txt
-rw-r--r-- 1 root root 21 Sep 27 05:28 s.txt
-rw-r--r-- 1 root root 35 Sep 27 10:13 t.txt
[[email protected] ~/tmp/dir]#ls -l |xargs -t -L 2
/bin/echo total 44 -rwx------ 1 root root 1026 Sep 27 05:28 data.txt
total 44 -rwx------ 1 root root 1026 Sep 27 05:28 data.txt
/bin/echo -rw-r--r-- 1 root root 1047 Sep 27 05:28 d.txt -rwx------ 1 root root 2009 Sep 27 05:28 env2.txt
-rw-r--r-- 1 root root 1047 Sep 27 05:28 d.txt -rwx------ 1 root root 2009 Sep 27 05:28 env2.txt
/bin/echo -rwx------ 1 root root 2009 Sep 27 05:28 env.txt -rwx------ 1 root root 1998 Sep 27 05:28 export2.txt
-rwx------ 1 root root 2009 Sep 27 05:28 env.txt -rwx------ 1 root root 1998 Sep 27 05:28 export2.txt
/bin/echo -rwx------ 1 root root 1998 Sep 27 05:28 export.txt -rwx------ 1 root root 28 Sep 27 05:28 fuck.txt
-rwx------ 1 root root 1998 Sep 27 05:28 export.txt -rwx------ 1 root root 28 Sep 27 05:28 fuck.txt
/bin/echo -rwx------ 1 root root 5373 Sep 27 05:28 set.txt -rw-r--r-- 1 root root 21 Sep 27 05:28 s.txt
-rwx------ 1 root root 5373 Sep 27 05:28 set.txt -rw-r--r-- 1 root root 21 Sep 27 05:28 s.txt
/bin/echo -rw-r--r-- 1 root root 35 Sep 27 10:13 t.txt
-rw-r--r-- 1 root root 35 S
-n num 控制每次輸入的參數個數
假設你希望使用 rm 指令(該指令将作為 xargs 指令的參數)删除檔案。然而,rm 隻能接受有限數量的參數。如果你的參數清單超出該限制怎麼辦?xargs 的 -n 選項限制單個指令行的參數個數。
[[email protected] ~/tmp/dir]#ls
data.txt d.txt env2.txt env.txt export2.txt export.txt fuck.txt set.txt s.txt t.txt
[[email protected] ~/tmp/dir]#ls |xargs -t -n 2 file
file data.txt d.txt
data.txt: ISO-8859 text
d.txt: ISO-8859 text
file env2.txt env.txt
env2.txt: ASCII text, with very long lines
env.txt: ASCII text, with very long lines
file export2.txt export.txt
export2.txt: ASCII text, with very long lines
export.txt: ASCII text, with very long lines
file fuck.txt set.txt
fuck.txt: ASCII text
set.txt: ASCII text, with very long lines
file s.txt t.txt
s.txt: ASCII text
t.txt: ASCII text
[[email protected] ~/tmp/dir]#
-E EOF 指定輸入結束符
[[email protected] ~/tmp/dir]#cat t.txt
tata-hi -fuck - ok
fuck _you _ you
[[email protected] ~/tmp/dir]#xargs -a t.txt -E _
tata-hi -fuck - ok fuck _you
[[email protected] ~/tmp/dir]#xargs -a t.txt -E -
tata-hi -fuck
-r xargs 預設是空參數comand也要執行一次,如使用-r參數遇到空參數則直接退出,不會再執行一次,避免程式執行錯誤。
[[email protected] ~/tmp/dir]#ls |grep Tata
[[email protected] ~/tmp/dir]#ls |grep Tata |xargs -t
/bin/echo
[[email protected] ~/tmp/dir]#ls |grep Tata |xargs -t -r