Xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times
with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
-p Prompt the user about whether to run each command line and read a line from the terminal.
-I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.
Examples:
List all *.dat files in the current directory and zip each file.
ls *.dat | xargs -I {} zip ./{}.zip {}
Generates a compact listing of all the users on the system.
-p Prompt the user about whether to run each command line and read a line from the terminal.
-I replace-str Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character.
Examples:
List all *.dat files in the current directory and zip each file.
ls *.dat | xargs -I {} zip ./{}.zip {}
Generates a compact listing of all the users on the system.
cut -d: -f1 < /etc/passwd | sort | xargs echo
找出所有文件中行数最多的:
ls | xargs -I {} wc -l {} | sort -n | tail -1
本文介绍了Xargs命令的基本用法及其如何从标准输入读取数据并执行指定命令。通过几个实用示例展示了如何利用Xargs来提高工作效率,如批量压缩文件、整理用户列表以及查找文件中行数最多的内容。
350

被折叠的 条评论
为什么被折叠?



