【问题标题】:Find a file with an extension .abc, .cde and .fgh and a folder WITHIN a folder查找扩展名为 .abc、.cde 和 .fgh 的文件以及文件夹内的文件夹
【发布时间】:2020-10-16 19:03:29
【问题描述】:

我想查找具有 3 个扩展名的文件,即 .abc、.cde 和 .fgh。我还想在包含我提到的文件扩展名的文件的目录中找到一个名为“Bigone”的文件夹,并使用myscript. 对它们执行操作。我应该使用什么命令?帮忙。谢谢。

【问题讨论】:

  • 到目前为止您尝试过什么?请添加更多详细信息,尤其是关于 使用 myscript 对它们执行操作 部分
  • 找到 -E 。 -regex '.*\.(abc|cde|fgh)' -print0 | while read -d $'\0' lp myscript "$lp" 这可以找到具有上述扩展名的文件。我也想找到“Bigone”文件夹。
  • 那你为什么不把它添加到正则表达式中呢?
  • 我不知道要添加我想查找的文件夹名称。就像可以有“Bigone”、“Bigone 2”等。如何修改命令?帮忙。谢谢。
  • 在您的帖子中添加您尝试过的内容以及您在尝试时遇到的问题。

标签: bash shell terminal parameter-passing


【解决方案1】:

您打算构建以下内容吗?
作为准备,我设置了一个简单的文件夹结构,其中包含您提到的一些文件:

~/$ find . -type f \( -name "*.abc" -o -name "*.cde" -o -name "*.fgh" \)
./tmp1/somefile.cde
./tmp1/somefile.fgh
./tmp1/somefile.abc
./tmp2/somefile.abc
~/$ find . -type d -name Bigone
./tmp2/Bigone

此外,我使用以下内容模拟您的脚本:

~/$ cat myscript 
#!/bin/bash
f=$1
echo "Performing action on file/folder \"$f\""

请注意,我还考虑了文件夹包含子文件夹“Bigone”的可能性(在这种情况下该命令不应失败)。下面是我构建的包装脚本...

~/$ cat ./mywrapper 
#!/bin/bash
find . -type f \( -name "*.abc" -o -name "*.cde" -o -name "*.fgh" \) | xargs -n1 ./myscript
find . -type f \( -name "*.abc" -o -name "*.cde" -o -name "*.fgh" \) | xargs dirname | uniq | xargs -n1 -I{} find {} -type d -name Bigone | xargs -n1 ./myscript

...及其运行时的输出:

~/$ ./mywrapper 
Performing action on file/folder "./tmp1/somefile.cde"
Performing action on file/folder "./tmp1/somefile.fgh"
Performing action on file/folder "./tmp1/somefile.abc"
Performing action on file/folder "./tmp2/somefile.abc"
Performing action on file/folder "./tmp2/Bigone"

find 在这里做的工作最多,其余的只是将结果传送到您的“myscript”。

【讨论】:

  • 我想在所有我提到的扩展名的文件和名为“Bigone”的文件夹上运行 myscript。如果我使用您的第一个建议,它只会在它找到并退出的前两种类型的扩展上运行。我希望它在所有具有特定扩展名和文件夹的文件上运行。
  • 哦,对不起,我首先误解了你。我已编辑答案以反映您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多