
In such cases, you should use the -print0 option of the find command. Grep: lotus.txt: No such file or directory type f -name "*.txt" | xargs grep -l red Now when it is processed via xargs, it is seen as two separate files as three and lotus.txt. Let’s say I renamed three_lotus.txt to “three lotus.txt”. If you have file with space in its name, it will cause issues. But let's focus on the xargs command here. The find exec command combination works similarly. You can combine find and grep commands with the help of xargs: :~/tutorial$ find. Let’s say, you want to get all the files ending in. Thanks to xargs, you can use the result of the find command for specific purposes like renaming them, moving them, deleting them and whatnot. The find command searches for files and directories and returns their names. You’ll often find it being used in combination of the “find command”. You realize the power of xargs command now, don’t you? xargs and find: made for each other Think of it as equivalent to feeding those filenames to the du command: :~/tutorial$ du -h lily.txt one_lotus.txt rose.txt three_lotus.txt two_lotus.txt :~/tutorial$ cat flowers.txt | xargs du -h The magic of xargs command is that it will take this and the text separated by blanks or new lines and convert them into individual inputs to the next command. It’s a like a words separate by new line character.

Second, the output of the cat command is not individual file names. Why? First, the du command doesn’t take standard input. Common sense says that I could combine cat command to display all the filenames and then pipe it to the du command to check the file size.īut if I pipe it directly, it won’t give the size of each of the files mentioned in the flowers.txt file. Now my aim is that I want to see the file size of all the files mentioned in the flowers.txt. In my current directory, I have some text files and the flowers.txt has the name of all these files: :~/tutorial$ lsįlowers.txt lily.txt one_lotus.txt rose.txt three_lotus.txt two_lotus.txt It’s power lies in combining the output of one command to another. Xargs command has the following syntax: xargs ]īut you are probably not going to use it like that. Since xargs works on redirection, I highly recommend brushing up your knowledge of stdin, stdout and pipe redirection in Linux. The find command gives you a list of filenames and the xargs command lets you use those filenames, one by one, as if it was input to the other command. You’ll often find xargs command being used with the find command. The xargs command reads lines of text from the standard input or from the output of another command and turns them into commands and execute them. In this tutorial, I’ll show you how you can xargs to convert the standard input into commands.

Xargs is one of the most powerful commands in Linux.
