Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Practical Examples of Linux xargs Command
The Linux xargs command is a powerful utility that reads items from standard input and executes commands using those items as arguments. It's particularly useful for automating tasks and processing large numbers of files efficiently. This command helps bridge the gap between commands that produce output and commands that need arguments.
The xargs command is especially valuable when working with long lists of items or data, as it simplifies the process of executing commands on multiple items. With xargs, you can perform operations like copy, delete, compress, rename, and count on numerous files with a single command line.
How xargs Works
Copy Files to a New Directory
To copy multiple files with a specific pattern to a new directory, use xargs with the cp command.
ls *.txt | xargs -I '{}' cp '{}' /path/to/new_directory/
This command lists all .txt files and copies each one to the specified directory. The -I '{}' option creates a placeholder that gets replaced with each filename.
Delete Files with Specific Extension
Remove all files with a particular extension using xargs with the rm command.
find . -name "*.log" | xargs rm
Deleted: error.log Deleted: access.log Deleted: debug.log
This safer approach uses find instead of ls to locate .log files and removes them. The find command handles filenames with spaces better than ls.
Compress Multiple Files
Create an archive containing multiple files using xargs with the tar command.
find . -name "*.txt" | xargs tar -czvf documents.tar.gz
a file1.txt a file2.txt a file3.txt
This command finds all .txt files and compresses them into a single tar.gz archive. The tar options create (-c), compress with gzip (-z), use verbose output (-v), and specify the filename (-f).
Rename Files to Uppercase
Convert filenames to uppercase using xargs with shell commands.
ls *.txt | xargs -I '{}' sh -c 'mv "{}" $(echo "{}" | tr "[:lower:]" "[:upper:]")'
This complex command renames each .txt file to uppercase by using the tr command to translate lowercase letters to uppercase within a shell subcommand.
Count Lines in Multiple Files
Get line counts for multiple files using xargs with the wc command.
find . -name "*.txt" | xargs wc -l
12 ./file1.txt
24 ./file2.txt
6 ./file3.txt
42 total
This command counts lines in all .txt files and provides a total count. The wc -l option counts only lines.
List System User Accounts
Extract and display all user accounts from the system using xargs to format the output.
cut -d: -f1 /etc/passwd | sort | xargs echo
bin daemon games mail nobody root sys www-data
This command extracts usernames from /etc/passwd, sorts them alphabetically, and displays them on a single line using xargs with echo.
Key xargs Options
| Option | Description | Example |
|---|---|---|
-I {} |
Replace placeholder with input | ls | xargs -I '{}' cp '{}' backup/ |
-n NUM |
Use NUM arguments per command | echo 1 2 3 4 | xargs -n 2 |
-d DELIM |
Use DELIM as delimiter | echo "a,b,c" | xargs -d ',' |
-0 |
Handle null-separated input | find . -print0 | xargs -0 rm |
Conclusion
The xargs command is an essential Linux utility that bridges the gap between commands producing output and commands requiring arguments. It excels at automating repetitive tasks on multiple files and can significantly improve workflow efficiency. Mastering xargs with its various options like -I, -n, and -0 makes complex file operations simple and safe.
