Linux / UNIX List Just Directories Or Directory Names

Tutorial details Difficulty Easy (rss) Root privileges No Requirements None Time 1m

ADVERTISEMENTS



Display or list all directories in Unix

How do I list just directory names on a Linux and UNIX-like operating systems? How can I list directories only in Linux?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 and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.

Type the following command:

$ ls -l | grep `^d'

$ ls -l | egrep `^d'

Or better try the following ls command only to list directories for the current directory:

$ ls -d */

Sample outputs:



Linux list only directories using ls command

Run the following ls command:

ls -d */



Linux Display or list only files

Type the following command to display list only files in Linux or Unix:

$ ls -l | egrep -v '^d'

$ ls -l | egrep -v '^d'

The grep command is used to searches input. It will filter out directories name by matching first character ‘ d ‘. To reverse effect i.e. just to display files you need to pass the -v option. It invert the sense of matching, to select non-matching lines.

You can create two aliases as follows to list only directories and files.

alias lf="ls -l | egrep -v '^d'"

alias ldir='ls -d */'

##alias ldir="ls -l | egrep '^d'"

Put above two aliases in your bash shell startup file:

$ cd

$ vi .bash_profile



Append two lines:

alias lf="ls -l | egrep -v '^d'"

alias ldir='ls -d */'

#alias ldir="ls -l | egrep '^d'"

Save and close the file in vim. Now just type lf – to list files. Again run ldir to list directories only:

$ cd /etc

$ lf

Sample outputs:

-rw-r--r-- 1 root root 2149 2006-09-04 23:25 adduser.conf -rw-r--r-- 1 root root 44 2006-09-29 05:11 adjtime -rw-r--r-- 1 root root 197 2006-09-04 23:48 aliases -rw------- 1 root root 144 2002-01-18 13:43 at.deny -rw-r--r-- 1 root root 162 2006-09-22 23:24 aumixrc -rw-r--r-- 1 root root 28 2006-09-22 23:24 aumixrc1 .... .. ....

List directory names only:

$ cd /etc

$ ldir



Sample outputs:

drwxr-xr-x 4 root root 4096 2006-09-22 16:41 alsa drwxr-xr-x 2 root root 4096 2006-09-20 20:59 alternatives drwxr-xr-x 6 root root 4096 2006-09-22 16:41 apm drwxr-xr-x 3 root root 4096 2006-09-07 02:51 apt drwxr-xr-x 2 root root 4096 2006-09-08 01:46 bash_completion.d .... ..... .

Use find command to list either files or directories on Linux

The find command can be used as follows to list all directories in /nas, enter:

find / nas -type d find / nas -type d -ls find . -type d -ls find /nas -type d find /nas -type d -ls find . -type d -ls

Sample outputs:

1070785 8 drwxrwxrwt 8 root root 4096 Jul 5 07:12 . 1070797 8 drwx------ 2 root root 4096 Jul 4 07:22 ./orbit-root 1070843 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./w 1070789 8 drwxr-xr-x 10 root root 4096 Jun 17 14:54 ./b 1071340 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/init.d 1071581 8 drwxr-xr-x 3 root root 4096 Jun 16 18:55 ./b/bind 1071584 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/bind/bak 1071617 8 drwxr-xr-x 2 root root 4096 Jun 16 18:55 ./b/fw 1071628 8 drwxr-xr-x 8 root root 4096 Jun 16 18:55 ./b/scripts

Pass the -maxdepth 0 to limit listing to the starting-points i.e. the current working directory only:

find /path/to/dir -maxdepth 1 -type d

find . -maxdepth 1 -type d

find . -maxdepth 1 -type d -ls



Putting it all together

Say you want to find all directories ending with .bak extension and delete it, run the following find command in the current directory:

find . -type d -iname ".bak" -delete

Verify it:

find . -type d -iname ".bak" -ls

The following shell script does two things for Apache/Nginx/Lighttpd Webroot such as /webroot/:

First, finds all files and directories and set permission to read-only for security reasons. Second, it allows our web server to read files regardless of permission so that we don’t get an HTTP/403 error.

In other words, all write permissions are removed from Webroot. The server/web-app can only read files but can not alter any files or upload any files. It helps reduces attack surfaces provided that you configure the rest of the server and web application firewall correctly.

#!/usr/bin/env bash # Purpose: Remove all write permissions from Apache/Nginx WebRoot. Make it read-only # Author: Vivek Gite {https://www.cyberciti.biz} under GPL v2.0+ # ----------------------------------------------------------------------------------- _dp = "0445" #dir read-only and allow nginx/php/perl-cgi to read it _fp = "0444" #file read-only _dir = "$1" _user = "www-nixcraft" _group = "www-nixcraft" _chown = "/bin/chown" _chmod = "/bin/chmod" _find = "/usr/bin/find" _xargs = "/usr/bin/xargs" # die if $_dir not found [ $# -eq 0 ] && { echo "Usage: $0 dirname" ; exit 1 ; } # Get base and dir names from arg _dn = " $(dirname $_dir) " _bn = " $(basename $_dir) " # Avoid working on / [ " $_dn " == " $_bn " ] && { echo "$0: Sorry, can't work on root path." ; exit 1 ; } # Let us get started if [ -d " $_dir " -o -e " $_dir " ] then echo "Chowning $_dir to $_user : $_group ..." $_chown -R $_user : $_group " $_dir " echo "Setting $_dir perm to $_fp ..." $_chmod -R $_fp " $_dir " if [ -d " $_dir " ] then echo "Allowing php/perl/our webserver to read files in all subdirs..." $_find " $_dir " -type d -print0 | $_xargs -0 -I { } $_chmod $_dp "{}" fi else echo "Error: $_dir not found." fi #!/usr/bin/env bash # Purpose: Remove all write permissions from Apache/Nginx WebRoot. Make it read-only # Author: Vivek Gite {https://www.cyberciti.biz} under GPL v2.0+ # ----------------------------------------------------------------------------------- _dp="0445" #dir read-only and allow nginx/php/perl-cgi to read it _fp="0444" #file read-only _dir="$1" _user="www-nixcraft" _group="www-nixcraft" _chown="/bin/chown" _chmod="/bin/chmod" _find="/usr/bin/find" _xargs="/usr/bin/xargs" # die if $_dir not found [ $# -eq 0 ] && { echo "Usage: $0 dirname"; exit 1; } # Get base and dir names from arg _dn="$(dirname $_dir)" _bn="$(basename $_dir)" # Avoid working on / [ "$_dn" == "$_bn" ] && { echo "$0: Sorry, can't work on root path."; exit 1; } # Let us get started if [ -d "$_dir" -o -e "$_dir" ] then echo "Chowning $_dir to $_user:$_group ..." $_chown -R $_user:$_group "$_dir" echo "Setting $_dir perm to $_fp ..." $_chmod -R $_fp "$_dir" if [ -d "$_dir" ] then echo "Allowing php/perl/our webserver to read files in all subdirs..." $_find "$_dir" -type d -print0 | $_xargs -0 -I {} $_chmod $_dp "{}" fi else echo "Error: $_dir not found." fi

We can runt it as follows:

/path/to/read-only-webroot.sh /webroot/

/path/to/read-only-webroot.sh /home/httpd

Conclusion

We explained how to list the directories only using various command-line options in Linux and Unix-like operating systems.