How to list folders using bash commands?

2,898

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
Share:
2,898

Related videos on Youtube

Naved Ali
Author by

Naved Ali

Updated on September 18, 2022

Comments

  • Naved Ali
    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

  • Teddy
    Teddy over 12 years
    You probably want -maxdepth 1 too.
  • Josue Figueiredo
    Josue Figueiredo over 11 years
    Older 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
  • Vins
    Vins over 11 years
    ls -- */ lists all directories with their contents below
  • Louis Waweru
    Louis Waweru about 10 years
    @8088 What is the difference between ls -d -- */ and ls -d */?
  • Cristian Ciupitu
    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.