Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List sub-directories with ls [closed]

Tags:

linux

shell

How could I list sub-directories with ls, with '-d' only the current directory is shown. I want something like find . -type d -maxdepth 1 would give me.

like image 766
Bernhard Avatar asked Mar 02 '11 13:03

Bernhard


People also ask

How do I list sub folders?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.

Which command will find all the subdirectories within directories using ls?

Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.

How do I get a list of folders and subfolders with the files in Linux?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


3 Answers

This should help:

ls -d */

*/ will only match directories under the current dir. The output directory names will probably contain the trailing '/' though.

like image 132
Costi Ciudatu Avatar answered Oct 11 '22 15:10

Costi Ciudatu


You can combine with grep:

ls -l | grep '^d'

To get just the filenames:

ls -l | grep '^d' | awk '{ print $9 }'

You can make this into a handy alias:

alias ldir="ls -l | grep '^d'"
like image 40
dogbane Avatar answered Oct 11 '22 14:10

dogbane


ls -d */ and ls -d */*/ seem to work just fine.

like image 45
Šimon Tóth Avatar answered Oct 11 '22 14:10

Šimon Tóth