How to use a pipe with Linux find command?



Linux find statement is one of the most widely used statements that allows us to walk a file hierarchy. It is used to mostly find a specific file or directories and we can also append different other Linux statements or flags along with it to enhance or do a complex operation.

Let’s explore an example of a find statement to understand it better.

In the Linux code shown below, I am trying to search for a file inside my Downloads folder, and for that I am making use of the find statement

find sample.sh

Output

sample.sh

Notice that if the find command is able to locate the file then it will simply print the name of the file, if not then it will not return anything the terminal process will terminate.

Now we know how the find statement works, let’s explore the case where we want to use the find command along with a pipe.

A pipe in Linux is just a vertical bar on your keyboard. It is used to consider the command that is on the left side of it as an input to the command on the right side of it.

Now that we know about both the find command the pipe in Linux, let’s consider an example where we will make use of both of these in a Linux command.

Command

find . -name '*.txt' | xargs cat

In the above command, just before the pipe, I am considering all the files that has a name that ends with a .txt extension and then after the pipe, I am simply printing those with the help of the cat command.

Output

immukul@192 directory1 % find . -name '*.txt' | xargs cat
this is a test file and it is used for testing and is not available for anything else so
please stop asking, lionel messi
orange
orange
blabla
blabla
foofoo
here
is the
text
to keep between the 2 patterns
bar
blabla
blabla

Advertisements