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

Output:

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/

#OR

$ chmod 0111 test/

Verify the directory permissions using the following command:

$ ls -ld test/

Output:

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/

Output:

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

Output:

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

Output:

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.