jfreak53
jfreak53

Reputation: 2359

Find all files, then grep all files for those names

I want to run a command on a directory to print out all files in that directory, then grep all files recursively for those found file names, basically to check and see if X file is used in any PHP or HTML files in a directory. If X file is found to NOT be in any files, print it's name.

I have this so far, I can't figure out how to pass to grep what I have then print those without results.

find . -path ./images -prune -o -name '*' -type f -print | sed 's!.*/!!'

I could use exec as so:

find . -path ./images -prune -o -name '*' -type f -exec grep -R '{}' ./* \;

But then I can't sed away the ./dir/etc/ from in front of the file names that find gives me.

The other part of the query that is perplexing me is I can't figure out how to get grep or find to print those without results in grep.

Upvotes: 2

Views: 2605

Answers (2)

Chris Seymour
Chris Seymour

Reputation: 85845

Use -exec to call basename and pipe to grep:

find ./images -maxdepth 1 -type f -exec basename {} \; | grep ...

This will print just the filenames of all the files found in ./images.

Upvotes: 1

Dave Rager
Dave Rager

Reputation: 8160

I think something like this might help:

$ find . -type f -printf "%f\n" | grep --color -R -f - .

In this example, -printf "%f\n" causes find to return only the file name without the path. The result is a file used as the search patterns for grep and is passed as input on stdin using -f -. The -R tells grep to search recursively (which you already know). The --color makes things look nice.

To list only the matching file names use grep -l -R -f - .. Then to find the files without results, you will have to find the files that are not in that list.

This post might help you with that.

Upvotes: 3

Related Questions