Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. xargs

xargs

xargs is a command that takes values from standard input and passes them as arguments to another command. Combined with pipes, it lets you feed file lists or search results into another command all at once.

Syntax

Pass standard input as arguments to a command.

command | xargs target-command

Use the -I option to insert input values into a placeholder {}.

command | xargs -I{} target-command {}

Use the -n option to limit the number of arguments passed at once.

command | xargs -n count target-command

Use the -P option to run in parallel.

command | xargs -P num-processes -I{} target-command {}

Handle filenames with spaces or newlines safely (-0).

find . -name "*.txt" -print0 | xargs -0 target-command

Options

OptionDescription
-I{}Replaces {} with each input value. You can use any symbol as the placeholder.
-n countSets the maximum number of arguments passed to each command invocation.
-P countSpecifies the number of processes to run in parallel.
-0Reads input delimited by null characters. Use with find -print0.
-tPrints each command before executing it. Useful for debugging.
-pPrompts for confirmation before each command is executed.
-rDoes not run the command if the input is empty (GNU xargs only).
-L linesPasses the specified number of input lines as arguments per invocation.

Sample Code

The examples below use this directory structure.

📁 ~/project/ 📁 src/ 📄 index.php (contains "TODO") 📄 utils.php (contains "TODO") 📁 logs/ 📄 app.log 📄 error.log 📄 data1.txt 📄 data2.txt 📄 data3.txt

Pass the results of find to another command via xargs. This example deletes all .log files.

find . -name "*.log" | xargs rm

If file paths may contain spaces or special characters, use -print0 with -0.

find . -name "*.log" -print0 | xargs -0 rm

Use -I{} with a placeholder to rename files one by one.

ls *.txt | xargs -I{} mv {} {}.bak

Combine find and grep to search across multiple files.

find . -name "*.php" | xargs grep -l "TODO"
./src/index.php
./src/utils.php

Use -n N to limit how many arguments are passed at once.

echo "a b c d e f" | xargs -n 2 echo
a b
c d
e f

Use -t to print each command before it runs. This is handy for debugging.

echo "file1 file2 file3" | xargs -t -n 1 echo
echo file1
file1
echo file2
file2
echo file3
file3

Use -P N to process files N at a time in parallel. This example compresses files using 4 parallel processes.

ls *.csv | xargs -P 4 -I{} gzip {}

This example downloads URLs from a list using 5 parallel processes.

cat urls.txt | xargs -P 5 -I{} wget -q {}

Notes

When file paths may contain spaces or special characters, find -print0 | xargs -0 is essential. Using null characters as delimiters lets you handle any filename safely.

The -P option makes full use of your CPU by running tasks in parallel. It is especially effective for independent tasks such as compressing, converting, or downloading multiple files. For more on pipes and redirection, see Pipe (|).

If you find any errors or copyright issues, please .