Open In App

Shell Script to List all Hidden Files in Current Directory

Here we are going to see how to write a script to list all hidden files in the current directory, But before starting we will see how to hide the file in the current directory.

Hide Files in Linux:

In Linux, the files which start with a period (.) sign are the hidden files. We will write a small script to hide the file by providing the name of the file as an input to the console.



Code:

#!/bin/bash
echo "This is to HIDE any file."
echo "Enter the name of the file  in the current directory:"
read file
# a (.) or period sign is used to hide any file in linux
mv $file .$file
echo ".....The file has been hide successfully....."



Give rwx permissions

# chmod 777 hide.sh

Make an empty file so that we could hide it and also check if it is created.

# touch file1.txt
# ls

File has been created.

Execute the script and give the file name as input.

# ./hide.sh

The above code has just changed the name of the original file adding a period (.) at the start.

List Hidden Files in Linux

To find the hidden files we will use the ‘find’ command which has many options which can help us to carry out this process.

Code:

#!/bin/bash
dir=$(pwd)
echo -e "We will list all the Hidden file in the current Directory $dir"
# find <path> <pattern> <action>
find . -type f -name ".*" -ls

Give Permissions and run the file.

# chmod 777 listHide.sh
# ./listHide.sh

Hence, the hidden files have been listed. We could also see our “.file1.txt” which we made hidden earlier.

Article Tags :