find with grep not returning results - grep

The first line returns dozens of finds. The second line returns nothing.
sudo find /var/www/ . -type 'f' -name index.php | grep "php"
sudo find /var/www/ . -type 'f' -name index.php | grep "require"
There are files called index.php that contain both text strings. Why doesn't the second line seem to work?

You want to be calling xargs before piping to grep. What you have is grep searching the results of find, not the contents of the files that find has found.
sudo find /var/www/ . -type 'f' -name index.php | xargs grep "php"
If you do lots of searching like this, you may want to look at ack. For instance, to search all the PHP files in /var/www for "require", you would use:
ack --php require /var/www

Related

Using find recursively and only returning the matching file names

I am trying to find all instances of a string within files - I am using find and it works great, however, it returns not only the file but every instance of that string within the file which results in a huge long list whereas I really only want the file name.
I am using:
find . -name '*.php' -exec grep -i 'MATCH' {} \; -print
This will show me every instance of MATCH and then the file name then the next batch and the filename so something like:
MATCH
MATCH
MATCH
./filename
MATCH
MATCH
MATCH
./filename2
I tried changing GREP to:
find . -name '*.php' -exec grep -H -i 'MATCH' {} \; -print
and this then gave me:
./filename: MATCH
./filename: MATCH
./filename: MATCH
./filename2: MATCH
./filename2: MATCH
./filename2: MATCH
however this still results in the same number of lines being shown all be it slightly differently laid out.
I tried changing GREP to:
find . -name '*.php' -exec grep -l -i 'MATCH' {} \; -print
and this then gave me:
./filename
./filename
./filename
./filename2
./filename2
./filename2
Ideally I would like something like:
./filename
./filename2
which only lists each of the file which match once regardless of how many times it appears in each file - can this be done?
Use the features provided by grep:
-l, --files-with-matches print only names of FILEs containing matches
-R, -r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
-i, --ignore-case ignore case distinctions
I.e.
grep -ril --include="*.php" 'MATCH' .
It's not the grep that's the problem, you're telling find to print every file name:
find . -name '*.php' -exec grep -l -i 'MATCH' {} \; -print
Note the -print at the end. Just remove that:
find . -name '*.php' -exec grep -l -i 'MATCH' {} \;
Look:
$ echo 'foo' > file1
$ echo 'foo' > file2
$ find . -name 'file*' -exec grep -l -i foo {} \; -print
./file1
./file1
./file2
./file2
$ find . -name 'file*' -exec grep -l -i foo {} \;
./file1
./file2

How to grep for filenames found by find in other files?

How can I grep for the result of find within another pattern?
That's how I get all filenames with a certain pattern (in my case ending with "ext1")
find . -name *ext1 -printf "%f\n"
And then I want to grep for these filenames with another pattern (in my case ending on "ext2"):
grep -r '[filname]' *ext2
I tried with
find . -name *ext1 -printf "%f\n" | xargs grep -r *ext2
But this only makes grep tell me that it can not find the files found by find.
You would tell grep that the patterns are in a file with the -f option, and use the "stdin filename" -:
find ... | grep -r -f - *ext2

Delete a list of files with find and grep

