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
Tunneling With SSH to Your Destination
Viewed 4521 times since Wed, May 30, 2018
Transform XML to CSV Format | Unix String Pattern Manipulation The Ugly Way
Viewed 5741 times since Sun, Jan 9, 2022
Utilities to Tidy Up Your Reports
Viewed 16355 times since Wed, May 30, 2018
30 Examples for Awk Command in Text Processing
Viewed 15409 times since Sat, Nov 24, 2018
perl podstawy
Viewed 2105 times since Mon, May 21, 2018
Linux: Repeat Command N Times – Bash FOR Loop
Viewed 3110 times since Mon, Feb 18, 2019
Bash: Read File Line By Line – While Read Line Loop
Viewed 2320 times since Mon, Feb 18, 2019
How to get a password from a shell script without echoing - solutions
Viewed 13934 times since Fri, Feb 22, 2019
10 Xargs Command Examples in Linux / UNIX
Viewed 4020 times since Fri, Jun 1, 2018
8 Practical Examples of Linux Xargs Command for Beginners
Viewed 5607 times since Fri, Jun 1, 2018