Reputation: 165
What is the easiest way to get a list of all hidden and non-hidden directories (only directories) in a single call?
Obviously I can do it by connecting 2 different commands with &&
like this:
ls -d ./.*/ && ls -d ./*/
but shouldn't there be a better way?
EDIT: I do not want the current directory to be included in the list.
Also, ls -d ./.*/ ./*/
is a better alternative to what I have up there.
Upvotes: 0
Views: 2068
Reputation: 10982
If you have the tree
command, you can get a nice view of your directories:
tree -a -d -L 3
where:
Output example:
.
├── bin
├── Common
├── .git
│ ├── branches
│ ├── hooks
│ ├── info
│ ├── logs
│ │ └── refs
│ ├── objects
│ │ ├── 08
│ │ ├── 38
│ │ ├── 62
│ │ ├── 6f
│ │ ├── 8c
│ │ ├── 9e
│ │ ├── a0
│ │ ├── cb
│ │ ├── d9
│ │ ├── info
│ │ └── pack
│ └── refs
│ ├── heads
│ ├── remotes
│ └── tags
├── Setup
├── test
└── tools
27 directories
You also get the number of directories. If you do not want it, add the --noreport
option.
It is also possible to exclude pattern etc... man tree
is your friend there.
Another example: a flat list of directories, excluding obj*
and refs
tree -a -d -L 3 -if -I "obj*|refs" --noreport
returns:
.
./bin
./Common
./.git
./.git/branches
./.git/hooks
./.git/info
./.git/logs
./Setup
./test
./tools
Upvotes: 1
Reputation: 7499
Simple find
should do it:
find /some/path -mindepth 1 -maxdepth 1 -type d
-maxdepth 1
ensures you don't descend into subdirectories. However, if that's what you want, you can just remove it.
-mindepth 1
ensures find
does not include the directory itself.
Upvotes: 2
Reputation: 785156
In bash
you don't need to call any expternal utility to list all directories (including hidden ones). Just use:
# make sure we match entries starting with dot and return empty when list is empty
shopt -s dotglob nullglob
# print all the directories including ones starting with dot
printf '%s\n' */
Upvotes: 5