53
22
Is there any way to list just the folders in a directory using bash commands? ( as the ls
command lists all the files and folders )
53
22
Is there any way to list just the folders in a directory using bash commands? ( as the ls
command lists all the files and folders )
68
You can use:
ls -d -- */
Since all directories end in /
, this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.
29
Stephen Martin's response gave a warning, and listed the current folder as well, so I'd suggest
find . -mindepth 1 -maxdepth 1 -type d
(This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)
1Older question I know. While I would too initially turn to find for this task, I like the ls -d -- */
option, as find
will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1 – matchew – 2012-12-21T16:16:49.877
12
find . -maxdepth 1 -type d
Will list just folders. And as Teddy pointed out you'll need -maxdepth to stop it recusrsing into sub dirs
5You probably want -maxdepth 1
too. – Teddy – 2011-09-14T08:40:07.603
6
Daniel’s answer is correct. Here are some useful additions, though.
To avoid listing hidden folders (like .git
), try this:
find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \)
And to replace the dreaded dot slash at the beginning of find
output in some environments, use this:
find . -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \) | sed 's|^\./||g'
1
You're "not supposed to" parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.
if you don't want ls or find, you may want to try filtering "*" with "[ -d ]".
I did just that, for some reason ls and find weren't working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of
for f in * ; do [ -d "$f" ] && echo $f is indeed a folder ; done
0
You can also use:
du
Or:
git ls-tree -d -r --name-only @
0
Just to emphasize a thing that confused me out here, in respect to glob patterns selection; say you have this:
$ cd /tmp
$ mkdir testglob
$ for ix in {00,01,02,03} ; do mkdir testglob/mydir_${ix} ; done
$ for ix in {00,01,02,03} ; do touch testglob/myfile_${ix} ; done
$ for ix in {00,01,02,03} ; do touch testglob/mydir_${ix}.txt ; done
$ for ix in {00,01,02,03} ; do mkdir testglob/otherdir_${ix} ; done
$ tree testglob/
testglob/
├── mydir_00
├── mydir_00.txt
├── mydir_01
├── mydir_01.txt
├── mydir_02
├── mydir_02.txt
├── mydir_03
├── mydir_03.txt
├── myfile_00
├── myfile_01
├── myfile_02
├── myfile_03
├── otherdir_00
├── otherdir_01
├── otherdir_02
└── otherdir_03
8 directories, 8 files
So, say here you want to select only mydir*
directories. Note that if you leave out the terminating slash, ls -d
will list files as well:
$ ls -d testglob/mydir* # also `ls -d -- testglob/mydir*`
testglob/mydir_00 testglob/mydir_01 testglob/mydir_02 testglob/mydir_03
testglob/mydir_00.txt testglob/mydir_01.txt testglob/mydir_02.txt testglob/mydir_03.txt
... however, with a terminating slash, then only directories are listed:
$ ls -d testglob/mydir*/ # also `ls -d -- testglob/mydir*/`
testglob/mydir_00/ testglob/mydir_01/ testglob/mydir_02/ testglob/mydir_03/
0
printf "%s\n" */
will list all directories in the $PWD.
echo */
will also work, but in a long one-line, more difficult when names have spaces.
2ls -- */ lists all directories with their contents below – Vins – 2013-01-21T11:32:05.197
3@8088 What is the difference between
ls -d -- */
andls -d */
? – Louis – 2014-03-24T17:25:52.0436@Louis,
--
is conventionally used to mark the end of options, so that if a file is named-l
ls won't interpret it as the long listing format option. – Cristian Ciupitu – 2014-04-22T00:20:42.663