Search Files For TextUse 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 DeleteDelete files that match a search.
find . -name thumbs.db | xargs --interactive rmThe 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 FilesRead a list of filenames from a file and delete the files listed.
xargs -a deleteus.txt --interactive rmThe 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 ',' rmThe 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 ArchivingUse the powerful filter functionality of ls to create an archive file with tar.
ls -1 --ignore=*.tar | xargs tar -cvf backup.tarThe 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 ListingsModify the ls command to display the number of lines in each file as opposed to a byte count.
ls *.log | xargs wc -lThe 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 Usersxargs 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
A linux command that converts standard input into arguments to a command.