文件查找
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 選項限制單個命令行的參數個數。
[cpp] view plain copy
[[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 指定輸入結束符
[cpp] view plain copy
[[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參數遇到空參數則直接退出,不會再執行一次,避免程序執行錯誤。
[cpp] view plain copy
[[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 |grep Tata
[[email protected] ~/tmp/dir]#ls |grep Tata |xargs -t
/bin/echo
[[email protected] ~/tmp/dir]#ls |grep Tata |xargs -t -r
[[email protected] ~/tmp/dir]#