I want to delete all files which have names containing a specific word, e.g. "car".
So far, I came up with this:
find|grep car
How do I pass the output to rm?
find . -name '*car*' -exec rm -f {} \;
or pass the output of your pipeline to xargs:
find | grep car | xargs rm -f
Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.
To view what you are going to delete first, since rm -fr is such a dangerous command:
find /path/to/file/ | grep car | xargs ls -lh
Then if the results are what you want, run the real command by removing the ls -lh, replacing it with rm -fr
find /path/to/file/ | grep car | xargs rm -fr
I like to use
rm -rf $(find . | grep car)
It does exactly what you ask, logically running rm -rf on the what grep car returns from the output of find . which is a list of every file and folder recursively.
You can use ls and grep to find your files and rm -rf to delete the files.
rm -rf $(ls | grep car)
But this is not a good idea to use this command if there is a chance of directories or files, you don't want to delete, having names with the character pattern you are specifying with grep.
You really want to use find with -print0 and rm with --:
find [dir] [options] -print0 | grep --null-data [pattern] | xargs -0 rm --
A concrete example (removing all files below the current directory containing car in their filename):
find . -print0 | grep --null-data car | xargs -0 rm --
Why is this necessary:
-print0, --null-data and -0 change the handling of the input/output from parsed as tokens separated by whitespace to parsed as tokens separated by the \0-character. This allows the handling of unusual filenames (see man find for details)
rm -- makes sure to actually remove files starting with a - instead of treating them as parameters to rm. In case there is a file called -rf and do find . -print0 | grep --null-data r | xargs -0 rm, the file -rf will possibly not be removed, but alter the behaviour of rm on the other files.
This finds a file with matching pattern (*.xml) and greps its contents for matching string (exclude="1") and deletes that file if a match is found.
find . -type f -name "*.xml" -exec grep exclude=\"1\" {} \; -exec rm {} \;
Most of the other solutions presented here have problems with handling file names with spaces in them. Here's a solution that handles spaces properly.
grep -lRZ car . | xargs -0 rm
Notes on arguments used:
-l tells grep to print only filenames
-R enables grep recursive search in subfolders
-Z tells grep to separate results by \0 instead of \n
-0 tells xargs to separate input arguments by \0 instead of whitespace
car is the regular expression to search for
. is the folder where to search
Can also use rm -f to force the removal (as usual).
A bit of necromancy, but you can also use find, grep, and xargs
find . -type f | grep -e "pattern1" -e "pattern2" | xargs rm -rf
^ Find will need some attention to make it work for your needs potentially, such as is a file, mindepth, maxdepth and any globbing.
when find | grep car | xargs rm -f get results:
/path/to/car
/path/to/car copy
some files which contain whitespace will not be removed.
So my answer is:
find | grep car | while read -r line ; do
rm -rf "${line}"
done
So the file contains whitespace could be removed.
find start_dir -iname \*car\* -exec rm -v {} \;
I use:
find . | grep "car" | while read i; do echo $i; rm -f "$i"; done
This works even if there are spaces in the filename as well as in recursive manner, searching for directories as well.
Use rm with wildcard *
rm * will delete all files
rm *.ext will delete all files which have ext as extension
rm word* will delete all files which starts with word.

find grep exclude some file names for dos2unix

so far I have gotten this far:
prompt$ find path/to/project -type f | grep -v '*.ori|*.pte|*.uh|*.mna' | xargs dos2unix 2> log.txt
However, the files with extensions .ori, .pte, .uh and .mna still show up.
It is better to leave the excluding to find, see Birei's answer.
The problem with your grep pattern is that you have specified it as a shell glob. By default grep expects basic regular expressions (BRE) as its first argument. So if you replace your grep pattern with: .*\.\(ori\|pte\|uh\|mna\)$ it should work. Or if you would rather use extended regular expressions (ERE), you can enable them with -E. Then you can express the same exclusion like this: .*\.(ori|pte|uh|mna)$.
Full command-line:
find . -type f | grep -vE '.*\.(ori|pte|uh|mna)$'
One way:
find path/to/project *.* -type f ! \( -name '*.ori' -o -name '*.pte' -o -name '*.uh' -o -name '*.mna' \)
| xargs dos2unix 2> log.txt

To understand recursive grep in xargs

What is the practical difference between the following two commands?
Command A
find . -type f -print0 | xargs -0 grep -r masi
Command B
find . -type f -print0 | xargs -0 grep masi
In short, what is the practical benefit of Command A?
None .. -r is for recursively searching directories, but the -type f will prevent find from returning directory names.
I think none
The A will try to recurse over file names (as the find is only searching for files) so it will not recurse into anything...

Resources