Linux and Unix xargs command tutorial with examples

Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.

Estimated reading time: 3 minutes

Table of contents

cut man page

What is the xargs command in UNIX?

The xargs command in UNIX is a command line utility for building an execution pipeline from standard input. Whilst tools like grep can accept standard input as a parameter, many other tools cannot. Using xargs allows tools like echo and rm and mkdir to accept standard input as arguments.

How to use xargs

By default xargs reads items from standard input as separated by blanks and executes a command once for each argument. In the following example standard input is piped to xargs and the mkdir command is run for each argument, creating three folders.

echo 'one two three' | xargs mkdir
ls
one two three

How to use xargs with find

The most common usage of xargs is to use it with the find command. This uses find to search for files or directories and then uses xargs to operate on the results. Typical examples of this are removing files, changing the ownership of files or moving files.

find and xargs can be used together to operate on files that match certain attributes. In the following example files older than two weeks in the temp folder are found and then piped to the xargs command which runs the rm command on each file and removes them.

find /tmp -mtime +14 | xargs rm

xargs v exec {}

The find command supports the -exec option that allows arbitrary commands to be found on files that are found. The following are equivalent.

find ./foo -type f -name "*.txt" -exec rm {} \; 
find ./foo -type f -name "*.txt" | xargs rm

So which one is faster? Let’s compare a folder with 1000 files in it.

time find . -type f -name "*.txt" -exec rm {} \;
0.35s user 0.11s system 99% cpu 0.467 total

time find ./foo -type f -name "*.txt" | xargs rm
0.00s user 0.01s system 75% cpu 0.016 total

Clearly using xargs is far more efficient. In fact several benchmarks suggest using xargs over exec {} is six times more efficient.

How to print commands that are executed

The -t option prints each command that will be executed to the terminal. This can be helpful when debugging scripts.

echo 'one two three' | xargs -t rm
rm one two three

How to view the command and prompt for execution

The -p command will print the command to be executed and prompt the user to run it. This can be useful for destructive operations where you really want to be sure on the command to be run. l

echo 'one two three' | xargs -p touch
touch one two three ?...

How to run multiple commands with xargs

It is possible to run multiple commands with xargs by using the -I flag. This replaces occurrences of the argument with the argument passed to xargs. The following prints echos a string and creates a folder.

cat foo.txt
one
two
three

cat foo.txt | xargs -I % sh -c 'echo %; mkdir %'
one 
two
three

ls 
one two three

Further reading

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
10 Awk Tips, Tricks and Pitfalls
Viewed 6097 times since Wed, Aug 19, 2020
“Yes/No” in Bash Script – Prompt for Confirmation
Viewed 33142 times since Mon, Feb 18, 2019
30 Examples for Awk Command in Text Processing
Viewed 15505 times since Sat, Nov 24, 2018
Tunneling With SSH to Your Destination
Viewed 4587 times since Wed, May 30, 2018
Linux: Repeat Command N Times – Bash FOR Loop
Viewed 3172 times since Mon, Feb 18, 2019
Epoch & Unix Timestamp Conversion Tools
Viewed 63262 times since Fri, Jun 22, 2018
View Config Files Without Comments
Viewed 2434 times since Mon, May 21, 2018
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
Viewed 5024 times since Thu, Feb 11, 2021
Linux / UNIX: Convert Epoch Seconds To the Current Time
Viewed 3163 times since Fri, May 25, 2018
Convert a human readable date to epoch with a shell script on OpenBSD and Mac OS X
Viewed 15316 times since Fri, May 25, 2018