How to List Only Hidden Files in Linux.
In Linux, hidden files are files or directories whose names start with a dot (.). These files are often used for configuration or settings purposes, such as .zshrc, .gitconfig, .profile, .ssh, and more. By hiding these files, the system reduces the chance that a user might accidentally delete or alter them, which could cause issues with the functionality of applications or the system itself. Keeping these files hidden ensures that the directory remains clean and organized, while still allowing users or programs to access them when needed for system and application configurations.
🎧
Using ls
Command
The ls
command is commonly used to list directory contents in Linux. By default, it doesn't display hidden files, but we can use specific options to show only hidden files.
Command line (CLI):
$ ls -ld .?*
This will list all hidden files and directories in the current folder, excluding. (the current directory) and .. (the parent directory).
Here's a breakdown of each part:
1. ls
:
• This is the basic command used to list directory contents.
2. -l
(long listing format):
• This option tells ls
to show detailed information about each file or directory, such as file permissions, number of links, owner, group, file size, and modification time.
3. -d
(directory):
• This tells ls
to list directories themselves, not their contents. Normally, ls
would list all the contents inside a directory if it’s given a directory name. With -d
, it lists the directory name itself instead.
4. .?*
:
• This is a pattern used to match hidden files and directories.
• .
: In Linux, files that start with a dot (.) are considered hidden files.
• ?
: The question mark matches exactly one character, so it ensures that at least one character follows the dot.
• *
: The asterisk matches zero or more characters. So this part matches any sequence of characters that follow the single character matched by ?
.
Hidden files in Linux serve important roles, from storing user and application configurations to maintaining system functionality and security. Their primary purpose is to reduce clutter in directories, protect system-critical data, and organize application-specific files in a way that's accessible when needed but out of the way during daily operations. Although these files are hidden, they are easy to reveal and modify when necessary using commands like ls -a
or text editors.
We hope you found these insights useful!