🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

xargs Command

Intermediate Text Processing man(1)

Build and execute command lines from standard input

👁 12 views 📅 Updated: Mar 15, 2026
SYNTAX
xargs [OPTION]... [COMMAND]

What Does xargs Do?

xargs builds and executes command lines from standard input. It reads items from stdin and passes them as arguments to a specified command, enabling you to use output from one command as arguments for another.

xargs solves the common problem of passing too many arguments to a command (argument list too long error) by splitting input into manageable batches. It is essential for connecting find, grep, and other listing commands with action commands like rm, cp, chmod, and more.

xargs supports parallel execution, custom delimiters, confirmation prompts, and can handle filenames with spaces and special characters when used with -0 flag and null-terminated input.

Options & Flags

OptionDescriptionExample
-I {} Replace {} with each input item echo 'file1 file2' | xargs -I {} cp {} /backup/
-0 Input items are null-terminated (use with find -print0) find . -name '*.log' -print0 | xargs -0 rm
-n Use at most N arguments per command invocation echo 1 2 3 4 5 | xargs -n 2 echo
-P Run up to N processes in parallel find . -name '*.png' | xargs -P 4 -I {} convert {} -resize 50% {}
-p Prompt before execution (interactive) find /tmp -name '*.tmp' | xargs -p rm
-t Print command before executing echo *.txt | xargs -t wc -l
-L Use N lines per command invocation cat urls.txt | xargs -L 1 curl -O

Practical Examples

#1 Delete files found by find

Safely deletes files with special characters in names using null-terminated input.
$ find /tmp -name '*.tmp' -print0 | xargs -0 rm -f

#2 Count lines in all PHP files

Passes all PHP file paths as arguments to wc for line counting.
$ find src/ -name '*.php' | xargs wc -l

#3 Replace placeholder

Renames each .bak file by appending .old using placeholder replacement.
$ find . -name '*.bak' | xargs -I {} mv {} {}.old

#4 Parallel image processing

Processes 8 images simultaneously for faster batch conversion.
$ find photos/ -name '*.jpg' | xargs -P 8 -I {} convert {} -resize 800x600 resized/{}

#5 Batch chmod

Makes all .sh files executable in one efficient operation.
$ find . -name '*.sh' | xargs chmod +x

#6 Download multiple URLs

Downloads URLs from a file, one per line, with 4 parallel downloads.
$ cat urls.txt | xargs -L 1 -P 4 wget -q

#7 Search in found files

Searches for password in all .conf files found by find.
$ find . -name '*.conf' | xargs grep -l 'password'

Tips & Best Practices

Always use -0 with find: Use find -print0 | xargs -0 to handle filenames with spaces, quotes, and newlines safely. Without -0, filenames with spaces break.
Parallel execution: -P N runs N processes in parallel. Use -P $(nproc) to use all CPU cores. Great for batch image/video processing.
xargs vs find -exec: xargs is faster than find -exec because it batches arguments. find -exec {} + also batches but xargs offers more features like parallel execution.

Frequently Asked Questions

How do I use xargs with find?
Use find /path -name 'pattern' -print0 | xargs -0 command. The -print0 and -0 flags handle filenames with spaces and special characters safely.
How do I run xargs commands in parallel?
Add -P N to run N processes in parallel: find . -name '*.png' | xargs -P 4 -I {} convert {} output/{}. Use -P 0 for unlimited parallelism.
What does -I {} mean in xargs?
-I {} tells xargs to replace {} with each input item. This lets you place the argument anywhere in the command: xargs -I {} cp {} /backup/

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →