How to use “cat” command on “find” command’s output?

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 😛

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Command Substitution

Another option is to use Command Substitution. Wrapping a command in $() will run the command and replace the command with its output.

cat $(find ./inhere -size 1033c 2> /dev/null)

will become

cat ./inhere/file1 ./inhere/file2 ./inhere/file3

This is more or less equivalent to using the older style of wrapping commands with back ticks:

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

More details from the docs linked above

Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by $, `, or . The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.

Command substitutions may be nested. To nest when using the backquoted form, escape the inner backquotes with backslashes.

If the substitution appears within double quotes, word splitting and filename expansion are not performed on the results.

See this other answer for some good examples of usage.

Method 3

This will print the name and contents of text (ascii) files-only recursively..

find . -type f -exec grep -Iq . {} ; -print | xargs awk 'FNR==1{print FILENAME ":" $0; }'

One more attempt

find . -type f -exec grep -Iq . {} ; -printf "n%p:" -exec cat {} ;

Method 4

Use pipe and xargs

find /* -name pg_hba.conf | xargs cat

If it fails because of lack of permissions, you can use sudo to make sure you have permission to execute find command on the directory you are searching.

 sudo find /* -name pg_hba.conf | xargs sudo cat


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x