Display Only Hidden Files in Linux

Displaying hidden files in Linux is quite easy. You use the ls command in this manner:

ls -a

That's fine. You can see the hidden files with their names starting with a dot (.).

But you see all the files in the current directory, the hidden ones and the regular ones.

What if you want to ONLY see the hidden files, not the regular ones?

There is no ready-to-use option like -a and -A. However, you can always find a way to achieve things by combining a few commands through the wonderful pipe redirection.

Display only the hidden files and directories

To display only the hidden files and directories, the simplest approach is to show the hidden files with ls -a and then use grep to filter only the entries that start with a . (dot).

ls -a | grep "^\."

The ^ means 'start with'. The dot character needs to be escaped with \ and since you used special characters, everything is enclosed in double-quotes. With "^\.", you tell grep command to only show results that start with a dot.

Let me share an example. Here's the content of my sample directory:

abhishek@itsfoss:~/toto$ ls -l
total 352
-rw-rw-r-- 1 abhishek abhishek  45443 May 24 09:03 apt-get.pdf
-rw-rw-r-- 1 abhishek abhishek  29983 May 27 16:07 bash.pdf
-rw-rw-r-- 1 abhishek abhishek 249773 May 26 14:56 cronjob-cheatsheet.png
-rw-rw-r-- 1 abhishek abhishek   4943 Jun  2 20:09 gnome-console-voiceover
-rw-rw-r-- 1 abhishek abhishek  12721 May 29 12:29 members.2022-05-29.csv
-rw-rw-r-- 1 abhishek abhishek    143 May 30 12:06 routes.yaml

Now I am going to filter it out so that it only shows the hidden files and directories:

abhishek@itsfoss:~/toto$ ls -a | grep "^\."
.
..
.hidden-file
.hid_dir
.member.csv

It shows the special . (current directory) and .. (parent directory) directories as well. You can filter them out by using the -A option instead of -a.

abhishek@itsfoss:~/toto$ ls -A | grep "^\."
.hidden-file
.hid_dir
.member.csv

This is better, right? Here's a screenshot if you want to see them all together in a single image:

There is still a little problem here. Though you can see them in individual lines, you cannot distinguish if it's a file or a directory.

If you want that, there is another way to achieve that.

Alternate method

To show just the hidden files and directories in the long listing format (so that you can see if it's a file or directory), you can use this:

ls -ld .*

It will show the following result for the example here:

drwxrwxr-x  3 abhishek abhishek  4096 Jun 30 10:38 .
drwxr-x--- 28 abhishek abhishek  4096 Jun 29 15:15 ..
-rw-rw-r--  1 abhishek abhishek 41145 May 27 15:24 .hidden-file
drwxrwxr-x  2 abhishek abhishek  4096 Jun 30 10:38 .hid_dir
-rw-rw-r--  1 abhishek abhishek 12721 Jun  3 09:41 .member.csv

How does it work? First, the .* part is shell globbing. It expands right there on the input. So, basically, you can think of it as:

ls -ld . .. .hidden-file .hid_dir .member.csv

The use of option -d is important here. It tells ls to only list the directory, not its contents. Otherwise, the ls command will also show the contents of the .hid_dir.

In case you don't want to display the . and .. directories, you can use the ls command like this:

ls -ld .!(|.)

Focusing on the .!(|.) part obviously. This is a pattern. ! is negation and | is OR condition. (|.) means nothing or dot. When you negate it with !, you are saying it should not be "nothing" or dot. Combine it all together .!(|.) and you are saying a pattern where . is followed by something (thus single dot is ruled out) except another dot (thus ruling out double dots).

You get the desired result now:

-rw-rw-r-- 1 abhishek abhishek 41145 May 27 15:24 .hidden-file
drwxrwxr-x 2 abhishek abhishek  4096 Jun 30 10:38 .hid_dir
-rw-rw-r-- 1 abhishek abhishek 12721 Jun  3 09:41 .member.csv

Display only hidden files (not hidden directories)

Displaying only hidden files without the hidden directories is quite easy. Use this:

ls -ld .* |grep -v ^d

You are displaying both the hidden files and directories and then filtering out the entries starting with d (which are directories).

abhishek@itsfoss:~/toto$ ls -ld .* 
drwxrwxr-x  3 abhishek abhishek  4096 Jun 30 10:38 .
drwxr-x--- 28 abhishek abhishek  4096 Jun 29 15:15 ..
-rw-rw-r--  1 abhishek abhishek 41145 May 27 15:24 .hidden-file
drwxrwxr-x  2 abhishek abhishek  4096 Jun 30 10:38 .hid_dir
-rw-rw-r--  1 abhishek abhishek 12721 Jun  3 09:41 .member.csv
abhishek@itsfoss:~/toto$ ls -ld .* |grep -v ^d
-rw-rw-r--  1 abhishek abhishek 41145 May 27 15:24 .hidden-file
-rw-rw-r--  1 abhishek abhishek 12721 Jun  3 09:41 .member.csv

Of course, there could be several other possible ways. The find command is always there for such stuff.

If you can think of a different solution, do share it in the comment section.