I am doing a find
and then getting a list of files. How do I pipe it to another utility like cat
(so that cat displays the contents of all those fi
To list and see contents of all abc.def files on a server in the directories /ghi and /jkl
find /ghi /jkl -type f -name abc.def 2> /dev/null -exec ls {} \; -exec cat {} \;
To list the abc.def files which have commented entries and display see those entries in the directories /ghi and /jkl
find /ghi /jkl -type f -name abc.def 2> /dev/null -exec grep -H ^# {} \;
Sounds like a job for a shell script to me:
for file in 'find -name *.xml'
do
grep 'hello' file
done
or something like that
To achieve this (using bash) I would do as follows:
cat $(find . -name '*.foo')
This is known as the "command substitution" and it strips line feed by default which is really convinient !
more infos here
Here is my shot for general use:
grep YOURSTRING `find .`
It will print the file name
In bash, the following would be appropriate:
find /dir -type f -print0 | xargs -0i cat {} | grep whatever
This will find all files in the /dir
directory, and safely pipe the filenames into xargs
, which will safely drive grep
.
Skipping xargs
is not a good idea if you have many thousands of files in /dir
; cat
will break due to excessive argument list length. xargs
will sort that all out for you.
The -print0
argument to find
meshes with the -0
argument to xargs
to handle filenames with spaces properly. The -i
argument to xargs
allows you to insert the filename where required in the cat
command line. The brackets are replaced by the filename piped into the cat
command from find
.
I use something like this:
find . -name <filename> -print0 | xargs -0 cat | grep <word2search4>
"-print0
" argument for "find" and "-0
" argument for "xargs" are needed to handle whitespace in file paths/names correctly.