gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:31:39 26 4
gpt4 key购买 nike

我想递归地查找名称为“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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com