12 Practical Examples of Linux Xargs Command for Beginners

Xargs is a great command that reads streams of data from standard input, then generates and executes command lines; meaning it can take output of a command and passes it as argument of another command. If no command is specified, xargs executes echo by default. You many also instruct it to read data from a file instead of stdin.

There are several ways in which xargs is useful in daily usage of the command line. In this article, we will explain 12 practical Linux xargs command examples for beginners.

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

Here, the action command -print0 enables printing of the full file path on the standard output, followed by a null character and -0 xargs flag effectively deals with space in filenames.

$ find Pictures/tecmint/ -name "*.png" -type f -print0 | xargs -0 tar -cvzf images.tar.gz
Find Images and Archive Using Tar
Find Images and Archive Using Tar

2. You can also convert muti-line output from ls command into single line using xargs as follows.

$ ls -1 Pictures/tecmint/
$ ls -1 Pictures/tecmint/ | xargs
List Files in Single Line
List Files in 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 | sort | xargs
Find List of Linux Users
Find List of Linux Users

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

$ ls *upload* | xargs wc
Count Number of Lines, Words and Characters in Files
Count Number of Lines, Words and Characters in Files

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

$ find Downloads -name "DomTerm" -type d -print0 | xargs -0 /bin/rm -v -rf "{}"
Find and Recursively Delete Directory
Find and Recursively Delete Directory

6. Similarly to the previous command, you can also finds all 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 
Copy File to Multiple Directories
Copy File to Multiple Directories

8. You can also use the find command, xargs and rename commands together to to rename all files or subdirectories in a particular 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 few files with a given 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 standard input using the -a flag as shown.

$ xargs -a rss_links.txt

11. You can enable verbosity using the -t flag, which tells xargs to print the command line on 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 items using blank spaces, you can use the -d flag to set the delimiter which may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code.

In addition, you can also prompt the user about whether to run each command line and read a line from the terminal, using the -p flag as shown (simply type y for yes or n for no).

$ echo ./Templates/ ./Documents/ | xargs -p -n 1 cp -v ./Downloads/SIC_Template.xlsx 
Prompt User Before Running Command
Prompt User Before Running Command

For more information, read the xargs man page.

$ man xargs 

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

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

8 thoughts on “12 Practical Examples of Linux Xargs Command for Beginners”

  1. How to make wc work with xargs, I know wc.

    Just wc works fine, note file 3 had space in it ‘file 3‘.

    $  wc file*
       9   84  552 file1
       9   82  512 file2
       9   67  508 file 3
      27  233 1572 total
    

    wc command with xargs

    $ echo file? 'file 3' | xargs   wc -l 
       9 file1
       9 file2
    wc: file: No such file or directory
    wc: 3: No such file or directory
    
    $  ls -l file*
    -rw-rw-r-- 1  552 Dec 25 08:05  file1
    -rw-rw-r-- 1  512 Dec 25 08:06  file2
    -rw-rw-r-- 1  508 Dec 25 08:08 'file 3'
    
    $ echo file? 'file 3' | xargs   -I '{}'  wc -l '{}'
    wc: 'file1 file2 file 3': No such file or directory
    
    $ echo file? 'file 3' | xargs   -I %  wc -l %
    wc: 'file1 file2 file 3': No such file or directory
    
    Reply
  2. Examples 5, 6, 8, and 11 don’t define the replace-str. They need `-I {}` added.

    Examples 5, 6, 11 enclose {} within double quotation marks. The quoting of {} is unnecessary in all mainstream shells, as discussed here https://unix.stackexchange.com/questions/8647 (That discussion is in the context of GNU find, but is equally applicable to xargs.)

    Reply
  3. Hi guys!

    I really like this article, I just really felt the need to point out, this article could use some spell checking and general grammar checking. Also, the output of a command is referred to as stdout NOT stdin, stdin would be the keyboard. So you may want to correct that. So you are instructing xargs to act on the output a command that is stdout i.e. the display.

    All the best and keep up the good work amigos!

    Reply
    • @Gunnar

      Thanks for your useful feedback. Concerning this: “Also, the output of a command is referred to as stdout NOT stdin, stdin would be the keyboard.” I think it is clearly explained in the article in terms of how xargs works. It reads data from standard input, then generates and executes command lines.

      True the output of a command is sent to stdout but when piped to xargs, it considered as std in(please read the xargs man page for more information). In the above examples, we have used the pipe character to enable it to read from standard input, which in this case is the output of another command. We will work on the spell checking and general grammar checking. Thanks once again.

      Reply
  4. Thank you tecmint so much. I always learn reading your posts. Since I love what you do, I want to help make it better.

    There is something that I think is wrong in the first example.

    You should enclose the asterisk in quotes to prevent getting expanded by the shell before being passed as a parameter to find. Meaning, the command should look like (notice the argument to -name option):

    find Pictures/tecmint/ -name "*.png" -type f -print0 | xargs -0 tar -cvzf images.tar.gz

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.