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):
stat .*
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:
ls -a
The image shows that the ls command has printed all the hidden files and folders of a directory.
Note: Regex patterns are sequences of characters that define a search pattern. To search for all the hidden files within a directory on your system, execute the below command:
ls -dl .[^.]* <path>
Here, the -d
option specifies the directory. And the .[^.]*
regex pattern searches for the contents starting with a (.) dot inside the certain <path>
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 ".*"
Here, the first “.(dot)” indicates to do the find operation on the current directory. After that, the “-name” indicates to search by name and the “.*” expression looks for the names starting with . (dot).
In the image, you can see that the find command has listed the hidden files and folder inside the current directory.
Note: Run the following command to find and list only hidden folders or directories (denoted by the option -type d
) inside the current directory:
find . -name ".*" -type d
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 --all
or -a
option is passed with the dir command.
Let’s execute the following command into the terminal to list hidden files and directories:
dir -a
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:
echo .*
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:
#!/bin/bash
echo “before hiding the sample”
ls -a
mv sample .sample
echo “after hiding the sample”
ls -a
With the mv sample .sample
command, the mv command has changed the file name from sample to .sample and this makes the sample file hidden.
In the above image, you can see that the mv command has made the sample file hidden.
Note: To hide a directory or folder use the syntax: mv folder_name .folder_name
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:
lsh
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.
Conclusion
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
What are hidden files in Linux?
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 dot (.)
.
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”.
How do I see the size of hidden files in Linux?
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.
How do I edit hidden files in Linux?
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.
Which command will list the hidden file type in Unix?
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.
FUNDAMENTALS A Complete Guide for Beginners