Linux tutorial on the xargs command, covering basic and advanced usage with practical examples.
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.
This example demonstrates how to use xargs to pass a list of files to the rm command for deletion.
basic_usage.sh
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.
This example shows how to use xargs with find to delete all .log files in a directory.
xargs_find.sh
find /path/to/dir -name “*.log” | xargs rm
The find command locates all .log files, and xargs passes them to rm for deletion.
This example demonstrates how to handle filenames with spaces using the -0 option.
handle_spaces.sh
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.
This example shows how to limit the number of arguments passed to each command invocation using the -n option.
limit_arguments.sh
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.
xargs can run multiple commands by using the -I option to define a replace string. This allows more complex operations on each input item.
multi_command.sh
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
This example demonstrates how to run commands in parallel using the -P option.
parallel_commands.sh
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.
xargs can prompt before execution using the -p option. This adds safety when running destructive commands by requiring confirmation.
interactive_xargs.sh
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.
This example shows how to use xargs with grep to search for a pattern in multiple files.
xargs_grep.sh
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.
By default xargs uses whitespace as delimiter, but this can be changed with the -d option to handle different input formats.
custom_delim.sh
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
Use -0 for Safety: Always use -0 with find -print0 to handle filenames with spaces.
Limit Arguments: Use -n to control the number of arguments passed to each command.
Parallel Execution: Use -P to speed up tasks by running commands in parallel.
Test Commands: Test xargs commands with echo before executing them.
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.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.