linux - 查找包含在名为的目录中的所有文件

标签 linux bash shell command-line find

我想递归地查找名称为“name1”或名称为“name2”的目录中包含的所有文件

例如:

structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name1/subfolder/file1s.a
structure/of/dir/name1/subfolder/file2s.b
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c
structure/of/dir/name2/subfolder/file1s.a
structure/of/dir/name2/subfolder/file2s.b
structure/of/dir/name3/name1.a ←this should not show up in the result
structure/of/dir/name3/name2.a ←this should not show up in the result

所以当我开始我的魔法命令时,预期的输出应该是这样的,而且只有这样:

structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c

我编写了一些脚本,但它不起作用,因为它在文件中搜索,而不仅仅是文件夹名称:

for entry in $(find $SEARCH_DIR -type f | grep 'name1\|name2');
    do
      echo "FileName: $(basename $entry)"
 done

最佳答案

如果您可以使用 -regex 选项,避免使用 [^/] 的子文件夹:

~$ find . -type f -regex ".*name1/[^/]*" -o -regex ".*name2/[^/]*"
./structure/of/dir/name2/file1.a
./structure/of/dir/name2/file3.c
./structure/of/dir/name2/subfolder
./structure/of/dir/name2/file2.b
./structure/of/dir/name1/file1.a
./structure/of/dir/name1/file3.c
./structure/of/dir/name1/file2.b

关于linux - 查找包含在名为的目录中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734957/

相关文章:

c - 如何在 Linux 上从汇编代码中调用 c 库?

linux - 即使安装新版本并删除旧版本后也无法修改 gcc 版本

Bash 按字面打印颜色代码而不是实际颜色

java - shell 脚本崩溃后可靠地重新启动 java 进程

python - "SyntaxError: Missing parentheses in call to ' print '. Did you mean print(' 安装ssl时寻找', f)?"

linux - 通过 cronjob 中的 bash 脚本进行 sftp : 'get' and 'rm' commands not found?

regex - txt文件删除url到最后 "/"得到文件

linux - Linux 的面向对象 shell?

linux - 将单个 csv 文件的前两列合并到另一列中

shell - ENTRYPOINT 与 CMD 结合