How to Use xargs with Find on Linux 1

How to Use xargs with Find on Linux

Are you wondering about the best solution for using xargs with Find on Linux? We’ve got you covered with the quick guide down below. 

In Linux, there are two ways to work with input. One can take input from the command line or from the standard input stream. Some commands can work only with standard input. For such a command, one needs xargs to process the input. Here, we have discussed the xargs command with every single detail you need to know. 

How to Use xargs with Find on Linux?

xargs command

The xargs command takes command as input from standard input and executes the command. It basically builds an execution pipeline. This can be used in combination with other commands such as cp, rm, and mkdir commands. The working of xargs is very simple. It reads input from standard input separated by spaces and executes a command once for each argument. 

Using xargs to echo content

The following command shows the basic usage of xargs. When run alone, it asks the user for an input and echoes it back to the standard output stream. The following line illustrates that:

$xargs

The above command will ask the user for input and echoes it back as follows:

user input echoes

xargs with find

One can precede xargs with the find command. The output of the find command can be used for further processing by xargs. This can include removing those files, changing their permissions, etc. The following command removes all the files with the sh extension:

$find /home.demo .-name'*.sh' -type f| xargs rm -f

The above command first searches for all the files with extension ‘sh’ and then the list of files output by the find command is piped for xargs. The xargs command then uses the rm command with the -f flag to forcefully remove all the files.

Note that xargs doesn’t automatically process input with blank spaces. To include those files, use the -print0 option as follows:

$find /home/demo/ .-name '*.sh' -type f -print0 | xargs -0 rm -f

Using xargs to search for a string in a file

One can use xargs with the grep command to search for a string in a list of files provided by the find command. The following command searches for the string ‘hello’ in all the text files:

$find . -name '*.txt' | xargs grep 'hello'

Moving the specific files to a directory

Suppose you want to move all the images to a specific directory. You can use the xargs command to perform this task as follows:

$find . -name '*.jpeg' | xargs mv -t demo

The above command first searches for all the files with the .jpeg extension and then moves these files to the demo directory.

Changing permissions of the file

Suppose you want to search for files with permissions for everyone (777) and then change the full permission to owner only and read/execute permission for group users and others (755), the following command can be used:

find . -perm 777 | xargs chmod 755

Running multiple commands with xargs

You can run multiple commands with xargs. For this purpose, use the -I option as follows:

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

In the above command, the data.txt file contents are first displayed using the echo command. The directory is created based on the contents of data.txt.

Passing input from a file

Xargs, by default, read from the standard input stream. However, the xargs can also read from a file. For this purpose, the -a option can be used as follows:

$xargs -a file.txt

Creating a tar archive using xargs and find command

The following command finds all the images with jpg extension and archives them using the tar command with xargs:

$find /home/demo -name "*.jpg" -type f | xargs tar -cvfz images.tar.gz

Using the -t flag to see the commands run by xargs 

To see the commands run by xargs, use the -t option as follows:

$ cat file1.txt | xargs -t mkdir

This option is very useful for debugging purposes.

Displaying a prompt before executing a command

The execution performed by xargs, such as removing a file, copying, etc., is irreversible. Therefore, you can use the -p command to first confirm from the end-user before performing the operation.

$ cat file1.txt | xargs -p mkdir

The above command will ask the user first before creating the directory.

Limit the number of input

One can limit the number of arguments received by xargs at a time. For this purpose, the -n option can be used to specify the number of inputs xargs can take at a time. The following command illustrates that:

$echo "1 2 3 4 5 6" | xargs -n 3

The above command splits the input into sizes of 3 and echoes two lines.

How to Use xargs with Find on Linux

Tip: 

  • The default delimiter for the argument is space. However, one can change the delimiter to any other character using the -d command.
  • The find command supports the execution of arbitrary commands with exec. So, instead of using xargs with the output for find, exec can be used. However, the execution of xargs is much faster than the exec command.

We discussed xargs that can be used to build an execution pipeline. The command can be used in combination with other commands, such as find to search for a file, and then the output can be used in combination with xargs to perform some command or action on those files. The various options of xargs have been discussed in this article. More details can be seen in the manual.

If this guide helped you, please share it.

Leave a Reply
Related Posts