Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing only directories using ls in Bash?

Tags:

directory

bash

ls

This command lists directories in the current path: ls -d */

What exactly does the pattern */ do?

And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents) for listing only directories in that path?

like image 377
Sibi Avatar asked Jan 16 '13 06:01

Sibi


People also ask

How do I list only directories in 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 list all directories in Bash?

Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.

Which command is used for to list only directories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems.

How do I get a list of directories in Linux?

The ls command is the basic command used to list files and directories within the Linux file system.


1 Answers

*/ is a pattern that matches all of the subdirectories in the current directory (* would match all files and subdirectories; the / restricts it to directories). Similarly, to list all subdirectories under /home/alice/Documents, use ls -d /home/alice/Documents/*/

like image 89
Gordon Davisson Avatar answered Oct 24 '22 06:10

Gordon Davisson