Shell脚本以递归方式列出包含具有特定扩展名的文件的文件夹中的所有子目录

时间:2022-05-27 07:09:12

I have a folder named a, and it may have one or multiple sub-directories and the sub-directories may have multiple sub-directories and so on. I want to know how can I write a shell script to list all the sub-directories that contain a file with specific extension.

我有一个名为a的文件夹,它可能有一个或多个子目录,子目录可能有多个子目录,依此类推。我想知道如何编写shell脚本来列出包含具有特定扩展名的文件的所有子目录。

So, it may be like

所以,它可能就像

A -> B -> C

  -> D -> f2.txt

       -> F -> f3.txt

  -> E -> G -> H -> f4.txt

So only D, F and H directories will be listed. Actually I need this as a quick way to find the package names of particular java classes, by listing their directory tree. For example in the previous example A.E.G.H is a package same as A.D, but A.E.G is not a package as G only contains a sub directory H no files.

因此,只列出D,F和H目录。实际上我需要通过列出它们的目录树来快速查找特定java类的包名。例如,在前面的示例中,A.E.G.H是与A.D相同的包,但A.E.G不是包,因为G仅包含子目录H无文件。

3 个解决方案

#1


3  

I think you need find command. You can use like this,

我想你需要找到命令。你可以这样使用,

find . -name '*.txt'

Here,

. - dot(.) is current directory. You can also specify the path where to start.

。 - 点(。)是当前目录。您还可以指定从哪里开始的路径。

-name - file name to find.

-name - 要查找的文件名。

You can also use -maxdepth option to limit the depth of recursive finding. Normally, find will find the file recursively.

您还可以使用-maxdepth选项来限制递归查找的深度。通常,find会递归地找到该文件。

Update:

If you want to list out the directories,

如果要列出目录,

find . -name '*.txt' -exec dirname {} \;

#2


1  

find . -name '*.txt' -printf '%h\n'

找 。 -name'* .txt'-printf'%h \ n'

man find will give other possible format flags usable with -printf

man find将给出与-printf一起使用的其他可能的格式标志

#3


0  

A simpler and faster alternative for systems like MAC. No need to use 2 additional tools and even repeatingly call dirname:

对于像MAC这样的系统,更简单,更快速的替代方案无需使用2个额外工具,甚至可以重复调用dirname:

find -type f -name '*.txt' | awk 'BEGIN{FS=OFS="/"}{--NF}!a[$0]++'

-type f may be removed optionally if it doesn't work.

如果不起作用,可以选择性地去除-type。

#1


3  

I think you need find command. You can use like this,

我想你需要找到命令。你可以这样使用,

find . -name '*.txt'

Here,

. - dot(.) is current directory. You can also specify the path where to start.

。 - 点(。)是当前目录。您还可以指定从哪里开始的路径。

-name - file name to find.

-name - 要查找的文件名。

You can also use -maxdepth option to limit the depth of recursive finding. Normally, find will find the file recursively.

您还可以使用-maxdepth选项来限制递归查找的深度。通常,find会递归地找到该文件。

Update:

If you want to list out the directories,

如果要列出目录,

find . -name '*.txt' -exec dirname {} \;

#2


1  

find . -name '*.txt' -printf '%h\n'

找 。 -name'* .txt'-printf'%h \ n'

man find will give other possible format flags usable with -printf

man find将给出与-printf一起使用的其他可能的格式标志

#3


0  

A simpler and faster alternative for systems like MAC. No need to use 2 additional tools and even repeatingly call dirname:

对于像MAC这样的系统,更简单,更快速的替代方案无需使用2个额外工具,甚至可以重复调用dirname:

find -type f -name '*.txt' | awk 'BEGIN{FS=OFS="/"}{--NF}!a[$0]++'

-type f may be removed optionally if it doesn't work.

如果不起作用,可以选择性地去除-type。