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.
I know there is already a selected answer, but you can get the requested behavior with just
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.
No, but a simple
find command will do it:
find . -type d -depth 1
ls -F | grep /
You could then alias either one if necessary.
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.
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
zsh (as found by default on macOS, it even used to be
/bin/sh there), you'd use glob qualifiers to select files based on their type:
List non-hidden directories:
ls -d -- *(/)
List all directories:
ls -d -- *(D/)
.. are always excluded, add them individually if you want them)
Also include symlinks to directories:
ls -d -- *(D-/)
- makes so further qualifiers apply after symlink resolution).
I think ls has a bug on Mac OS X. A workaround is to use grep...
ls -l / | grep "^d"
$ ls -p | grep /
-p flag will make
ls add a slash (`/') after each filename if that file is a directory.
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 -CF /dir1 /dir2 /dir3 /beaches /Work /Other
this will show your directories in blue