在主文件夹中搜索文件

时间:2019-01-09 05:51:14

标签: perl unix find

我有一个主结果文件夹,其中有多个 config.txt 文件。 但是,我只需要在给出的主结果文件夹中的 netlist 子文件夹中获取 config.txt 文件的路径。

我已经尝试了这段代码,但是没有成功。

find $mainResultPath -name "config.txt"

这是代码段,假设我们已经在 $ mainResultPath 中了 Code snippet and reqd files ticked

1 个答案:

答案 0 :(得分:0)

这应该有效:

find $mainResultPath -type d -name netlist | \
   xargs -I {} find {} -maxdepth 1 -type f -name config.txt

即首先过滤掉netlist目录,然后在其中搜索config.txt文件。

我的测试的示例输出:

$ find $HOME/Documents -type f -name config.txt
/home/.../Documents/Downloads/RFC/netlist/config.txt
/home/.../Documents/config.txt
/home/.../Documents/netlist/config.txt

$ find $HOME/Documents -type d -name netlist
/home/.../Documents/Downloads/RFC/netlist
/home/.../Documents/Downloads/netlist
/home/.../Documents/netlist

$ find $HOME/Documents -type d -name netlist | xargs -I {} find {} -maxdepth 1 -name config.txt
/home/.../Documents/Downloads/RFC/netlist/config.txt
/home/.../Documents/netlist/config.txt

编辑2:如果您不喜欢第二个发现,那么也可以

$ find $HOME/Documents -type d -name netlist | \
     while read _d; do [ -f "${_d}/config.txt" ] && echo "${_d}/config.txt"; done