A-Z Popular Blog Encyclopedia Search »
Technology
 Advertisements
Information Technology

Integration

It Management

6 Useful Xargs Commands For Linux

 ,
The command xargs is used to feed arguments from standard input to other commands in linux. When combined with the pipe operator |, xargs can be used to convert the output of one command to the arguments of another command. The following useful commands that are constructed in this fashion.

Search Files For Text

Use find to obtain a list of files that xargs feeds to grep as parameters to search each file for a textual string.
find . -name *.log | xargs grep "error 57"
The example above searches all log files in a directory tree for the text "error 57."

Search And Delete

Delete files that match a search.
find . -name thumbs.db | xargs --interactive rm
The command above will find all files named thumbs.db and delete them. The --interactive flag on xargs will prompt the user once to confirm the deletion.

Delete A List of Files

Read a list of filenames from a file and delete the files listed.
xargs -a deleteus.txt --interactive rm
The command above will delete the files that correspond to the filenames listed in the .txt file. The entries in the text file must be separated by newlines or spaces. The delimiter can be changed to another character with the -d flag. For example:
cat deleteus.txt | xargs -d ',' rm
The command above has a slightly different format but also deletes a list of files. The only different is the -d flag has been set to change the filename delimiter to a comma.

Filter Files For Archiving

Use the powerful filter functionality of ls to create an archive file with tar.
ls -1 --ignore=*.tar | xargs tar -cvf backup.tar
The command above creates an archive that includes all files and directories except tar files. This can be used to avoid backing up your backups.

Count Lines In File Listings

Modify the ls command to display the number of lines in each file as opposed to a byte count.
ls *.log | xargs wc -l
The command above will show the number of lines in all the .log files under the current working directory. The wc command can also display a word count by adding -w.

Display All Users

xargs is often used to feed the results of some text formatting to echo. For example, the command below displays the users on a system by parsing the /etc/passwd file.
cut -d: -f1 < /etc/passwd | sort | xargs echo
Overview: xargs
Type
Definition
A linux command that converts standard input into arguments to a command.
Related Concepts

Linux Commands

This is the complete list of articles we have written about linux commands.
ls
shutdown
xargs
If you enjoyed this page, please consider bookmarking Simplicable.
 

Linux Commands

A list of unusually useful linux commands with brief explanations.

Linux Processes

The basic types of linux process.

ls

A quick reference of useful ls commands.

Shutdown

A list of useful shutdown, halt, reboot and power-off commands for linux.

xargs

A list of xargs commands for linux you might actually use.

Application Server

An overview of application server with examples.

Transcoding

The definition of transcoding with examples.

Static Content

The definition of static content with examples.

Dynamic Content

The definition of dynamic content with examples.

Computer Science Examples

The definition of computer science with examples.

Command Prompt

The definition of command prompt with examples.
The most popular articles on Simplicable in the past day.

New Articles

Recent posts or updates on Simplicable.
Site Map