Xargs multiple commands

What is xargs in Linux

What is xargs in Linux, with 20 examples:

Xargs command in Linux provides us the best features to automate complex tasks with a single command line. Of course, I always recommend that everybody get more Linux skills to go deep on this command. But, sometimes looking at man xargs is a good opportunity, but sometimes it’s not enough to give us an answer for a specific problem, so Bit Lovers prepared a practical list of good examples of Xargs in Linux:

  • 1. When you are trying to delete too several files using rm, you may see the error message: /bin/rm Argument list too long – Linux. Use the xargs command in Linux to bypass this problem.
find ~ -name ‘*.log’ -print0 | xargs -0 rm -f
  • 2. You can also change the multi-line output from ls command in a single line using the xargs in Linux as follows:
ls -1 Images/bitslovers/
ls -1 Images/bitslovers/ | xargs
  • 3. Considering you have a list of files and need to recognize the number of lines/words/characters in each file in the list. You can use the ls command and xargs for this method as follows.
ls *upload* | xargs wc
  • 4. Xargs also enables you to discover and recursively remove a directory. As an example, the following command will recursively remove bitslovers.tar.gz in the directory Downloads
find Downloads -name "bitslovers.tar.gz" -type d -print0 | xargs -0 /bin/rm -v -rf "{}"
  • 5. Here, the command -print0 allows printing of the entire file path on the standard output, accompanied by a null character and -0 xargs flag entirely deals with space in filenames.
find Image/bitslovers/ -name "*.jpg" -type f -print0 | xargs -0 tar -cvzf image.tar.gz
  • 6. Likewise to the past command, you can additionally find all files named bitslovers_backup in the current directory and delete them.
find . -name "bitslovers_backup" -type f -print0 | xargs -0 /bin/rm -v -rf "{}"
  • 7. You can similarly use the find commandxargs, and rename commands concurrently to rename each file or subdirectories in a distinct directory to lowercase as follows.
find Documnets -depth | xargs -n 1 rename -v 's/(.*)/([^/]*)/$1/L$2/' {} ;
  • 8. Here is a different helpful usage example for xargs. It shows how to delete all files inside a directory, excluding one or a few files with a given extension.
find . -type f -not -name '*doc' -print0 | xargs -0 -I {} rm -v {}

9. As discussed shortly, you can tell xargs to scan items from a file instead of standard input using the -a flag as pointed.

xargs -a bitslovers_links.txt
  • 10. You can use the verbose mode using the -t flag, which says xargs, to show the command line on the standard error output before doing it.
find Downloads -name "BitsLovers.doc" -type d -print0 | xargs -0 -t /bin/rm -rf "{}"
  • 11. To create a shortlist of full Linux user accounts on the system, use the subsequent command.
cut -d: -f1 < /etc/passwd | sort | xargs
  • 12. By default, xargs ends/delimits items using blank spaces. You can use the -d flag to set the delimiter, a single character, a C-style character escape such as n, or an octal or hexadecimal escape code.

You can also prompt the user to run each command line and read a line from the terminal, applying the -p flag as displayed (type y for yes or n for no).

echo ./Blog/ ./Documents/ | xargs -p -n 1 cp -v ./Downloads/bitslovers-best-way-to-use-xargs.xlsx
  • 13. Create a list of all the *.conf files under /etc/. There are several ways to get the equivalent result. The following case is only to illustrate the use of xargs. This example the find command output using ls –l one by one using xargs.
find /etc -name "*.conf" | xargs ls –l
  • 14. If you own a file with the list of URLs that you would like to download, you can utilize xargs, as noted below.
cat URL-LIST.txt | xargs wget –c
  • 15. Find out all the jpg images and archive it.
find / -name *.jpg -type f -print | xargs tar -cvzf bitslovers.tar.gz
echo ./Blog/ ./Documents/ | xargs -n 1 cp -v ./Downloads/bitslovers-best-way-to-use-xargs.xlsx
  • 17. Copy all the images to an external hard-drive.
ls *.png | xargs -n1 -i cp {} /hard-drive/directory
  • 20. Include Arguments at an Appropriate Location

Xargs allows possibilities to insert the listed arguments at any random position from different than the end of the command line.

The -I option accepts a string that gets replaced with the provided input before the command does. Although this string can be any collection of characters, a simple choice for it is “%”.

Let’s move catalina.log to the tmp directory:

find ./log -type f -name "*.log" | xargs -I % mv % tmp/
ls -l ./log
total 0
ls -l ./tmp/
-rw-rw-rw- 1 someone someone 6273 Jan 1 19:09 catalina.log

  • 21. Let’s terminate all pods from a Kubernetes Cluster from a specific namespace
  • kubectl get pods -n namespace | awk '{print $1}' | grep "pods-name*" | xargs kubectl delete pod

    The command above, first list all pods, then we filter (awk) to show only the pod’s names column, and later we filter (grep) the pod’s names that we need to delete, and finally, we execute the xargs command to delete them.


    Handling Files with Special Characters in the Names

    A filename can contain special characters like single quotes, double quotes, or spaces.

    Let’s see how to handle those files when passed to xargs.

    Filename Contains Spaces

    Let’s verify if xargs uses the file “file 1.log”, to the rm command, despite the space in the name:

    ls -l ./log
    -rw-rw-rw- 1 bitslovers bitslovers 1234 April 13 2020 file 1.log
    find ./log -type f | xargs rm
    rm: cannot remove ‘./log/file’: No such file or directory
    rm: cannot remove ‘1.log’: No such file or directory

    As we can notice, xargs transfers two arguments ./log/file and 1.log to the rm command instead of an individual argument ./log/file 1.log. Of course, there are no files with name file and 1.log, so the rm command returns a failure message No such file or directory.

    To make xargs identify the files with spaces in their names, apply the -I option. But we must quote the placeholder:

    find ./log -type f | xargs -I % rm "%"
    ls -l ./log
    total 0

    Now has been deleted the file 1.log.

    Xargs when the filename contains quotes:

    Let’s begin with a great example using find commad:

    find ./log -type f
    ./log/file2.log
    ./log/file1.log

    We have built two log files. The file file’1.log has a single quote in the name, while the file”2.log has a double quote character.

    Now, let’s pass the output of find to the xargs command and try to delete the two files:

    find ./log -type f | xargs rm

    xargs: unmatched double quote; by default, quotes are special to xargs unless you use the -0 option

    Let’s see if quoting the placeholder of the xargs‘s -I option helps:

    find ./log -type f | xargs -I % rm '%'

    xargs: unmatched double quote; by default, quotes are specific to xargs unless you use the -0 option

    find ./log -type f | xargs -I % rm "%"

    xargs: unmatched double quote; by default, quotes are specific to xargs unless you use the -0 option

    The outline shows, if a file includes a single or double quote character in its name, xargs cannot run by quoting the filename.

    To deal with this restriction, both xargs and the command giving the output to xargs must handle a null character as the record separator.

    We can give the option -print0 to the find command to request it to output filenames separated by a null character instead of the default newline.

    On the xargs command side, since the default record separator is a newline character too, we require the -0 option to apply a null character as a record separator:

    find ./log -type f -print0 | xargs -0 rm
    ls -l ./log
    total 0

    The output above shows the pair files have been deleted.

    See, however, that some commands cannot accept a null character as a separator (for example, headtaills, echo, sed, and wc). In these circumstances, xargs cannot manage such commands if the output contains quote marks.

    Conclusion

    So, In this short tutorial with these examples, we noticed the use of xargs to create and perform commands from standard input.

    It is used in circumstances when a command can get input from other commands, just in the form of arguments.

    And, It is also advantageous when we have a list of STDIN files, and we need to do something with them.

    Lastly, we noticed that the xargs command has its limitation with files having a single quote or double quote in the name.

    Xargs in Linux, supports several parameters, to get good visibility man xargs could be used to explore them.

    Also, there is another combination of how to use xargs in Linux. Check here others examples with xargs and bash -c and create a group of commands. I hope that you enjoy reading that Xargs command in the Linux post and have learned something new.

     

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.