给定一个带有文件名的文本文件,我如何在当前目录的子目录中找到文件? [英] Given a text file with file names, how can I find files in subdirectories of the current directory?
问题描述
我在不同的子目录中有一堆不同名称的文件.我用这些名称创建了一个 txt 文件,但我无法让 find
使用该文件.我看过关于创建列表的问题,关于不使用 find
的帖子(虽然不明白原因).建议?我很难想出一个例子,因为我不知道如何重现目录结构.以下是文件名(以防出现格式问题)
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
我想出的最好的是:
cat ExtendedList.txt | xargs -I {} find . -name {}
它显然死在它找到的第一个目录中.
It obviously dies in the first directory that it finds.
我也试过
ta="AO-169 AO-170 AO-171"
find . -name $ta
但它抱怨 find: AO-170: unknown primary or operator
推荐答案
如果你想问我怎样才能在当前目录的子目录中找到具有这些名称的文件",答案看起来应该是喜欢
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 -name %s ' <ExtendedList.txt |
xargs -r0 find . -false
-false
只是让 actual 谓词列表以...或"开头的一种可爱方式.
The -false
is just a cute way to let the list of actual predicates start with "... or".
如果 ExtendedList.txt
中的名称列表很大,如果第二个 xargs
决定在 -o
和 -name
.
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
.
选项 -0
不可移植,但应该可以使用,例如在 Linux 或任何你有 GNU xargs
的地方.
The option -0
is not portable, but should work e.g. on Linux or wherever you have GNU xargs
.
如果你能保证ExtendedList.txt
中的字符串列表不包含任何对shell有问题的字符(比如单引号),你可以简单地说
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
这篇关于给定一个带有文件名的文本文件,我如何在当前目录的子目录中找到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!