How to use "cat" command on "find" command's output?
Solution 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.
Solution 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.
Related videos on Youtube
liz14
Updated on September 18, 2022Comments
-
liz14 almost 2 years
I want to redirect the output of the
find
command tocat
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 ascat ./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 :P
-
liz14 about 8 yearsSo, If I'm correct : {} is like a variable that in it is stored the output of the find command and after the "+" sign you're supposed to write the cat parameters ? No ? Edit : Alright you edited your post and now you just answered my question :P Thanx man ! Solved !
-
heemayl about 8 years@general656 Check my edits..
-
Stéphane Chazelas over 7 yearsAnd if someone has created a file called
/tmp/ /dev/urandom /pg_hba.conf
, that will let root dump the whole content of/dev/urandom
. Worse can probably be done depending on the context.xargs
should almost never be used without-0
if at all. Alsosudo xargs
would be better thanxargs sudo
. And/*
would be better as/
. -
App Work over 7 yearsThe OP wants to use cat to print the file content on stdout. So the purpose is served by cat. The question is how to use cat from the out put of find command to print the content of the file not how to use sudo or find , The above command solves the question. Yes xargs -0 was the only constructive thing you pointed. But did not show the usage. Try not to be rude.
-
Stéphane Chazelas over 7 yearsSorry if that appeared rude. That was not the intention. I wanted to point out the limitations. Your answer shows bad practices (
find|xargs
without-0
should really be banned), hence the down vote. The output offind
without-print0
is not post-processable reliably.-print0
is not standard. Having said that, standardsudo find / -name pg_hba.conf -exec cat {} +
could also make you dump the content of/dev/urandom
(for instance if someone created a symlink from/tmp/pg_hba.conf
to it and there's no easy portable way around that that doesn't at least have a race condition). -
lindhe almost 7 yearsLet's not use
sudo
"just in case", shall we? -
devinbost over 6 yearsWell, it's better than running
sudo rm -rf /
-
Kusalananda over 4 yearsNote that if
find
finds too many files, this would generate an "argument list too long" error whet trying to invokecat
. It would also have problems handling filenames containing whitespace characters and possibly also filenames containing filename globbing characters. -
mazunki over 4 yearsThe fact that I didn't know about
cat
's-n
option surprises me. I love reading random questions. -
Rajesh Chaudhary almost 4 yearsUnfortunately,
find /location -size 1033c -exec cat -n {} \;
only opens the first file.