Never Ending Security

It starts all here

Xargs Commands Cheatsheet

Xargs Command Examples

xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Commands like grep and awk can accept the standard input as a parameter, or argument by using a pipe. However, others like cp and echo disregard the standard input stream and rely solely on the arguments found after the command. xargs breaks the list of arguments into sublists small enough to be acceptable.

xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.


xargs [-0prtx] [-E eof-str] [-e[eof-str]] [–eof[=eof-str]] [–null] [-d delimiter] [–delimiter delimiter] [-I replace-str] [-i[replace-str]] [–replace[=replace-str]] [-l[max-lines]] [-L max-lines] [–max-lines[=max-lines]] [-n max-args] [–max-args=max-args] [-s max-chars] [–max-chars=max-chars] [-P max-procs] [–max-procs=max-procs] [–interactive] [–verbose] [–exit] [–no-run-if-empty] [–arg-file=file] [–show-limits] [–version] [–help] [command [initial-arguments]]

Xargs Basic Example

The xargs command (by default) expects the input from stdin, and executes /bin/echo command over the input. The following is what happens when you execute xargs without any argument, or when you execute it without combining with any other commands.

When you type xargs without any argument, it will prompt you to enter the input through stdin:

$ xargs
This is a Test

After you type something, press ctrl+d, which will echo the string back to you on stdout as shown below.

$ xargs
This is a Test.Hey, This is a Test.

Specify Delimiter Using -d option

Delimiters can be applied so that each character in the input is taken literally using -d option in xargs.

In the previous example, even though the input contained a \n (newline)  the echo output did not contain the newline ‘\n’ . Using the -d\n, it will preserve newline delimiter in the output, and display the output exactly as it was typed.

$ xargs -d\n
This is a Test.

This time xargs will preserve the newline.

$ xargs -d\n
This is a Test.Hey, 
This is a Test.

Limit Output Per Line Using -n Option

By default xargs displays whatever comes to its stdin.

$ echo a b c d e f| xargs
a b c d e f

But, the output of the xargs command can be split into multiple lines using -n option.

$ echo a b c d e f| xargs -n 3
a b c
d e f

In the same way, you can also split the output with 2 items per line using -n 2.

$ echo a b c d e f| xargs -n 2
a b
c d
e f

Prompt User Before Execution using -p option

Using option -p, you can confirm the execution of the xargs command from the user.

If we want to confirm each execution of the /bin/echo command by the user, use the -p option along with -n option as shown below.

$ echo a b c d e f| xargs -p -n 3
/bin/echo a b c ?...y
/bin/echo d e f ?...a b c
d e f


$ echo a b c d e f| xargs -p -n 3
/bin/echo a b c ?...n
/bin/echo d e f ?...n
/bin/echo ?...n

Note: This is helpful when you are combining xargs with commands that are disruptive like rm.

Avoid Default /bin/echo for Blank Input Using -r Option

When there is a blank input (i.e no input was given to xargs command), it will execute a /bin/echo command which will display a new line.

$ xargs -p

Press ctrl-d after typing “xargs -p”, which will indicate that it executed a /bin/echo as shown below.

$ xargs -p
                      /bin/echo ?...y


Print the Command Along with Output Using -t Option

In the following example, type “abcd” as the input for the xargs -t command.

$ xargs -t

Press ctrl-d to complete the above xargs -t command, which will display the command that xargs really executes before displaying the output. In this case, the command that xargs executes is “/bin/echo abcd”, which is displayed here.

$ xargs -t
/bin/echo abcd

Combine Xargs with Find Command

This is one of the most important usage of xargs command. When you need to find certain type of files and perform certain actions on them.

The xargs command is very effective when we combine with other commands.

In the following example, we took the output of the find command, and passed it as input to the xargs command. But, instead of executing the default /bin/echo command, we are instructing xargs command to execute the rm -rm command on the input.

So, in this example, the output of the find command is all the files with *.c extension, which is given as input to the xargs command, which in-turn execute “rm -rf” command on all the *.c files.

$ ls
one.c  one.h  two.c  two.h

$ find . -name "*.c" | xargs rm -rf

$ ls
one.h  two.h

Delete Files that has White-space in the Filename

So we see that despite of running the rm command on the .c files in this directory, the file ‘New File.c’ was not deleted. This is because this file contains white space characters in its name.

$ touch "New File.c"

$ ls
one.c  one.h  two.c  two.h New File.c

$ find . -name "*.c" | xargs rm -rf

$ ls
one.h  two.h  New File.c

In this situation, use the -print0 option with find command and -0 option with xargs command to delete files including those that has space in the filenames as shown below.

$ ls
one.c  one.h  two.c  two.h New File.c

$ find . -name "*.c" -print0 | xargs -0 rm -rf

$ ls
one.h  two.h

Display System Limits on xargs using –show-limits option

The following example displays all the limits set by the OS that will have an impact on the way how xargs command works.

$ xargs --show-limits
Your environment variables take up 1203 bytes
POSIX upper limit on argument length (this system): 2093901
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2092698
Size of command buffer we are actually using: 131072

Execution of xargs will continue now, and it will try to read its input and 
run commands; if this is not what you wanted to happen, please type the 
end-of-file keystroke.

Warning: /bin/echo will be run at least once.  If you do not want that to happen, 
then press the interrupt keystroke

Combine Xargs with Grep command

The xargs command can be combined with grep command to filter particular files from the search results of the find command.

In the following example, find command provided all the .c files as input to xargs.

The xargs command executes the grep command to find all the files (among the files provided by find command) that contained a string ‘stdlib.h’.

$ find . -name '*.c' | xargs grep 'stdlib.h'

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s