Working with hidden files in Linux

Hidden Files

Hidden files in Linux are the files that are not listed when the user runs ls command. The name of a hidden file starts with a. dot(.) In Linux, not only files, but directories can be hidden as well. Files are hidden in Linux for many reasons. One of them is to make sure that users don’t accidentally modify the contents of these files. Another could be to prevent the accidental deletion of these files. On shared systems files could be hidden for privacy issues.

Most hidden files contain environment settings or data that are accessed by programs being run by the user. They’re not intended to be edited by the user, only the application should have access to these. That’s why they’re hidden from the user’s normal view. These can be configuration files, log files. Examples of system files that are hidden are “.profile“, “.bashrc,”  “.vimrc”

How to show hidden files in Linux?

In the Linux terminal, ls command is used to list all the files, but as we discussed this will not list hidden files. Running simple ls will yield output like the one shown below.

Ls Command

To show hidden files in Linux, you can use the ls -a option as shown below to display “all” files:

$ ls -a
Ls -a Command

We can now see hidden files being listed. All the files starting with a dot are hidden files. In GUI this can be done by pressing Ctrl+H while viewing a directory. That will list all the hidden files in that directory.

To get a more verbose output in a list manner use :

$ ls -al
ls -al

Create hidden files in Linux

To create hidden files just create a file with the filename starting with a dot. This will let the Linux system know that the file is meant to be a hidden file.

$ touch .[filename]
Create Hidden Files

A file named .hidden.txt was created. The dot at the beginning of the filename rendered it hidden. When we run a normal ls, the file is not listed. However, upon running ls -a command the file is listed (4th row, 2nd column).

Hiding an existing file or directory in Linux

Converting existing file into a hidden file is simple and can be done with the following command :

$ mv [filename] .[filename]

mv command simply renames the file to have a name that begins with a dot. The same result can be obtained by editing the name of the file using GUI. Edit the filename and add a dot at the beginning to hide the file in Linux.

Moving File

This command moved the existing input.txt to the list of hidden files. The opposite of this can also be achieved using mv command, that is a hidden file can be converted into a normal file.

$ mv .[filename] [filename]

Conclusion

Files in Linux are Hidden to limit the visibility of a file. These can be system files, application files or files created by users. There are ways to view these files, however, one should be careful while dealing with files that are hidden (they are hidden for a reason). To learn more about hidden files, refer to the official documentation of Linux.