Xargs
跳至導覽
跳至搜尋
package.lua第80行Lua錯誤:module 'Module:CGroup/core' not found Template:Lowercase xargs是一條Unix和類Unix操作系統的常用命令。它的作用是將參數列錶轉換成小塊分段傳遞給其他命令,以避免參數列表過長的問題[1]。xargs的作用一般等同於大多數Unix shell中的反引號,但更加靈活易用,並可以正確處理輸入中有空格等特殊字符的情況。對於經常產生大量輸出的命令如find、locate和grep來說非常有用。
示例[編輯]
例如,下面的命令:
rm $(find /path -type f)
如果path目錄下文件過多就會因為「參數列表過長」而報錯無法執行。但改用xargs以後,問題即獲解決。
find /path -type f -print0 | xargs -0 rm
本例中xargs將find產生的長串文件列表拆散成多個子串,然後對每個子串調用rm。-print0表示輸出以null分隔(-print使用換行);-0表示輸入以null分隔。這樣要比如下使用find命令效率高的多。
find /path -type f -exec rm '{}' \;
上面這條命令會對每個文件調用"rm"命令。當然使用新版的"find"也可以得到和"xargs"命令同樣的效果:
find /path -type f -exec rm '{}' +
find . -name "*.foo" | xargs grep bar
該命令大體等價於
grep bar $(find . -name "*.foo")
find . -name "*.foo" -print0 | xargs -0 grep bar
使用了GNU特殊規定的空字符。
find . -name "*.foo" -print0 | xargs -0 -t -r vi
與上面的基本相同但啟動vi進行編輯。-t參數會提前打印錯誤信息。-r參數是一個GNU擴展,表明在無輸入情況下則不構造命令執行。
find . -name "*.foo" -print0 | xargs -0 -i mv {} /tmp/trash
使用-i參數將{}中內容替換為列表中的內容。
參見[編輯]
參考[編輯]
- ↑ package.lua第80行Lua錯誤:module 'Module:Citation/CS1/People' not found
外部鏈接[編輯]
- : construct argument lists and invoke utility – 命令與工具(Commands & Utilities)參考,單一UNIX®規範第7期,由國際開放標準組織發布
手冊頁[編輯]
- – GNU Findutils參考
- : construct argument list(s) and execute utility – FreeBSD通用命令(General Commands)手冊頁
- : construct argument list(s) and execute utility – NetBSD通用命令(General Commands)手冊頁
- : construct argument list(s) and execute utility – OpenBSD通用命令(General Commands)手冊頁
- : construct argument lists and invoke utility – Solaris 10用戶命令(User Commands)參考手冊頁
package.lua第80行Lua錯誤:module 'Module:Navbar/configuration' not found