How can I find all immediate sub-directories of the current directory on Linux?
Solution 1
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
Solution 2
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
Solution 3
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.
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
How can I find all immediate sub-directories of the current directory on Linux?