In this short tutorial, we will see how to prohibit directory listing in Linux from commandline. To put this in other words, we are going to deny the regular users from listing the contents of a directory using ls
command, but still allow the users to access and modify the files from the same directory.
Prohibit Directory Listing in Linux
Let us create a 'test
' directory with some files in it.
$ mkdir test
$ touch test/{file1,file2}.txt
Now set the appropriate permissions to the 'test
' directory using chmod
command:
$ chmod 0111 test/
This command will restrict the regular users from listing the contents of 'test
' directory
Let us verify the directory permissions:
$ ls -ld test/
Sample output:
d--x--x--x 2 ostechnix ostechnix 4096 Dec 21 18:23 test/
As you see in the above output, the test directory has only execute (x) permission. The read (r) and write (w) permissions are revoked. Meaning - the regular users can not access the directory contents using ls
command.
Let us try to list the directory contents using ls
command:
$ ls test/
Sample output:
ls: cannot open directory 'test/': Permission denied
See? We can't list the directory contents. Because, we disabled the directory listing for 'test' directory.
However, we still can access the files in the directory by using their name.
$ echo "Welcome to OSTechNix blog" > test/file1.txt
$ cd test/
$ cat file1.txt
Welcome to OSTechNix blog
This trick can be useful in securing the contents of a directory in a website. This technique is one way to restrict the bots from crawling a web directory. Of course, the super and root user can access the directory without any restrictions.