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 communityI 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.
/
on the end--but now that I see you answer it makes sense. After all, tab-completion always adds it.
– gvkv
Sep 6 '10 at 3:46
find
is actually a good choice in that case.
– biocyberman
Jan 31 '17 at 20:59
find
also has the advantage when the number of directories exceeds the maximum argument length.
– Steven D
Feb 1 '17 at 9:31
No, but a simple find
command will do it:
find . -type d -depth 1
or grep
ls -F | grep /
You could then alias either one if necessary.
-maxdepth 1
. Also find
wanted me to flip the order: find . -maxdepth 1 -type d
– Brad Cupit
Aug 28 '15 at 14:33
ls -F | grep /
solution works wonders! It seems to be the only one I can get to work on my FreeBSD machine. I think using Fish means that anything with */
may not work?
– cjm
Jul 27 '16 at 4:38
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 .
and ..
)
ls -a
to include directory entries whose names begin with a dot (.), which avoids the annoyance of matching .
and ..
.
– nclark
Feb 3 '20 at 14:50
With 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/)
(.
and ..
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).
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/
$ ls -p | grep /
The -p
flag will make ls
add a slash (`/') after each filename if that file is a directory.
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:
--
to protect against interpreting directory names starting with a dash as options.
– Kusalananda♦
Jan 2 '20 at 21:32
zsh
solution just above, you see it is a shell thing about extended pattern matching. Where is that */
notation in bash defined?
– user373503
Jan 3 '20 at 17:12
-l
in the current directory.
– Kusalananda♦
Jan 3 '20 at 17:20
I think ls has a bug on Mac OS X. A workaround is to use grep... ls -l / | grep "^d"
grep "^d"
hack
– Michael Mrozek
Sep 6 '10 at 2:10
ls -l /home
and ls -dl /home
– Steven D
Sep 6 '10 at 2:11
ls -l / | grep ^d
. If you are going to use quotes, since you don't need parameter expansion, make them single quotes. ls -l / | grep '^d'
. Yes, I'm a pedant. But really, do this. :)
– Wildcard
Nov 1 '16 at 1:08
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
ls
implementations I know and in the POSIX specification of ls
, -C
is to output in columns, not to skip non-directory files. In your example, the /
are printed before the file names, which no ls
implementation would do. With -F
, /
is appended to the file name for directory files.
– Stéphane Chazelas
Jun 21 '18 at 16:15
ls -G
this will show your directories in blue
ls -d
gives only.
and why the*/
must be added to the end to make it work? – cwd Oct 4 '11 at 2:49ls
defaults to.
. And-d
means don't print the directory's contents, print the directory itself. – Mikel May 23 '12 at 14:52ls -p
it shows the/
after the directory names. So*/
is just a pattern which is matched against the directory name and/
combo. – Pitt Oct 24 '12 at 15:55