Master Linux xargs with 6 Real‑World Examples for Faster Automation
This tutorial explains the Linux xargs command and provides six practical, step‑by‑step examples—including copying files, deleting by extension, compressing directories, renaming to uppercase, counting lines, and listing user accounts—to help automate common system tasks efficiently.
In this guide we explore the Linux xargs command, a powerful tool for feeding the output of one command as arguments to another, and demonstrate six concrete use cases that simplify everyday system‑administration tasks.
1. Copy a list of files to a new directory
Use xargs with cp to copy all .txt files in one step.
ls *.txt | xargs -I '{}' cp '{}' /path/to/new_directory/file1.txt file2.txt
The command lists .txt files, substitutes each filename for the placeholder {}, and copies them to the target directory.
2. Delete all files with a specific extension
Combine xargs with rm to remove every .log file.
ls *.log | xargs rmfile1.log file2.log File3.log
The list of .log files is passed to rm, which deletes each one.
3. Compress every file in a directory
Pipe the file list to tar via xargs to create a single archive.
ls | xargs tar -czvf archive.tar.gz user@ubuntu:~/directory$ ls | xargs tar -czvf archive.tar.gz
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
archive.tar.gz
user@ubuntu:~/directory$This gathers all filenames, feeds them to tar, and produces archive.tar.gz.
4. Rename multiple files to uppercase
Use xargs with a shell command to transform filenames.
ls | xargs -I '{}' sh -c 'mv "{}" $(echo "{}" | tr "[:lower:]" "[:upper:]")' file1.txt
file2.txt
file3.txt
mv file1.txt FILE1.TXT
mv file2.txt FILE2.TXT
mv file3.txt FILE3.TXTThe -I option substitutes each filename, and tr converts it to uppercase before moving.
5. Count lines in multiple files
Combine xargs with wc -l to display line counts per file.
ls | xargs wc -l 12 file1.txt
24 file2.txt
6 file3.txtThe command lists files, passes them to wc -l, and prints each file’s line total.
6. List all Linux user accounts
Extract usernames from /etc/passwd, sort them, and feed the list to xargs for further processing. cut -d: -f1 < /etc/passwd | sort | xargs cut -d: -f1 extracts the first field (username) from /etc/passwd.
sort arranges the usernames alphabetically.
xargs receives the sorted list as arguments for the next command.
$ cut -d: -f1 < /etc/passwd | sort | xargs echo
bin daemon ftp games gnats irc list lp mail man messagebus news nobody proxy root systemd-network sync sys syslog systemd-bus-proxy systemd-journal-gateway systemd-journal-remote systemd-timesync uucp www-dataThese examples illustrate how xargs can streamline repetitive file‑handling operations, saving time and reducing manual effort for Linux users.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
