Finding and listing hidden files under Linux

Under Linux, the find command is very handy, and once again proves its worth when it comes to recursively searching for and listing hidden files on a machine. By specifying a root as the starting point for the search, we'll be able to search all hidden files, or all hidden folders, as this works for both types of item.

Today, we're going to answer a number of questions: How can I recursively list all hidden files and directories? How do we save the result in a text file? What commands can we use?

A quick reminder before going any further: hidden files and folders are easily identifiable because their name begins with a dot. It's the same on all Unix/Linux-based systems. One example is the ".git" directory, which is present in the directory of a project initialized with Git.

Note: all commands used in this tutorial are native commands under Linux.

1. See hidden files with find

The find command accepts the " -type "which is used to specify the type of element, in this case files, so the " f" . For this search, we're targeting elements whose name begins with ".*", i.e. a dot followed by any character string. The find command will search all sub-folders starting from the specified root, enabling a recursive search.

find /etc/git/projet1/ -type f -name ".*" -ls

A hidden file named ".secret" is present in my target folder: the find command works very well and returns only the hidden item(s).

search for Linux hidden files

To obtain the list of results with the full path to each file, you can use " -print "instead of " -ls " :

find /etc/git/projet1/ -type f -name ".*" -print

In this case, the order returns :

/etc/git/projet1/.secret

To store the result in a file on the machine, you can use the usual redirector by specifying the path to an output file :

find /etc/git/projet1/ -type f -name ".*" -ls > /home/tutobox/resultat.txt

2. See hidden folders with find

In the same spirit, you can search for hidden directories simply by changing the type to "-type d". Here's an example:

find /etc/git/projet1/ -type d -name ".*" -ls

In this case, the hidden files are not returned because the ".secret" file is not in the list of results. However, the ".git" directory on my Linux machine is there.

find hidden folders Linux

3. Is this possible with the ls command?

The ls command, a must on Linux, can also be used to obtain a list of hidden files and folders. Remember that this command is used to list the contents of a directory and to obtain information on item properties, notably rights.

Firstly, by adding the "-a" or "-A" option, which is used to list all elements, both visible and hidden. Here's an example:

ls -a

or :

ls -a /etc/git/projet1/

The disadvantage is that the command returns all elements, not just the hidden ones. Using "grep" as a complement, you can filter using a regex (regular expression): you look for the presence of a "." at the very beginning of the name.

ls -a | grep "^."

This command is useful for searching in the current folder, unlike find, which has a broader scope.

4. Conclusion

After reading this article from the tutorial box, you will be able to find and list hidden files under LinuxThese very useful commands contain many options that you can explore by looking at the help.

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *