Find Hidden Files and Directories in Linux

In Linux, files and directories whose names begin with a dot are considered “hidden”. The wildcard character * does not even consider all files in a directory: Files that start with a dot (often configuration files that are supposed to be invisible) are ignored.

If you now think you can capture invisible files with .*, everything gets even worse: This does not only mean invisible files starting with ., but also the directories . and .. (i.e. the current and the parent directory). If the respective command is able to process entire directories, the consequences can be fatal.

The problem can be fixed with the search pattern .[!.]*. This covers all file names whose first character is a dot, which have at least one further character that is not a dot, and which have any number (including zero) of further characters

echo .[!.]*

With the command ls the option -a can be used. It causes all files to be displayed, even invisible ones. However, no masks (such as rc) may be specified when using ls. -a only works if ls is allowed to search for the files itself and the shell does not take over this task.

Only find works really universally in this case, too. The following command finds all hidden files in the current directory:

find -maxdepth 1 -type f -name ".*"

Thanks for Reading !!

Related Posts