How To Disable Directory Listing From the Command Line in Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

When you create a directory and add files to it, any user who has access to your system and knows how to use the ls command may simply list the files in that directory and see what you have added to it.

You can stop this from happening by taking away the read (r) permission from the directory. This way, you won’t be able to list the directory’s files, but you will still be able to read and write to them.

If you are working on a web server and you want to prohibit people from accessing certain directories, it makes sense to set this permission to prevent users and bots from crawling the directory content.

In this article, you will learn how to disable the directory content listing from the command line in Linux.

Tutorial Details

DescriptionDisable Directory Listing in Linux
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesmkdir, touch, ls, chmod
Internet RequiredNo

Disable Directory Listing in Linux

Let’s create a sample directory using the following command:

$ mkdir test

Then create a few files inside the directory using the following command:

$ touch test/{file1,file2,file3}.txt


Creating a directory and adding files to it

Now, revoke the read (r) and write (w) permissions, leaving only the executable (x) permission for the directory, using the chmod command.

$ chmod ugo-rw test/


$ chmod 0111 test/

Verify the directory permissions using the following command:

$ ls -ld test/


Verifying the directory permissions

As you can see from the above picture, only executable (x) permission is assigned to the β€œtest” directory, and read (r) and write (w) permissions are revoked.

So, when you try to list the content of this directory, you will get the following error:

The root user or users with sudo privileges can still list and change the directory’s contents.
$ ls -l test/


Listing the prohibited directory content

Although, you are still able to access the files inside this directory and modify them, as shown.

$ echo "Hi, Linux TLDR!" > test/file1.txt
$ cat test/file1.txt


Modifying and viewing the file content inside the disable directory

Also, you are able to move into the directory using the cd command, but note that you will not be able to create new files inside the directory as write (w) permission is revoked from the directory.

To enable the user to create a new file, specify the write (w) permission to this directory using the β€œchmod ug+w test/” or β€œchmod 331 test/” commands.
$ cd test/
$ touch file4.txt


Moving to the disabled directory

So, that’s all it takes to disable directory listing from the command line in Linux.

If you have any questions or queries related to this article, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.