分类: LINUX
2013-01-07 23:01:55
xargs [options] [command]
Execute command (with any initial arguments), but read remaining arguments from standard input instead of specifying them directly. xargs passes these arguments in several bundles to command, allowing command to process more arguments than it could normally handle at once. The arguments are typically a long list of filenames (generated by ls or find, for example) that get passed to xargs via a pipe.
Options
-0, --null # Expect filenames to be terminated by NULL instead of whitespace. Do not treat quotes or backslashes specially.
-e[string] , -E [string] , --eof[=string] # Set EOF to _ or, if specified, to string.
--help # Print a summary of the options to xargs and then exit.
-i[string] , -I [string] , --replace[=string] # Replace all occurrences of { }, or string, with the names read from standard input. Unquoted blanks are not considered argument terminators. Implies -x and -L 1.
-l[lines] , -L [lines] , --max-lines[=lines] # Allow no more than lines nonblank input lines on the command line (default is 1). Implies -x.
-n args, --max-args=args # Allow no more than args arguments on the command line. Overridden by the maximum number of characters set with -s.
-p, --interactive # Prompt for confirmation before running each command line. Implies -t.
-P max, --max-procs=max # Allow no more than max processes to run at once. The default is 1. A maximum of 0 allows as many as possible to run at once.
-r, --no-run-if-empty # Do not run command if standard input contains only blanks.
-s max, --max-chars=max # Allow no more than max characters per command line.
-t, --verbose # Verbose mode. Print command line on standard error before executing.
-x, --exit # If the maximum size (as specified by -s) is exceeded, exit.
--version # Print the version number of xargs and then exit.
使用xargs的场景:
find命令的-exec选项处理匹配到的文件时,将所有匹配到的文件一起传递给exec执行, 但系统对exec要执行的命令参数长度有限制,在find命令运行一会后,就会出现溢出错误(“参数列太长”);另外一些系统中,-exec为每一个匹配到的文件开启一个处理进程,此种情况下,进程过多,系统性能下降,效率不高
find命令把匹配到的文件传递给xargs命令,xargs命令每次只获取一部分文件而不是全部(根据该命令的选项及系统内核中相应的可调参数来确定),先处理最先获取的一部分文件,然后是下一批,循环直至全部处理完成;xargs命令只有一个进程, 性能消耗少
examples:
find . -type f -print | xargs file
find / -name "core" -print | xargs echo "" > /tmp/core.log
find . -perm -7 -print | xargs chmod o-w
find . -type f -print | xargs grep "hostname"
find . -name \* -type f -print | xargs grep "hostnames"
# copy all files in current directory to directory specified on command-line
ls | xargs -i -t cp ./{} dest_dir
# -t is "verbose" (output command-line to stderr) option
# -i is "replace string" option
# {} is a placeholder for output text
# killing processes by name
pa ax | grep mysql | awk '{print $1}' | xrags -i kill {} 2&>/dev/null
# word frequency analysis
cat data-file | tr 'A-Z' 'a-z' | sed -e 's/\.//g' -e 's/\,//g' -e 's/ //g' | sort | uniq -c | sort -nr
xargs -n1 # List the file, one word per line
# gzips every file in current directory, one at a time, prompting before each operation
ls | xargs -p -l gzip
# in combination with find -print0 or grep -lZ. This allows handling arguments containing whitespace or quotes
xargs -0
find . -type f -print -exec grep "hello worlf" {} \;
find . -type f -print | xargs grep "hello worlf"