如何在 Linux 上找到当前目录的所有直接子目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4747905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find all immediate sub-directories of the current directory on Linux?
提问by vinoth
How can I find all immediate sub-directories of the current directory on Linux?
如何在 Linux 上找到当前目录的所有直接子目录?
回答by peoro
If you just need to get a list of sub directories (without caring about the language/tool to use) find
is the command that you need.
如果您只需要获取子目录列表(而不关心要使用的语言/工具)find
就是您需要的命令。
It's able to find anything in a directory tree.
它能够在目录树中找到任何东西。
If by immediateyou mean that you need only the child directories, but not the grandchild -maxdepth
option will do the trick. Then -type
will let you specify that you are only looking for directories:
如果立即表示您只需要子目录,但不需要孙子-maxdepth
选项就可以了。然后-type
让您指定您只查找目录:
find YOUR_DIRECTORY -type d -maxdepth 1 -mindepth 1
回答by ypnos
The simplest way is to exploit the shell globbing capabilities by writing echo */
.
最简单的方法是通过编写echo */
.
If you like to use ls
, e.g. to apply formatting/sorting options, make it ls -d */
.
如果您喜欢使用ls
,例如应用格式/排序选项,请将其设为ls -d */
。
Explanation:
解释:
- The slash ensures that only directories are considered, not files.
- Option
-d
: list directories themselves, not their contents
- 斜线确保只考虑目录,而不是文件。
- 选项
-d
:列出目录本身,而不是它们的内容
回答by rapadura
Use this
用这个
ls | grep /$
ls | 格雷普/$
the grep find anything ending in / which directories do.
grep 找到任何以 / 结尾的目录。
回答by Sumit Burnwal
You can also use the below -
您也可以使用以下 -
$ ls -l | grep '^d'
Brief explanation: As in long listing, the directories start with 'd', so the above command (grep
) filters out those result, that start with 'd', which are nothing but directories.
简要说明:在长列表中,目录以'd'开头,所以上面的命令(grep
)过滤掉了那些以'd'开头的结果,它们只是目录。
回答by MalcolmOcean
I came here because I was using find . -type d
and it was matching the current directory and I onlywanted subdirectories, and find ./* -type d
worked great.
我来到这里是因为我正在使用find . -type d
它并且它与当前目录匹配,我只想要子目录,并且find ./* -type d
效果很好。
Although it looks like that also finds sub-sub-dirs etc, which wasn't an issue for my situation. There's probably another flag for that.
虽然看起来也可以找到子子目录等,但这对我的情况来说不是问题。可能还有另一个标志。