How to list folders using bash commands?
Solution 1
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.
Solution 2
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)
Solution 3
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
Solution 4
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'
Solution 5
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
Related videos on Youtube
Naved Ali
Updated on September 18, 2022Comments
-
Naved Ali over 1 year
I want to make a plugin which can assign multiple roles to a single user. Some say its not possible to assign multiple roles in wordpress then why wordpress has provided 'roles' in array form. e.g when i call roles of user it returns array
$current_user=wp_get_current_user(); $user_roles = $current_user->roles;
here $user_roles will be an array return by roles.
Is there any function through which i can assign multiple role to a single user
-
Suyog over 8 years
-
Naved Ali over 8 yearsHey Suyog i saw the referred page but there too i didn't get how can i add multiple roles to a single user
-
Tristan over 8 yearsHave you tried codex.wordpress.org/Class_Reference/… You should be able to use
$current_user->add_role('role_name');
-
Naved Ali over 8 yearsHey Tristan..thanxx it worked :)
-
-
Teddy over 12 yearsYou probably want
-maxdepth 1
too. -
Josue Figueiredo over 11 yearsOlder question I know. While I would too initially turn to find for this task, I like the
ls -d -- */
option, asfind
will find hidden directorties too. Which can sometimes be useful, but also sometimes cause trouble. I hope this comment might help others. +1 -
Vins over 11 yearsls -- */ lists all directories with their contents below
-
Louis Waweru about 10 years@8088 What is the difference between
ls -d -- */
andls -d */
? -
Cristian Ciupitu about 10 years@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.