Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

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

like image 415
vinoth Avatar asked Jan 20 '11 13:01

vinoth


People also ask

What will show you all the sub directories of the current directory?

Typing just "ls" will give you a list of all the files and subdirectories in the current directory.

How do I list all subfolders in a directory?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory. WARNING: This can take a while if you have a large directory.

Which command will find all the sub directories with directories?

The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.


3 Answers

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
like image 52
ypnos Avatar answered Oct 01 '22 17:10

ypnos


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
like image 39
peoro Avatar answered Oct 01 '22 16:10

peoro


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.

like image 29
Sumit Burnwal Avatar answered Oct 01 '22 16:10

Sumit Burnwal