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
awk printf
Viewed 14359 times since Wed, Aug 19, 2020
Display basic information about Physical Volumes
Viewed 3058 times since Sun, Jun 3, 2018
8 Practical Examples of Linux Xargs Command for Beginners
Viewed 4983 times since Fri, Jun 1, 2018
How to sort IP addresses in Linux
Viewed 3429 times since Thu, May 24, 2018
Transform XML to CSV Format | Unix String Pattern Manipulation The Ugly Way
Viewed 4606 times since Sun, Jan 9, 2022
Time conversion using Bash
Viewed 2415 times since Fri, May 25, 2018
Convert a human readable date to epoch with a shell script on OpenBSD and Mac OS X
Viewed 14425 times since Fri, May 25, 2018
Bash: Read File Line By Line – While Read Line Loop
Viewed 1761 times since Mon, Feb 18, 2019
Using Shell Redirection: All About the Here-Doc
Viewed 9988 times since Wed, May 30, 2018
Bash: String Length – How To Find Out
Viewed 1958 times since Mon, Feb 18, 2019