This post will guide you how to list directories only from the command line in the Linux operating systems. How do I list just directory names using the ls command in Linux.
In Linux, you can use the ls command to list files or directories in your current direcotry or a given directory in Linux. And ls do not have an option to list directories only in your current directory. How to do it. You can use a combination of ls command and grep command to achive the result. Or you can also use the ls command with -d option and wildcards to list directories only in Linux.
- Ls Command
- List Directories only Using -l Option and Grep Command
- List Directories only Using ls Command with -d Option and Wildcards
- List Directories only Using Printf Command
- List Directories only Using Find Command
Ls Command
Ls command can be used to list information about the FILEs (the current directory by default).
The syntax of the ls command is as followss:
ls [OPTION]... [FILE]...
List Directories only Using -l Option and Grep Command
If you want to list only directories in your current working direcotry, and you can use the ls command with the -l option in combination with the grep command, type:
$ ls -l | grep ^d
outputs:
devops@devops:~$ ls -l | grep ^d
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Desktop
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Documents
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Downloads
drwxrwxr-x 3 devops devops 4096 Aug 29 09:44 IdeaProjects
drwxrwxr-x 2 devops devops 4096 Aug 8 06:46 jetbrains-toolbox-1.15.5796
drwxr-xr-x 2 devops devops 4096 Jun 5 06:21 logs
drwxr-xr-x 2 root root 4096 Jul 3 07:05 LxdDirectory
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Music
drwxrwxr-x 12 devops devops 4096 Sep 4 08:16 myphp
drwxr-xr-x 20 devops devops 4096 Aug 20 10:31 netbeans
drwxr-xr-x 2 devops devops 4096 Sep 8 23:40 Pictures
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Public
drwxr-xr-x 18 devops devops 4096 Jun 18 10:45 Python-3.7.3
drwxrwxr-x 2 devops devops 4096 Aug 29 22:37 sambashare
drwxr-xr-x 4 devops devops 4096 Aug 29 09:42 snap
drwxr-xr-x 3 devops devops 4096 Aug 22 06:46 Steam
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Templates
drwxrwxr-x 3 devops devops 4096 Jun 20 10:27 tensorflow_env
drwxrwxr-x 3 devops devops 4096 Sep 7 06:02 tmp
drwxr-xr-x 2 devops devops 4096 Jun 3 07:19 Videos
drwxr-xr-x 2 devops devops 4096 Sep 2 21:55 wordpress
drwxr-xr-x 8 devops devops 4096 Jun 5 06:29 world
If you only want to print the directory names, and you can pass the above output to the awk command through the pipline, type:
$ ls -l | grep ^d | awk '{print $9}'
Outputs:
devops@devops:~$ ls -l | grep ^d | awk '{print $9}'
Desktop
Documents
Downloads
IdeaProjects
jetbrains-toolbox-1.15.5796
logs
LxdDirectory
Music
myphp
netbeans
Pictures
Public
Python-3.7.3
sambashare
snap
Steam
Templates
tensorflow_env
tmp
Videos
wordpress
world
List Directories only Using ls Command with -d Option and Wildcards
you can pass the -d option to the ls command to print all directory names in your current directory, type:
$ ls -d */
Outputs:
Desktop/ IdeaProjects/ LxdDirectory/ netbeans/ Python-3.7.3/ Steam/ tmp/ world/ Documents/ jetbrains-toolbox-1.15.5796/ Music/ Pictures/ sambashare/ Templates/ Videos/ Downloads/ logs/ myphp/ Public/ snap/ tensorflow_env/ wordpress/
List Directories only Using Printf Command
You can also use printf command to print all directories in your current direcotry, type:
$ printf '%s\n' */
Outputs:
devops@devops:~$ printf '%s\n' */
Desktop/
Documents/
Downloads/
IdeaProjects/
jetbrains-toolbox-1.15.5796/
logs/
LxdDirectory/
Music/
myphp/
netbeans/
Pictures/
Public/
Python-3.7.3/
sambashare/
snap/
Steam/
Templates/
tensorflow_env/
tmp/
Videos/
wordpress/
world/
List Directories only Using Find Command
You can also use the find command to search for files in a directory hierarchy based on the file type, such as: directory. So you can type the following command to only search all directories in your given direcotry or the current working directory, type:
$ find . -maxdepth 1 -type d
Or
$ find . -maxdepth 1 -type d -ls
Outputs:
devops@devops:~$ find . -maxdepth 1 -type d
.
./.kodi
./tensorflow_env
./world
./Documents
./.steam
./.java
./.gconf
./snap
./.cache
./.netbeans
./.keras
./.rpmdb
./myphp
./.mozilla
./.minecraft
./.local
./.config
./.gradle
./.ssh
./Steam
./tmp
./LxdDirectory
./logs
./Desktop
./sambashare
./netbeans
./Python-3.7.3
./Music
./jetbrains-toolbox-1.15.5796
./.gnupg
./Pictures
./Downloads
./wordpress
./IdeaProjects
./Public
./Videos
./.pki
./.android
./.IdeaIC2019.2
./Templates
If you want to get more information about those commands, you can see the man page by the following comands:
$ man ls $ man grep $ man printf $ man find
Conclusion
You should know that how to use ls/grep/find/printf commands to list only directory names in a CentOS or RHEL or Ubuntu Linux server.