Zez
Zez

Reputation: 135

Find filenames matching lines in a text file

I have a text file as follows:

orthogroup1
orthogroup4
...

And I have a directory with text files as follows:

orthogroup1.faa.reconciled
orthogroup2.faa.reconciled
orthogroup3.faa.reconciled
orthogroup4.faa.reconciled
...

I want to use the text file to get the corresponding filenames in my directory.

The result should be something like:

orthogroup1.faa.reconciled
orthogroup4.faa.reconciled

How can I do this?

Thanks in advance :)

Upvotes: 0

Views: 102

Answers (3)

Max S.
Max S.

Reputation: 1

Something like this:

suffix='.faa.reconciled'
for line in $(cat file.txt); do
    echo $line$suffix
done

Example:

$ cat file.txt
orthogroup1
orthogroup4

$ ls -l ortho*
orthogroup4.faa.reconciled
orthogroup3.faa.reconciled
orthogroup2.faa.reconciled
orthogroup1.faa.reconciled

$ suffix='.faa.reconciled'; for line in $(cat file.txt); do echo $line$suffix; done
orthogroup1.faa.reconciled
orthogroup4.faa.reconciled

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53573

The -f option of grep allows you to take patterns from a file. Use that on the output of find to filter out the list:

find /path/to/dir -type f | grep -f /path/to/patterns

Upvotes: 1

zzzbra
zzzbra

Reputation: 123

Can you define what you mean by "get the filenames in my directory"? What next step do you have in mind for them?

Seems like you probably want to use something like sed or awk, although the latter is probably overkill. See What is the difference between sed and awk? for more information.

Upvotes: 0

Related Questions