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 »Ls » |