Is there any option with ‘ls’ command that I see only the directories?

Sometimes, I need to check only the directories not files. Is there any option with the command ls? Or is there any utility for doing that?

EDIT: I’m using Mac OS X, and ls -d gives me . even though I have directories.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I know there is already a selected answer, but you can get the requested behavior with just ls:

ls -ld -- */

(Note that the ‘–‘ marks the end of parameters, preventing folder names beginning with a hyphen from being interpreted as further command options.)

This will list all the non-hidden (unless you configure your shell’s globs to expand them) directories in the current working directory where it is run (note that it also includes symbolic links to directories). To get all the subdirectories of some other folder, just try:

ls -ld /path/to/directory/*/

Note that the -l is optional.

Method 2

No, but a simple find command will do it (here using the -{min,max}depth GNU extensions, also found on most implementations theses days):

find . -mindepth 1 -maxdepth 1 -type d

Or the POSIX (standard) equivalent:

find . ! -name . -prune -type d

On FreeBSD and some of its derivatives (including macOS), you can also do:

find . -depth 1 -prune -type d

Those also include hidden directories (not the . or .. special directories) and don’t sort the list of files. It also adds a ./ prefix to each file which with the GNU implementation of find, you can remove by adding -printf '%Pn'.

or grep (assuming file names don’t contain newline characters):

ls -p | grep /

(add the -A option to ls to include hidden ones).

You could then alias either one if necessary.

Method 3

I also needed to view hidden directories so have modified the suggestion above to fit my needs

ls -d -- */ .*/

(depending on the shell, that may also include . and ..)

Method 4

I like the tree utility to get an overview over the directory structure. It’s available in MacPorts and all Linux distributions I’ve tried.

tree -d -L 2

That would show all directories, two levels deep.

Method 5

With zsh (as found by default on macOS, it even used to be /bin/sh there, and it’s the default login shell in newer versions), you’d use glob qualifiers to select files based on their type:

  • List non-hidden directories:
    ls -ld -- *(/)
  • List all directories:
    ls -ld -- *(D/)

    (. and .. are always excluded, add them individually if you want them)

  • Also include symlinks to directories:
    ls -ld -- *(D-/)

    (- makes so further qualifiers apply after symlink resolution).

Here, using -l to get a long listing. For printing the list of matching files, you don’t need ls, you can replace ls -ld with print -rC1 (print raw on 1 Column, print being builtin). You may also want to add the N glob qualifier so as to print nothing if there’s no matching file instead of reporting an error:

print -rC1 -- *(ND/)

Method 6

There is not just one option to list directories…

But you can use -d (list directories themselves, not their contents) and */ to match directories themselves:

ls -d */

And try to use the dot, for hidden ones, ls -d .*/.

Just for fun, try: ls -d and ls */. The differences will be clear!

Ref:

Method 7

Use ls command like this

❯ ls -1d plugins/*/
plugins/localizable/
plugins/redmine_agile/
plugins/redmine_gitlab_hook/
plugins/redmine_mermaid_macro/
plugins/redmine_open_links_in_new_window/

Method 8

$ ls -p | grep /

The -p flag will make ls add a slash (`/’) after each filename if that file is a directory.

Method 9

I think ls has a bug on Mac OS X. A workaround is to use grep… ls -l / | grep "^d"

Method 10

ls -G

this will show your directories in blue

Method 11

The easiest way is to type the following command. This works across most UNIX and Linux platforms and versions. You can skip the -F if you want, but it is the argument that adds the / to the end of the directory name. The -C argument captures only directory names – all of them in the current directory. If you want to see only directories and subdirectories in the current path, simply add the -R argument (ls -CFR).

# ls -CF
/dir1  /dir2  /dir3  /beaches  /Work  /Other


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x