How to use the xargs command on Linux

Need to string some Linux commands together but one of them won’t take pipe input? xargs can take the output from one command and send it as a parameter to another command.

All standard Linux utilities have three data streams connected to them. These are the standard input stream (stdin), the standard output stream (stdout), and the standard error stream (stderr).

These streams work with text. We send an input (stdin) to a command with text, and the response (stdout) is written as text in the terminal window. Error messages are also written as text (stderr) in the terminal window.

One of the great things about Linux and Unix-like operating systems is the ability to route the stdout output of one command into the stdin input of a second command. The first command does not care that its output does not go to a terminal window, and the second command does not care that its input does not come from a keyboard.

Although all Linux commands have the standard three streams, not all of them accept the stdout of another command as input to their stdin. This means that you cannot forward any input to them.

xargs is an instruction to create execution pipelines using the standard data streams. By using xargs we can give orders like echo , rm , and mkdir accept standard inputs as arguments.

The xargs command

xargs accepts piped input. It can also accept input from a file. xargs uses this input as a parameter for the commands it is designed to work with. If we don’t tell xargs to work with a specific command, this is used by default echo .

We can use that to show how xargs always generates a single output line, even with a multi-line input.

If we use that -1 (list one file per line) option with ls , we get one single column with filenames .

                      ls -1 ./*.sh

This will list the shell script files in the current directory.

We get a single column as expected. If we pass it through xargs what do we get

                      ls -1 ./*.sh | xargs

The output is written as a long text stream in the terminal window.

It is this ability that gives us xargs Feed parameters into other commands.

Using xargs with wc

We can use xargs easy to have wc count them Words, characters and lines in multiple files.

                      ls *.page | xargs wc

This is what happens:

  • ls lists the * .page files and passes the list to xargs .
  • xargs passes the filenames wc .
  • wc treats the file names as if they were received as command line parameters.

The statistics for each file are displayed along with a grand total.

Using xargs with confirmation

We can use that -p To have an (interactive) option xargs ask us to confirm that we agree to proceed.

When we supply a string of filenames. to hand over touch , by xargs , touch will create the files for us.

                      echo 'one two three' | xargs -p touch

The command to be executed is displayed and xargs wait for our answer by entering “y” or “Y” or “n” or “N” and pressing Enter.

If you just push Enter, it is treated as “n”. The command is only executed if you enter “y” or “Y”.

We pressed and pressed “y” Enter. We can use ls to verify that the files were created.

                      ls one two three

Using xargs with multiple commands

We can use several commands with xargs with the -I (Initial arguments) option.

This option defines a “replace string”. Wherever the replacement string token appears on the command line, the values ​​that go to. were supplied xargs inserted.

Let’s use that tree Command to display the subdirectories of the current directory. the -d (Directory) option causes tree to ignore files and only report on directories.

                      tree -d

There is a single subdirectory called “images”.

In a file called “directories.txt” we have the names of some of the directories that we want to create. We can look at the content with cat .

                      cat directories.txt

We use this as input data for xargs . The command we are going to run is:

                      cat directories.txt | xargs -I % sh -c 'echo %; mkdir %'

This is broken down as follows:

  • cat directory.txt |: This pushes the contents of the file directories.txt (all new directory names) into xargs .
  • xargs -I%: Defines a “replace-string” with the token “%”.
  • sh -c: This starts a new subshell. the -c (Command) tells the shell to read commands from the command line.
  • ‘Echo%; mkdir% ‘: Each of the “%” tokens is replaced by the directory names that are passed xargs . the echo Command prints the directory name; the mkdir Command creates the directory.

The directories are listed one after the other.

We can use tree again to check that the directories were created.

                      tree -d

Copy files to multiple locations

We can use xargs to allow us to copy files to multiple locations with a single command.

We’re going to be the names of two directories in xargs as input parameters. We will say it xargs to pass only one of these parameters to the command it is working with at a time.

In this case the command is cp . So the effect is to call cp twice, each with one of the two directories as a command line parameter. the xargs The parameter that enables this is the -n (maximum number) option. We will set this to be one.

We also use that -v (detailed) option with cp so that it reports what is happening.

                      echo ~/Backups/ ~/Documents/page-files/ | xargs -n 1 cp -v ./*.page

The files are copied into the two directories one after the other. cp reports every file copy action so we can see how it is performed.

Deleting files in nested directories

If filenames contain spaces and strange characters – such as. B. Line break character – xargs will not be able to interpret them correctly. We can solve this problem by using the -0 (null terminator) option. That says xargs to use the null character as the last separator for file names.

We will use find in this example. find has its own option for Handling spaces and strange characters in filenames. It’s the -print0 (full name, null character) option.

                      find . -name "*.png" -type f -print0 | xargs -0 rm -v -rf "{}"

This is broken down as follows:

  • Find . -Name “* .png”: find will be in the current directory “.” Looking for. for objects with names that match “* .png”, which are files ( type -f ).
  • -print0: Names are terminated with a null character, spaces and strange characters are taken into account.
  • xargs -0: xargs also considers filenames null-terminated, and spaces and strange characters won’t cause any problems.
  • rm -v -rf “{}”: rm will be verbose and report what happens ( -v ). It will be recursive (-r) and will search nested subdirectories and remove files without prompting ( -f ). The “{}” is replaced by every file name.

All subdirectories are searched and the files that match the search pattern are deleted.

Remove nested directories

Let’s say we want to remove a number of nested subdirectories. tree we’ll let them see

                      tree -d

                      find . -name "level_one" -type d printo | xargs -o rm -v -rf "{}"

This command uses find to search recursively in the current directory. The search target is a directory called “level_one”. The directory names are passed through xargs to rm .

The only significant changes between this command and the previous command are that the search term is the top-level directory name and -type d told find look for directories, not files.

The name of each directory is printed when it is removed. We can check with tree :

                      tree -d

All nested subdirectories are deleted.

Delete all files except one type of file

We can use find , xargs and rm to delete all files except one type that we want to keep. It’s a little counter-intuitive, but we include the name of the type of file we want to keep, not the names of the ones we want to delete.

the -not Option says find to return the names of the files that do not match the search pattern. We use that -I (Initial arguments) option with xargs once more. This time the replacement string token we are defining is “{}”. This behaves exactly like the previously generated token to replace the character string, which happened to be a “%”.

                      find . -type f -not - name "*.sh" -print0 | xargs -0 -I {} rm -v {}

We can check with ls . The only files that remain in the directory are those that match the search pattern “* .sh”.

                      ls -l

Create an archive file with Xargs

We can use find to search for files and share them xargs to tar to create an archive file.

We’re looking in the current directory. The search pattern is “* .page”, so we’re looking for “.page” files.

                      find ./ - name "*.page" -type f -print0 | xargs -0 -tar -cvzf page_files.tar.gz

The files are listed as expected while the archive file is being created.

The data broker

Sometimes you need a small scaffolding when stacking. xargs bridges the gap between commands that can output information and commands that are not designed to receive it.

Both xargs and find have a variety of options. You are encouraged to read their man pages to learn more.

Linux commands
Files tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · Journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processes alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networking netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts