How to Use xargs Command in Linux

The Linux Terminal is a powerful tool that enables all sorts of computing processes. It has a wide-ranging repertoire of proven and tested commands that allows limitless combinations and possibilities. One such thing is xargs in Linux Command, a simple and versatile tool that packs tons of utility.

What is xargs Command in Linux?

xargs is Linux Command in Linux and Unix systems. It is very dependent on other commands to function effectively. In its official documentation, xargs works by passing the output of a command as an input for another command.

It is a quick fix if the user wants to automate super repetitive or super complicated tasks. In addition, there are certain tasks wherein an output from a command is needed for another command’s operation.

xargs Default Format

Commands like cp, rm, and echo need user input to function properly. With xargs, you can automate the process by “funneling” user inputs from other sources such as files or other commands. The result from the first command is funneled into xargs, which the second command would utilize.

$ <first command> | <second command> 
$ printf “1\n2\n3\n” | xargs mkdir

In the example above, the output from printf is funneled as an input to mkdir. The results from printf are then utilized by mkdir to run its functions. Performing this command creates three new folders named 1, 2, and 3.

Options for xargs Command in Linux

xargs has a lot of options to choose from, which can be helpful when tackling a unique project roadblock. However, not all the things it can offer can be are used by the majority of users due to the nature of usage. Here are some of the most commonly-used options.

-a: Takes input from a file instead of the standard output.

-d: Changes default delimiter with another character.

-I: Plugs data from standard input as an initial argument.

-L: Reads and connects multiple command lines, so it is read as one input.

-p: Asks the user whether to proceed or cancel the command.

-n: Customizes the number of arguments that xargs is allowed to process from standard input.

Using xargs Command in Linux with Other Commands

xargs’ most promising aspect is the fact that it can work with other Linux commands. However, the user would need to have a good understanding of other commands and their respective parameters to create the right syntax and perform the desired operations properly.

Finding and Processing Files

One of xargs’ strongest capabilities is shown when combined with find and other commands. This is a big deal when scripting because it can automate getting inputs from a process and displaying outputs from the results without the user’s interference.

Searching and Displaying Multiple Files and Information

The find command is often used with xargs to search and process unsorted files. Its output can be used as arguments through xargs. Typing xargs wc, will show the file’s byte counts, words, and more details depending on the parameters provided.

$ find -name "*.png" -type f | xargs wc
xargs command in linux

Searching and Moving Multiple Files

You can also search and move files from one directory to another without dragging and dropping the documents at the destination one at a time. This is done by combining find and mv to streamline the process.

Don’t forget to add -I as an additional xargs option, it plugs the data from the standard input as an argument. In this example, the command searches for every PNG file within the directory. It will then automatically move all those files to a folder named BTM.

$ find -name "*.png" -type f | sudo xargs -I '{}' mv '{}' BTM

Searching and Renaming Files

The following command is for searching and renaming specific files.  Doing it with multiple strings can become overly confusing, so it’s best to use one term at a time.

Here’s an example below: the first part deals with searching files a file called “grian.png” while the second part replaces it with “poultryman.png”.

$ find "grian.png" | xargs -I '{}' mv '{}' poultryman.png

Searching and Deleting Files

And lastly, you can also search and immediately delete any files by combining find and rm commands as follows:

$ find "mumbo.png" | xargs -I "{}" rm "{}"

Archiving Multiple Files

Packing up files for archiving can be a pain in the neck in the terminal, especially if you need to sort out specific documents in a directory with hundreds of unsorted files. It can be automated by using a nicely-created find parameter to get the necessary files. tar will then process the output to create the archive.

Check the example below, this collects all PNG files and archives them into “BTM.tar.gz” without the user manually searching and checking for files individually.

$ find -name "*.png" -type f | xargs tar cvf BTM.tar.gz
xargs with tar

Directory Functions

Here’s a trick if you don’t want to create dozens of folders manually: combine cat with xargs and mkdir. Use a TXT file to list all the individual folders that you want to create. xargs will funnel down all the strings within the TXT file into mkdir to create those folders.  

Alternatively, you can use echo to funnel strings into mkdir, and it should work similarly. However, having a reference file will be easier if you need to create 10+ folders.

$ cat btm.txt | xargs mkdir
$ cat btm.txt | xargs rmdir

You can also change the ownership and permissions for single or multiple files with xargs. Use find to select the specific files and pass the output to xargs afterward, which will send the data for processing to chmod.

$ find -name "*.png" -type f -print0 | xargs -0 chmod u+r

Printing and Standard Input

You can easily list out a directory content using the ls command. But if you only want to view items with specific phrases or file extensions, you can use the ls output with xargs and show it in the terminal using wc.

$ ls "scar.png" | xargs wc
xargs with wc

File contents are viewable by funneling cat output into xargs. You can modify how many words appear in each line by using -n followed by your desired number.

$ cat intro.txt | xargs -n 7
xargs command in Linux with cat

To add a layer of safety, you can prompt the user to proceed or cancel the command by adding -p. When this option is active, the user needs to input Y/N.

$ echo "Delete Mumbo?" | xargs -p rm mumbo.png
xargs with linux prompt

Final Thoughts

The beauty of the xargs command is that you can mix and match commands that are traditionally operating independently. If you have the patience and skills to create your CLI-based solutions, you will realize how xargs has tons of things to offer. If this guide helped you, please share it.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts