I want to list only the directories in specified path (ls doesn't have such option).
Also, can this be done with a single line command?
I want to list only the directories in specified path (ls doesn't have such option).
Also, can this be done with a single line command?
The following
find * -maxdepth 0 -type d
basically filters the expansion of '*', i.e. all entries in the current dir, by the -type d condition.
Advantage is that, output is same as ls -1 *, but only with directories
and entries do not start with a dot
ls -l | grep '^d'
You can make an alias and put it into the profile file
alias ld="ls -l| grep '^d'"
In bash:
ls -d */
Will list all directories
ls -ld */
will list all directories in long form
ls -ld */ .*/
will list all directories, including hidden directories, in long form.
I have recently switched to zsh (MacOS Catalina), and found that:
ls -ld */ .*/
no longer works if the current directory contains no hidden directories.
zsh: no matches found: .*/
It will print the above error, but also will fail to print any directories.
ls -ld *(/) .*(/)
Also fails in the same way.
So far I have found that this:
ls -ld */;ls -ld .*/
is a decent workaround. The ; is a command separator. But it means that if there are no hidden directories, it will list directories, and still print the error for no hidden directories:
foo
bar
zsh: no matches found: .*/
ls is the shell command for list contents of current directory
-l is the flag to specify that you want to list in Longford (one item per line + a bunch of other cool information)
-d is the flag to list all directories "as files" and not recursively
*/ is the argument 'list all files ending in a slash'
* is a simple regex command for "anything", so */ is asking the shell to list "anything ending in '/'"
See man ls for more information.
I put this:
alias lad="ls -ld */;ls -ld .*/"
in my .zshrc, and it seems to work fine.
NOTE: I've also discovered that
ls -ld .*/ 2> /dev/null
doesn't work, as it still prints sterr to the terminal. I'll update my answer if/when I find a solution.
Here's another solution that shows linked directories. I slightly prefer it because it's a subset of the "normal" ls -l output:
ls -1d */ | rev | cut -c2- | rev | xargs ls -ld --color=always
This is the answer most people will want.
ls -l | grep -E '^d' | awk '{print $9}'
The directory names, and nothing but the directory names.
I find there are many good answers listed before me. But I would like to add a command which we already use it several time, and so very easy to list all the directories with less effort:
cd
(Note: After cd give a space) and press tab twice, it will list only all the directories in current working directory. Hope this is easy to use. Please let me know if there is any problem with this. Thanks.
du -d1 is perhaps the shortest option. (As long as you don't need to pipe the input to another command.)