Cat Command – How to Use ‘cat’ on ‘find’ Command’s Output

catfind

I want to redirect the output of the find command to cat command so I can print the data of the given file.

So for example if the output of find is /aFile/readme then the cat should be interpreted as cat ./aFile/readme. How can I do that instantly ?

Do I have to use pipes ?

I tried versions of this :

cat | find ./inhere -size 1033c 2> /dev/null

But I guess this is completely wrong? Of course I'm sure that the output is only one file and not multiple files.

So how can I do that ? I've searched on Google and couldn't find a solution, probably because I didn't search right πŸ˜›

Best Answer

You can do this with find alone using the -exec action:

find /location -size 1033c -exec cat {} +

{} will be replaced by the files found by find, and + will enable us to read as many arguments as possible per invocation of cat, as cat can take multiple arguments.

If your find does not have the standard + extension, or you want to read the files one by one:

find /location -size 1033c -exec cat {} \;

If you want to use any options of cat, do:

find /location -size 1033c -exec cat -n {} +
find /location -size 1033c -exec cat -n {} \;

Here I am using the -n option to get the line numbers.

Related Question