Skip to content
Log In Sign Up info@muvcloud.com

The xargs Commands You Will Use Most

find /var/www -name "*.php" -print0 | xargs -0 grep -l "base64_decode"
find /var/log/app -name "*.log" -mtime +7 -print0 | xargs -0 -r gzip
cat hosts.txt | xargs -n 1 ping -c 1
find /var/backups -name "*.tar" -print0 | xargs -0 -P 4 -n 1 sha256sum

Each line reads names from the command on the left and passes them to the command after xargs. The examples search PHP files for a string, compress old logs, ping each host in a list once, and compute checksums of archives with four jobs in parallel.

What xargs Does

echo "a.txt b.txt c.txt" | xargs ls -l

xargs turns the words it reads into arguments, so this runs ls -l a.txt b.txt c.txt. It puts as many items as fit on one command line and runs the command again for the rest, which avoids the "Argument list too long" error you get from a shell pattern that matches too many files. Without a command, xargs runs echo.

Names with Spaces: -0 and -print0

By default, xargs splits its input on spaces and line breaks, and it treats quotes and backslashes as special. A file named my report.pdf becomes two arguments, my and report.pdf, and the command fails or, worse, acts on a different file. The fix is to separate names with a null character, which cannot occur in a file name:

find /srv/files -type f -print0 | xargs -0 ls -l

-print0 makes find end each name with a null character, and -0 makes xargs split only on that character. Use the pair every time the input is a list of file names.

Control How Items Are Passed

OptionWhat it does
-n 1One item per command
-I {}Puts the item where {} is
-rRuns nothing on empty input
-tPrints each command first
-pAsks before each command
find . -name "*.conf" -print0 | xargs -0 -I {} cp {} {}.bak

-I {} runs the command once per input line (once per name with -0) and replaces every {} with it, which you need when the name is not the last argument. GNU xargs runs the command once even with empty input unless you add -r.

Check Before You Change Anything

Before any xargs command that deletes or overwrites, run the left side alone and read the list, or put echo in front of the command so xargs prints what it would run:

find /tmp/app -type f -mtime +7 -print
find /tmp/app -type f -mtime +7 -print0 | xargs -0 -r echo rm
find /tmp/app -type f -mtime +7 -print0 | xargs -0 -r rm

-p goes further and asks for confirmation before each command. A deleted file on a server is gone; only a backup or snapshot brings it back.

Run Jobs in Parallel

find /var/backups -name "*.sql" -print0 | xargs -0 -P 4 -n 1 gzip

-P 4 runs up to four commands at the same time, here compressing four dumps at once. Pick a number no higher than the CPU cores, which nproc shows, and use -n 1 or -n with a small number so the work is spread across the jobs. Output of parallel jobs can mix on the screen.

xargs or find -exec?

For running a command on files that find selects, find ... -exec command {} + does the same batching as xargs, handles any file name safely, and needs no pipe; it is described in the find command guide. xargs is the better choice when you need parallel jobs with -P, or when the list comes from something other than find, such as a text file or the output of awk. To move many files into one folder, combine xargs with mv -t, as shown in the mv command guide.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations