Linux xargs Command

25 Practical Examples with Explanations and Output

Introduction to the xargs Command

The xargs command in Linux is used to build and execute command lines from standard input. It's particularly useful when you want to pass a list of items to a command that doesn't accept piped input directly.

Tip: xargs is often used in combination with find, grep, and other commands that generate lists of items.

Example 1: Basic Usage

Pass a list of files to ls -l for detailed listing.

$ echo "file1.txt file2.txt file3.txt" | xargs ls -l
-rw-r--r-- 1 user user 0 Jan 10 10:00 file1.txt -rw-r--r-- 1 user user 0 Jan 10 10:00 file2.txt -rw-r--r-- 1 user user 0 Jan 10 10:00 file3.txt

Example 2: Using find with xargs

Find all .txt files and pass them to rm for deletion.

$ find . -name "*.txt" -type f | xargs rm
# All .txt files are deleted (no output on success)

Example 3: Handling Files with Spaces

Use -print0 with find and -0 with xargs to handle filenames with spaces.

$ find . -name "*.txt" -type f -print0 | xargs -0 rm
# Files with spaces in names are properly handled

Example 4: Confirmation Before Execution

Use -p to prompt for confirmation before executing each command.

$ echo "file1.txt file2.txt" | xargs -p rm
rm file1.txt file2.txt ?...y # Files are removed after confirmation

Example 5: Limiting Arguments per Command

Use -n to specify the maximum number of arguments per command.

$ echo "1 2 3 4 5" | xargs -n 2 echo "Number:"
Number: 1 2 Number: 3 4 Number: 5

Example 6: Running Commands in Parallel

Use -P to run multiple commands in parallel.

$ echo "1 2 3 4" | xargs -n 1 -P 4 sleep
# Four sleep commands run in parallel

Example 7: Using Placeholder with -I

Use -I to specify a placeholder replacement string.

$ find . -name "*.bak" | xargs -I {} mv {} /backup/
# All .bak files are moved to /backup/ directory

Example 8: Counting Lines in Multiple Files

Count lines in all text files using wc -l.

$ find . -name "*.txt" | xargs wc -l
10 file1.txt 25 file2.txt 15 file3.txt 50 total

Example 9: Searching in Multiple Files

Search for a pattern in multiple files using grep.

$ find . -name "*.py" | xargs grep "import os"
script1.py:import os script2.py:import os utils.py:import os

Example 10: Creating Archive with Found Files

Find files and add them to a tar archive.

$ find . -name "*.log" -type f | xargs tar -czvf logs.tar.gz
./app.log ./system.log ./error.log

Example 11: Changing File Permissions

Change permissions of multiple files at once.

$ find . -name "*.sh" -type f | xargs chmod +x
# All .sh files become executable

Example 12: Removing Files Older Than 30 Days

Find and remove files older than 30 days.

$ find /tmp -type f -mtime +30 | xargs rm -f
# Files older than 30 days in /tmp are removed

Example 13: Copying Files to Multiple Destinations

Copy a file to multiple directories.

$ echo "/backup /archive /storage" | xargs -n 1 cp important.txt
# important.txt is copied to all three directories

Example 14: Downloading Multiple URLs

Download multiple URLs using wget.

$ cat urls.txt | xargs -n 1 wget
--2023-01-10 10:00:00-- http://example.com/file1.zip Saving to: 'file1.zip' --2023-01-10 10:00:01-- http://example.com/file2.zip Saving to: 'file2.zip'

Example 15: Processing CSV Data

Process comma-separated values with custom delimiter.

$ echo "apple,banana,cherry" | xargs -d, -n 1 echo "Fruit:"
Fruit: apple Fruit: banana Fruit: cherry

Example 16: Creating Directories from a List

Create multiple directories from a list.

$ echo "dir1 dir2 dir3" | xargs mkdir
# Directories dir1, dir2, dir3 are created

Example 17: Removing Directories

Remove multiple directories.

$ echo "dir1 dir2 dir3" | xargs rmdir
# Directories dir1, dir2, dir3 are removed

Example 18: Converting Images

Convert multiple images to a different format.

$ find . -name "*.png" | xargs -I {} convert {} {}.jpg
# All PNG files are converted to JPG format

Example 19: Running Custom Script on Multiple Files

Execute a custom script on each file found.

$ find . -name "*.data" | xargs -I {} ./process.sh {}
Processing file1.data... Done Processing file2.data... Done Processing file3.data... Done

Example 20: Showing Command Before Execution

Use -t to show the command before executing it.

$ echo "file1.txt file2.txt" | xargs -t rm
rm file1.txt file2.txt # Files are removed after command is displayed

Example 21: Using xargs with awk

Process each line with awk using xargs.

$ echo -e "1 2 3\n4 5 6" | xargs -L 1 awk '{print $1 + $2 + $3}'
6 15

Example 22: Processing Files with Spaces in Names

Handle files with spaces using find and xargs with null separator.

$ find . -name "*.doc" -type f -print0 | xargs -0 -I {} mv "{}" ~/Documents/
# All .doc files, even with spaces, are moved to Documents

Example 23: Using xargs with sed

Replace text in multiple files using sed.

$ find . -name "*.html" | xargs sed -i 's/old-text/new-text/g'
# All occurrences of 'old-text' are replaced with 'new-text' in HTML files

Example 24: Running Commands with Maximum Processes

Limit the number of parallel processes with -P.

$ echo {1..10} | xargs -n 1 -P 3 sleep
# 10 sleep commands run with maximum 3 processes at a time

Example 25: Combining find, grep and xargs

Find files containing a specific pattern and copy them to a directory.

$ find . -name "*.py" -exec grep -l "important_function" {} \; | xargs cp -t /important_scripts/
# All Python files containing 'important_function' are copied to /important_scripts/

Important Notes About xargs

1. Always be cautious when using xargs with commands like rm, as it can accidentally delete important files.

2. Use -print0 with find and -0 with xargs when dealing with filenames that contain spaces or special characters.

3. The -p option is useful for dry runs and confirmation before executing commands.

4. Use -n to control how many arguments are passed to each command invocation.

5. The -I option allows you to specify a replacement string for more complex commands.