abualiga
abualiga

Reputation: 35

combining grep and find to search for file names from query file

I've found many similar examples but cannot find an example to do the following. I have a query file with file names (file1, file2, file3, etc.) and would like to find these files in a directory tree; these files may appear more than once in the dir tree, so I'm looking for the full path. This option works well:

find path/to/files/*/*  -type f | grep -E "file1|file2|file3|fileN"  

What I would like is to pass grep a file with filenames, e.g. with the -f option, but am not successful. Many thanks for your insight.

This is what the query file looks like: so the file contains one column of filenames separated by '\n' and here is how it looks like:

103128_seqs.fna
7010_seqs.fna
7049_seqs.fna
7059_seqs.fna
7077A_seqs.fna
7079_seqs.fna

Upvotes: 1

Views: 123

Answers (1)

G. Cito
G. Cito

Reputation: 6378

grep -f FILE gets the patterns to match from FILE ... one per line*:

cat files_to_find.txt
n100079_seqs.fna
103128_seqs.fna
7010_seqs.fna
7049_seqs.fna
7059_seqs.fna
7077A_seqs.fna
7079_seqs.fna

Remove any whitespace (or do it manually):

perl -i -nle 'tr/ //d; print if length' files_to_find.txt

Create some files to test:

touch `cat files_to_find.txt`

Use it:

find ~/* -type f | grep -f files_to_find.txt

output:

/home/user/tmp/7010_seqs.fna
/home/user/tmp/103128_seqs.fna
/home/user/tmp/7049_seqs.fna
/home/user/tmp/7059_seqs.fna
/home/user/tmp/7077A_seqs.fna
/home/user/tmp/7079_seqs.fna
/home/user/tmp/n100079_seqs.fna

Is this what you want?

Upvotes: 1

Related Questions