Open In App

Automated Recursive Encryption in a Directory Using Shell Script

Last Updated : 21 Nov, 2019
Like Article

This script would encrypt file provided as an argument or a directory and its constituent files and sub-directories recursively. It would be very useful for automating the encryption of multiple files in a directory all together.

How it works?

  1. If no arguments are given, throw an error and exit the program.
  2. Read the password to use for encrypting the files in a variable.
  3. Find the path of the given argument by realpath.
  4. Create a function to check the provided argument is a directory or a file.
  5. If a directory is given, get the files recursively under that directory and sort and remove the duplicate entries in the files list by ls | uniq and store it in an array.
  6. Pass the output of ls | uniq to the function one by one by indices of the created array in a loop i.e calling the same function recursively.
  7. If a file a is given, do step 8
  8. Loop through each file and encrypt the file with the builtin ‘gpg‘ utility with the given password.
  9. If the ‘gpg‘ utility exits with a non-zero exit code, throw the error message and end the program. Else delete the original file and show the encrypted file’s name to indicate that it is successfully encrypted.

Source Code: You can use any editor on Linux like nano and save this as You may face some problems with the permission of this file while executing this shell script. Don’t worry, just use the command sudo chmod 774 to come over this problem.

#In this line we check that an
#argument is provided or not
if [ -z "$1" ]; then  
echo "No file provided" >&2
exit 1
#reading the password which would
#be used for encryption
read -p "Enter the password" pass 
function func() 
#checking if the provided
#argument is a directory
if [ -d `realpath $file_name` ]; then 
#going to the location
#of the directory
cd `realpath $file_name` 
#saving the output of the 
#“ls | uniq” in an array
array=(`ls | uniq `)     
#storing the length of the
#array in a variable
#looping through all the 
#indices of the array
while [ $i -lt $len ]; do   
#displaying the file in 
#the index right now
echo "${array[$i]}" 
#recursively calling the function
func ${array[$i]} 
#increasing  the counter
let i++ 
#checking if the argument is a file
if [ -f `realpath $file_name` ]; then 
#encrypting the file with the given password 
#and storing the return value of the gpg 
#in a variable
test= echo $pass | gpg -c --batch --yes --passphrase-fd 0 $file_name   
#checking if the gpg 
#executed properly or not
if [ "$test" == "1" ]; then 
#writing to the standard error
echo "Bad signature: gpg not executed properly" >&2 
exit 1
#checking if the gpg 
#executed properly or not
elif [ "$test" == "2" ]; then 
#writing to the standard error
echo "unexpected error: gpg not executed properly" >&2 
exit 1
#deleting the original file
rm $file_name 
#displaying the gpg created
echo " $file_name.gpg "     
func $1



Previous Article
Next Article

Similar Reads

Shell Script to Delete a File from Every Directory Above the Present Working Directory
In this article, we will discuss how to delete a file from every directory above the parent working directory using shell script in Linux. To delete a file from a directory we use the rm command in Linux/UNIX. Syntax: rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from every directory above the present working directo
2 min read
Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user's shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes. echo "$SHELL" will expand the $S
4 min read
Bash Script - Difference between Bash Script and Shell Script
In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capable of running a script. A script that is passed to
3 min read
Implementing Directory Management using Shell Script
Directory management constitutes the functions dealing with organization and maintenance of various directories. Directories usually contain files of any type, but this may vary between file systems. The content of a directory does not affect the directory object itself. Some of the directory functions are: Navigation Absolute/ Relative Pathnames L
3 min read
Shell Scripting - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 min read
Shell Script To Show Names of All Sub-Directories Present in Current Directory
In this given program, we are required to write a shell script to print the list of all the sub-directories present in the current directory. Currently, when we are running this program, the terminal is opened at the root directory location and hence, we are getting a list of sub-directories present at the root level. Moreover, this script comes in
2 min read
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
2 min read
Shell Script to Download Files From a Source Directory on a Remote FTP Server
In this article, we are going to write a shell script to download files from a certain directory in a server to a local computer using FTP. It is recommended to have some knowledge of FTP before going through this article, but it is not necessary. Before writing a shell script, we will see how to download a file directly using commands, then we wil
4 min read
Shell Script to Delete Zero Sized Files From a Directory
A zero-sized file is a file that contains no data and has a length of 0. It is also called a zero-byte file. Incomplete transfers can be responsible for the zero-sized file. You can create a zero-sized file intentionally by running the following command in the terminal : touch zero_sized_file_name Output: [caption width="800"]zero-sized file[/capti
3 min read
Shell Script to Check if Every Passed Argument is a File or Directory
While automating things in Linux we always work on the file and directories. And sometimes we pass the files and directories as the arguments to the shell scripts. And we have to determine whether the provided argument is a file or a directory, and today we are going to see how to check whether provide argument is the file or directory. Before movi
4 min read
Article Tags :