Linux xargs Command
last modified February 25, 2025
The xargs command in Linux is used to build and execute command
lines from standard input. It is particularly useful for processing lists of
items, such as filenames, and passing them as arguments to other commands.
This tutorial covers basic and advanced usage of xargs with
practical examples.
xargs is commonly used in combination with commands like
find, grep, and rm to handle large
numbers of arguments efficiently.
Basic Usage
This example demonstrates how to use xargs to pass a list of files
to the rm command for deletion.
echo "file1.txt file2.txt file3.txt" | xargs rm
The xargs command reads the list of files from standard input and
passes them as arguments to rm.
Using xargs with find
This example shows how to use xargs with find to
delete all .log files in a directory.
find /path/to/dir -name "*.log" | xargs rm
The find command locates all .log files, and
xargs passes them to rm for deletion.
Handling Spaces in Filenames
This example demonstrates how to handle filenames with spaces using the
-0 option.
find /path/to/dir -name "*.log" -print0 | xargs -0 rm
The -print0 option in find and -0 in
xargs ensure that filenames with spaces are handled correctly.
Limiting Arguments per Command
This example shows how to limit the number of arguments passed to each command
invocation using the -n option.
echo "file1.txt file2.txt file3.txt file4.txt" | xargs -n 2 rm
The -n 2 option ensures that rm is called with two
arguments at a time.
Running multiple commands with xargs
xargs can run multiple commands by using the -I option to define a replace string. This allows more complex operations on each input item.
echo "file1 file2 file3" | xargs -n 1 -I {} sh -c 'echo Processing {}; touch {}'
This processes each file individually, first echoing a message then creating the file. The -n 1 ensures one argument at a time is passed to the command.
$ ./multi_command.sh Processing file1 Processing file2 Processing file3
Running Commands in Parallel
This example demonstrates how to run commands in parallel using the
-P option.
echo "file1.txt file2.txt file3.txt" | xargs -n 1 -P 3 gzip
The -P 3 option runs up to three gzip commands in
parallel.
Interactive mode with xargs
xargs can prompt before execution using the -p option. This adds safety when running destructive commands by requiring confirmation.
echo "important_file" | xargs -p rm
The -p option makes xargs ask for confirmation before executing the command. This helps prevent accidental file deletion or other destructive operations.
Combining xargs with grep
This example shows how to use xargs with grep to
search for a pattern in multiple files.
find /path/to/dir -name "*.txt" | xargs grep "search_pattern"
The find command locates all .txt files, and
xargs passes them to grep for searching.
Using xargs with custom delimiters
By default xargs uses whitespace as delimiter, but this can be changed with the -d option to handle different input formats.
echo "file1,file2,file3" | xargs -d , -n 1 echo
This uses comma as delimiter instead of whitespace. Each item is processed separately due to -n 1. Useful for CSV data or custom formats.
$ ./custom_delim.sh file1 file2 file3
Best Practices for xargs
- Use
-0for Safety: Always use-0withfind -print0to handle filenames with spaces. - Limit Arguments: Use
-nto control the number of arguments passed to each command. - Parallel Execution: Use
-Pto speed up tasks by running commands in parallel. - Test Commands: Test
xargscommands withechobefore executing them.
Source
In this article, we have explored various examples of using the xargs
command for efficient command-line processing, including handling spaces,
limiting arguments, and running commands in parallel.
Author
List all Linux tutorials.