21

How can I list folders from within the terminal, which command should I use?

7 Answers 7

33

to list only folders try: ls -d */


Explanation

-d

list directories themselves, not their contents. To explain this, consider what happens if we type ls */. ls goes one layer down, into each subdirectory, and lists all the files in each of those sequentially

Source: man ls

*/

*/ is known as a "glob" in UNIX. (see Wikipedia for more details). But basically, it means "any file name ending in a forward slash." In UNIX, directories are really just files, fundamentally. But they are specially named ending in a forward slash so the operating system knows they are directories (or folders, in everyday-person-speak). And the asterisk * is technically a wildcard standing for "any string of characters."

What is a glob?

This paragraph will not pertain specifically to your question, but if you've never read about this, it'll be good to see it. Globs are different from Regular Expressions, as (partially) explained in What is the difference between Regular Expressions and Globbing? There have been whole books written on regular expressions, but tl;dr there are a bunch of different ways to encode pattern-matching expressions.

2
  • 1
    How to show hidden folders as well? ls -d .*/ shows only hidden folders. How to view BOTH hidden and non-hidden folders? I can only think of ls -d */ .*/ Anything better?
    – LFC_fan
    Oct 26, 2010 at 13:16
  • 2
    well, you can try ls -la | grep ^d but it is much longer :)
    – Praweł
    Oct 26, 2010 at 13:40
7

As I am a very inexperienced user I love this website. It tells you all you want to know about bash commands, in some cases it even gives you examples. Very useful.

In your case:

  • ls to list the files
  • ls -a to include hidden files
  • ls -l for a long listing format
  • ...
6
ls -1 -d */

where

-1

lists one directory per line.

5

If you want to be able to distinguish folders from files easily, use something like ls -alhF. I usually define l as an alias for that, ie. I put the line alias l='ls -alhF' in my .bashrc.

4
ls -al | grep ^d

include hidden files '-a' grep ^d get start with 'd' wich means directory when name starts with dot directory is hidden

to list recursively see this

find ./ -type d | less
./ ..................... means starts find in current folder
-type .................. indicates the type to be searched
d ...................... means directory
| ...................... redirects the command
less ................... enables paging using the keyboard arrows and leave with q
1
  1. To view home directory folders the ls command is enough - this will keep it simple.

    $ ls
    
    Desktop    Downloads         hadoop  Pictures  Templates
    Documents  examples.desktop  Music   Public    Videos
    
  2. You can even specify multiple directories ls ~ /usr

    $ ls ~ /usr
    
    /home/hadoop1:
    Desktop    Downloads         hadoop  Pictures  Templates
    Documents  examples.desktop  Music   Public    Videos
    
    /usr:
    bin  games  include  lib  local  locale  sbin  share  src
    
  3. To get the output in long format we can use the -l option

    $ ls -l
    total 48
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Desktop
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Documents
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Downloads
    -rw-r--r--  1 hadoop1 hadoop1 8980 Jul  1  2017 examples.desktop
    drwxr-xr-x 10 hadoop1 hadoop1 4096 Jul  1  2017 hadoop
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Music
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Pictures
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Public
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Templates
    drwxr-xr-x  2 hadoop1 hadoop1 4096 Jul  1  2017 Videos
    
0

ls will list the files.

ls -l will list the files with details (such as file size).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .