Xargs
From DssWiki
{{DISPLAYTITLE:{{#if:|:|}}xargs}}
xargs is a command of the Unix and most Unix-like operating systems which eases passing command output to another command as command line arguments.
It splits its often piped input at whitespaces (or the null character) and calls the command given as an argument with each element of the split input as parameter. If the parameter list is too long, it calls the program as often as necessary. It often covers the same functionality as the backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input.
This is often used in conjunction with the Unix commands find, locate and grep.Template:Fact
[edit] Examples
find . -name "*.foo" | xargs grep bar
in practice does the same as
grep bar `find . -name "*.foo"`
but will work even if there are so many files to search that they will not all fit on a single command line. Note that the preceding command uses backticks (`), not single quotes ('). It searches in all files in the current directory and its subdirectories which end in .foo for occurrences of the string bar.
find . -name "*.foo" -print0 | xargs -0 grep bar
...does the same thing, but uses GNU specific extensions to find and xargs to separate filenames using the null character; this will work even if there are whitespace characters, including newlines, in the filenames.
find . -name "*.foo" -print0 | xargs -0 -t -r vi
is similar to above, but launches the vi editor for each of the files. The -t prints the command to stderr before issuing it. The -r is a GNU extension that tells xargs not to run the command if no input was received.
find . -name "*.foo" -print0 | xargs -0 -i mv {} /tmp/trash
use -i to tell xargs to replace {} with the argument list
[edit] External links
{{#if: |
Unix command line programs and builtins (more) | |||
| File and file system management: | cat | chattr | cd | chmod | chown | chgrp | cksum | cmp | cp | du | df | file | fsck | fuser | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
| Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
| User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
| Text processing: | awk | comm | cut | ed | ex | fmt | head | iconv | join | less | more | paste | pg | sed | sort | tac | tail | tr | uniq | wc | xargs | ||
| Shell programming: | basename | echo | expr | false | printf | test | true | unset | Printing: | lp |
| Communications: inetd | netstat | ping | rlogin | traceroute | Searching: find | grep | strings | Miscellaneous: banner | bc | cal | dd | man | size | yes | |
