Fundamentals 3 min read

Introduction to xargs with Basic and Advanced Usage Examples

This article explains the purpose of the Unix xargs command, demonstrates basic usage for concatenating log files, shows an advanced example for renaming text files to log files using the -I placeholder, and provides a step‑by‑step breakdown of how the command pipeline works.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Introduction to xargs with Basic and Advanced Usage Examples

xargs is a Unix command that reads arguments from standard input, assembles them into a command line, and executes the resulting command, making it useful for turning many single‑line commands into efficient batch operations.

Basic xargs example: The command ls *.log | xargs cat lists all .log files in the current directory and feeds their names to xargs cat , which concatenates and outputs their contents.

Advanced xargs example: To rename every .txt file to .log in a directory, you can use: ls *.txt | awk -F '.' '{print $1}' | xargs -I{} mv {}.txt {}.log

The -I option tells xargs to replace each occurrence of {} in the following command with the input string (e.g., aaa ), resulting in commands like mv aaa.txt aaa.log that are executed sequentially.

The article also outlines the processing steps: ls outputs file names to standard output. awk splits each name at the dot and outputs the base name. xargs receives the base name, substitutes it for {} , and constructs the mv command. The mv command renames the file from .txt to .log .

Note: the provided command sequence has a known bug, left as an exercise for readers to improve.

batch processingShellscriptingcommand-linexargs
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.