12 Practical Examples of Linux Xargs Command for Beginners

Xargs is a large command that reads standard input data streams, then generates and executes command lines; meaning it can take the output of one command and pass it as an argument to another command. If no command is specified, xargs runs echo by default. They also instruct you to read data from a file instead of stdin.

There are several ways that xargs is useful in everyday command-line use. In this article, we will explain 12 practical examples of Linux xargs commands for beginners.

1. The first example shows how to find all .png images and archive them using the tar utility as follows.

Here, the action -print0

command allows you to print the full path of the file in the standard output, followed by a null character and the -0 xargs flag effectively handles space in file names.

$ find Pictures/tecmint/ -name “*.png” -type f -print0 | xargs -0 tar -cvzf images.tar.gz <img src="https://www.tecmint.com/wp-content/uploads/2018/08/Find-Images-and-Archive-Using-Tar.png" alt="Search images and archive using tar

” />
Search images and archive using tar

2. You can also convert the output of multiple lines of the ls command into a single line using xargs as follows.

$ ls -1 Pictures/tecmint/ $ ls -1 Pictures/tecmint/ | Xargs

lists files on a single line

3. To generate a compact list of all Linux user accounts on the system, use the following command.

$ cut -d: -f1 < /etc/passwd | Order | xargs <img src="https://www.tecmint.com/wp-content/uploads/2018/08/List-Files-in-Single-Line.png" alt="Search Linux user list

” />
Search Linux user list

4. Assuming you have a list of files and want to know the number of lines/words/characters in each file in the list, you can use the ls and xargs command for this purpose as follows.

$ ls *upload* | xargs wc <img src="https://www.tecmint.com/wp-content/uploads/2018/08/Find-List-of-Linux-Users.png" alt="Count number of lines, words, and characters

in files” />
Count number of lines, words, and characters in files

5. Xarags also allows you to recursively find and delete a directory, for example, the following command will recursively delete DomTerm in the Downloads directory.

$ find Downloads -name “DomTerm” -type d -print0 | xargs -0 /bin/rm -v -rf “{}”

Recursively find and delete directory
Recursively find and delete directory

6. Similar to the previous command, you can also find all the files named net_stats in the current directory and delete them.

$ find . -name “net_stats” -type f -print0 | xargs -0 /bin/rm -v -rf “{}”

7. Next, use xargs to copy a file to multiple directories at once; In this example we are trying to copy the file.

$ echo ./Templates/ ./Documents/ | xargs -n 1 cp -v ./Downloads/SIC_Template.xlsx <img src="https://www.tecmint.com/wp-content/uploads/2018/08/Find-and-Recursively-Delete-Directory.png" alt="Copy file to multiple directories

” />
Copy file to multiple directories

8. You can also use the find command, xargs, and the rename commands together to rename all files or subdirectories in a given directory to lowercase as follows.

$ find Documnets -depth | xargs -n 1 rename -v ‘s/(.*)/([^/]*)/$1/L$2/’ {} ;

9. Here is another useful usage example for xargs, it shows how to delete all files within a directory except one or a few files with a certain extension.

$ find . -type f -not -name ‘*gz’ -print0 | xargs -0 -I {} rm -v {}

10. As mentioned earlier, you can instruct xargs to read items from a file instead of the standard input using the -a flag as shown.

$ xargs -to rss_links.txt

11. You can enable verbosity using the -t flag, which tells xargs to print the command line to the standard error output before executing it.

$ find Downloads -name “DomTerm” -type d -print0 | xargs -0 -t /bin/rm -rf “{}”

12. By default, xargs terminates/delimits elements using white space, you can use the -d flag to set the delimiter to be a single character, a C-style character escape as n, or an octal or hexadecimal escape code.

In addition, you can also ask the user if he should run each command line and read a line from the terminal, using the -p flag as shown (just type y for yes or n for no).

$ echo ./Templates/ ./Documents/ | xargs -p -n 1 cp -v ./Downloads/SIC_Template.xlsx <img src="https://www.tecmint.com/wp-content/uploads/2018/08/Copy-File-to-Multiple-Directories.png" alt="Prompt user before running command

” />
Prompt user before running command

For more information, read the xargs man page. $ man xargs

That’s all for now! Xargs is a powerful utility for building a command line; It can help you pass the output of one command as an argument to another command for processing. In this article, we have explained 12 practical examples of xargs commands for beginners. Share your thoughts or questions with us via the feedback form below.

Contact US