开发者

How can I find all immediate sub-directories of the current directory on Linux?

开发者 https://www.devze.com 2023-02-06 06:20 出处:网络
How can I find all immediate sub-directories of the c开发者_JS百科urrent directory on Linux?The simplest way is to exploit the shell globbing capabilities by writing echo */.

How can I find all immediate sub-directories of the c开发者_JS百科urrent directory on Linux?


The simplest way is to exploit the shell globbing capabilities by writing echo */.

If you like to use ls, e.g. to apply formatting/sorting options, make it ls -d */.

Explanation:

  • The slash ensures that only directories are considered, not files.
  • Option -d: list directories themselves, not their contents


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.

It's able to find anything in a directory tree.

If by immediate you 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:

find YOUR_DIRECTORY -type d -maxdepth 1 -mindepth 1


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.


Use this

ls | grep /$

the grep find anything ending in / which directories do.


I came here because I was using find . -type d and it was matching the current directory and I only wanted subdirectories, and find ./* -type d worked great.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消