In Bash, hidden files are the files and directories whose names start with a dot (.). This makes them invisible in standard directory listings. These files often store configuration settings, user preferences, and other system-related information. The purpose of hiding files with a dot is to keep the file system clutter-free and prevent the accidental removal of vital files. In this article, I will demonstrate some techniques to show bash hidden files.
Why Do We Have Hidden Files?
Hidden files are not generally visible which protects them from being discovered in the first approach by the user. It helps them to be kept away from editing, deleting, or any other changes. Some reasons behind the creation of hidden files in Linux are:
- Organization and clutter reduction: As the system files, configuration files, or user settings files are not required to interact with most users, they are kept hidden from the home directory. It makes the system tidy and visually manageable.
- Protecting important files: System files contain sensitive settings or data related to system and application functionality. Hiding them prevents unintentional removal or modification.
- Maintaining compatibility: Some programs rely on filenames starting with a dot (.) hence hiding these program files makes them compatible with programs and avoids any failure to launch or execute the program.
6 Ways to Show Bash Hidden Files
There are many command-line tools Linux provides to show hidden bash files. Some of these commands are stat command, ls command, find command, dir command, tree command, etc. In this section, I will briefly discuss each of these commands with practical demonstration. So without further delay, let’s start!
1. Using “stat” Command
The stat command is the short form of status that prints details of files and folders inside a directory. This with
.* regular expression can find hidden files inside a directory. Here the
.* part looks for files or folders that start with a
. (dot) which are the hidden files.
Now, run the below command on the terminal to print the details of all the files and folders starting with a . (dot):
The image shows the details of hidden files starting with . (dot) have been printed.
2. With “ls” Command
The command ls stands for list. With the option –all or -a, the ls command lists all the files and folders of a directory. Here, the
-a option makes sure the visibility of all the entries starts with a . (dot).
Execute the following command in the command prompt to list all hidden files and folders:
The image shows that the ls command has printed all the hidden files and folders of a directory.
3. Using “find” Command
The find command is a tool to surf through the file and folder tree. It enables users to search files and folders by file, folder name, creation and modification dates, etc. The basic syntax for using the find command is:
find [options] [path] [expression]. Now I will run the following command to find hidden files and folders inside the current directory:
find . -name ".*"
In the image, you can see that the find command has listed the hidden files and folder inside the current directory.
4. Using “dir” Command
The word dir is the short form of a directory. The dir command is functionally close to the ls command. It shows the contents of the current directory. Similar to the ls command, the dir command can show all files or folders whether they are hidden or not inside the current directory if the
-a option is passed with the dir command.
Let’s execute the following command into the terminal to list hidden files and directories:
See in the image that the dir command has displayed the hidden files and folders in the current directory.
5. Using “tree” Command
The tree command is a tool that visualizes all the files, folders, and subfolders of the file hierarchy recursively. It helps programmers to navigate through the server and find the necessary file. With the
-a option, the tree command also displays the hidden contents of a directory.
Execute the below command in the terminal to display all the files, folders, and subfolders of the /home/susmit/file directory:
tree -a /home/susmit/file
The image shows that the tree command has visualized the files, folders, and subfolders in a tree-like structure of the file directory.
6. Using Wildcards
The echo command takes a string or a text as an argument and echos it to the standard output such as the command line interface. If the echo command gets
* as an argument, it prints all the file names and the folder names on the terminal. To find out the hidden files or folders inside a directory, pass
.* as an argument to the echo command.
Here I will run a command to print all the hidden file names and folder names of the current directory:
You can see that the echo command with wildcard “.*” has displayed all the hidden files and folders.
How to Hide Files in Linux
System files that are visible to the system users are prone to be deleted or edited unconsciously. To secure a file from being unintentionally deleted or edited, hiding is a good solution. To hide a file, you just need to set a . (dot) at the beginning of the file name. For this purpose, the mv command is the most useful tool. Simply use the command to rename the file name as intended.
Here is a bash script to hide the file named sample:
echo “before hiding the sample”
mv sample .sample
echo “after hiding the sample”
In the above image, you can see that the mv command has made the sample file hidden.
A Simple Tip on Showing Bash Hidden Files
To ease the frequent use of showing hidden files or folders feature by executing the same command repeatedly, programmers can define an alias that will contain the whole command to show hidden files or folders inside the current directory. The syntax for defining an alias is
alias ‘alias_name’=’command to show hidden files’. Whenever the alias name is specified it will execute the command defined inside the alias.
To define the alias, run the below command into the terminal:
alias lsh=’ls -a’
Here, an alias named lsh is defined and it contains the ‘ls -a’ command.
To access the alias, run the command:
The lsh will execute the ‘ls -a’ command which will print all the hidden files and folders inside the current directory.
The image shows that the lsh alias has displayed all the files and folders on the terminal.
To sum up, the hiding feature of bash files and directories is a great way to secure the system from unauthorized action by non-administrative users. This article has discussed different approaches to show hidden bash files. I believe this is enough resourceful article to implement this.
People Also Ask
The hidden files are generally invisible when the ls command is executed to view the contents of a directory. It can be a file or a directory possessing a name starting with a
How to View Hidden Files Using GUI?
To view hidden files at first, navigate to the directory where the hidden files might exist. Then press CTRL+H from the keyboard. It will show all the files and folders including hidden and unhidden inside the current directory.
Another approach is to press the three-horizontal-bar button from the top right corner. Then check the “Show Hidden Files”.
To check disk space used by hidden files in Linux, use the syntax:
du file_name. This will print the size of the hidden file with the file name.
To edit a hidden files in Linux, firstly, open the file in the nano editor using the syntax:
nano .file_name. Then edit the file as you wish. Finally press CTRL+S and CTRL+X to save the file then exit.
To list all the hidden files in Unix, execute the command:
ls -a. It will print all the files and folders including hidden files. The hidden files will contain a . (dot) at the beginning of the name. Moreover, to get the file type as well as the file names, execute the command:
ls -aF. It will give a full listing. From the command output, you will see a slash after the directory names and a star after the executable files. The above image shows the hidden file type.