简体   繁体   中英

How to grep/find for a list of file names?

So for example, I have a text document of a list of file names I may have in a directory. I want to grep or use find to find out if those file names exist in a specific directory and the subdirectories within it. Current I can do it manually via find . | grep filename find . | grep filename find . | grep filename but that's one at a time and when I have over 100 file names I need to check to see if I have them or not that can be really pesky and time-consuming.

What's the best way to go about this?

xargs is what you want here. The case is following:

Assume you have a file named filenames.txt that contains a list of files

a.file 
b.file 
c.file 
d.file 
e.file

and only e.file doesn't exist.

the command in terminal is:

cat filenames.txt | xargs -I {} find . -type f -name {}

the output of this command is:

a.file
b.file
c.file
d.file

Maybe this is helpful.

If the files didn't move, since the last time, updatedb ran, often < 24h, your fastest search is by locate.

Read the filelist into an array and search by locate. In case the filenames are common (or occur as a part of other files), grep them by the base dir, where to find them:

< file.lst mapfile filearr
locate ${filearr[@]} | grep /path/where/to/find

If the file names may contain whitespace or characters, which might get interpreted by the bash, the usual masking mechanisms have to been taken.

A friend had helped me figure it out via find . | grep -i -Ff filenames.txt find . | grep -i -Ff filenames.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM