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
Description | Disable Directory Listing in Linux |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | mkdir, touch, ls, chmod |
Internet Required | No |
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:

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:

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:
$ ls -l test/
Output:

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:

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.
w
) permission to this directory using the “chmod ug+w test/
” or “chmod 331 test/
” commands.$ cd test/
$ touch file4.txt
Output:

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.