xargs 

Send to Kindle
home » snippets » linux » xargs



Flags

Flag Description
-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.  Implies -x and -L 1.
-L max-lines Use at most max-lines non-blank input lines per command line.  Trailing blanks cause an input line to be logically continued on the next input line.  Implies -x.
-P maxprocs Parallel mode: run at most maxprocs invocations of utility at once.
-n num Max number of args to pass per invocation of the utility.  Default is 5000.
-o Reopen stdin as /dev/tty in the child process before executing the command (e.g. if you're running an interactive command.)
-p Confirm each command to be executed with a prompt.  No commands are executed if a terminal isn't attached.  See also -t.
-r,--no-run-if-empty GNU extension: If the standard input does not contain any non-blanks, do not run the command.  Normally, the command is run once even if there is no input.
-s Specify (increase) the amount of buffer space that xargs uses to buffer lines.  See the Buffer Size section below.
-t Echo the command to be executed to stderr immediately before it is executed.  See also -p.
x. --exit Exit if the size (see the -s option) is exceeded.

Buffer Size

When you use the -I option, each line read from the input is buffered internally.  This means that there is an upper limit on the length of input line that xargs will accept when used with the -I option.  To work around this limitation, you can use the -s option to increase the amount of buffer space that xargs uses, and you can also use an extra invocation of xargs to ensure that very long lines do not occur.  For example:

somecommand | xargs -s 50000 echo | xargs -I '{}' -s 100000 rm '{}'

Here, the first invocation of xargs has no input line length limit because it doesn't use the -I option.  It splits up the input (which might be a super long line) into lines about 50000 in length via the use of echo.  The second invocation of xargs does have such a limit, but we have ensured that the it never encounters a line which is longer than it can handle. 

The problem doesn't occur with the output of find because it emits just one filename per line. 

Snippets

# Delete old log files.
find . -maxdepth 1 -iname '*.log' -ctime +10 -mtime +2 -print0 | xargs -0 rm

# -n 1 == only pass 1 param to the command – in this case rm
# -P 8 == run up to 8 invocations of the process in parallel.
find . -maxdepth 1 -iname '*.log' -ctime +10 -mtime +2 -print0 | xargs -0 -n 1 -P 8 rm

# Copy my .zsh* files to /tmp before regenerating them.
ls .zsh*(.) | grep -v history | xargs '-I{}' cp '{}' /tmp