Linus provides many useful tools to help you work with files & directories. Sometimes you may need to show hidden files, list hidden files or display hidden files in Linux. There are different commands to help you view hidden files in Linux. We will look at each of them individually. You can use them for any Linux distribution since they are available on most distros.
How to View Hidden Files in Linux
You can easily view hidden files using ls, find and dir commands in Linux. We will look at their different use cases to view hidden files.
Show hidden files using ls
The most basic way to view hidden files is to simply use -a option in ls command. -a option stands for ‘all’.
$ ls -a /path/to/folder
For example,
$ ls -a /home/project
In the above command, ls will list all files (including hidden ones) in specified path. If you don’t specify the folder path, ls command will list files present in your current working directory.
If you do not want to display implied files & directories such as . (present folder) or .. (parent folder), use -A option instead
$ ls -A /path/to/folder
Also read : How to Fix Notice: Undefined Variable in PHP
Show only hidden files
The above commands will list both hidden and visible files & folders for you. If you only want to see the hidden files, use the following regular expression to filter the list of displayed files.
$ ls -dl .[^.]* /path/to/folder
Let us look at the above command in detail. We are using the regex .[^.]* to specify files & directories that start with a dot(.) in their name. This is because, in Linux, all hidden files & folders start with a dot in their name. In other words, we are asking ls command to list files & directories whose names start with a dot.
You can also use any of the following commands to get the same output
$ ls -d .* /path/to/folder OR$ ls -ld ~/.[^.]*
/path/to/folder OR$ ls -l ~/.??*
/path/to/folder OR$ ls -ld ~/.??*
/path/to/folder
Also read : How to Get Difference of Two Dictionaries in Python
Show Hidden Files using Find
You can also list all hidden files using find command with -name option
$ sudo find /path/to/folder -name ".*" 2> /dev/null
In the above command, we use -name option to specify that we want to find all files starting with “.”, that is, hidden files. We also direct the output of our command to /dev/null to avoid displaying error messages due to inadequate user permissions and access.
Here is the above command to find all hidden files on your system
$ sudo find / -name ".*" 2> /dev/null
Similarly, here is the find command to find all hidden files in your present working directory.
$ sudo find . -name ".*" 2> /dev/null
Also read : How to Disable Directory Browsing in Apache
Show Hidden Directories Only Using find
If you only want to display hidden folders and sub directories using find command, then use the -type option.
$ sudo find /path/to/folder -name ".*" -type d 2> /dev/null
Show hidden files using dir
You can also use dir command to display hidden files & folders. Just like ls command, dir too supports -a and -A options to include and exclude implied files & directories respectively.
$ sudo dir -a /path/to/folder $ sudo dir -A /path/to/folder
Like ls, if you want to display only hidden files & folders, you can use regular expressions.
$ sudo dir -dl .[^.]* /path/to/folder
In this article, we have learnt the different ways to view hidden files in Linux.
Also read : Difference between $host and $http_host in NGINX
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.