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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
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.
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).
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!
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