Home > front end >  In bash, how can I find files using a .txt file in subdirectories of the current directory
In bash, how can I find files using a .txt file in subdirectories of the current directory

Time:12-16

I have a bunch of files with different names in different subdirectories. I created a txt file with those names but I cannot make find to work using the file. I have seen posts on problems creating the list, on not using find (do not understand the reason though). Suggestions? Is difficult for me to come up with an example because I do not know how to reproduce the directory structure. The following are the names of the files (just in case there is a formatting problem)

AO-169 
AO-170 
AO-171 

The best that I came up with is:

cat ExtendedList.txt | xargs -I {} find . -name {}

it obviously dies in the first Directory that it finds.

Thanks in advance.

EDIT: I also tried: ta="AO-169 AO-170 AO-171" and then find . -name $ta it complains find: AO-170: unknown primary or operator

CodePudding user response:

If you are trying to ask "how can I find files with any of these names in subdirectories of the current directory", the answer to that would look something like

xargs printf -- '-o\0-name\0%s\0' <ExtendedList.txt |
xargs -r0 find . -false

The -false is just a cute way to let the list of actual predicates start with "... or".

If the list of names in ExtendedList.txt is large, this could fail if the second xargs decides to break it up between -o and -name.

The option -0 is not portable, but should work e.g. on Linux or wherever you have GNU xargs.

If you can guarantee that the list of strings in ExtendedList.txt does not contain any characters which are problematic to the shell (like single quotes), you could simply say

sed "s/.*/-o -name '&'/" ExtendedList.txt |
xargs -r find . -false
  • Related