Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Automated Recursive Encryption in a Directory Using Shell Script

Improve Article
Save Article
Like Article
Improve Article
Save Article
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



My Personal Notes arrow_drop_up
Last Updated : 21 Nov, 2019
Like Article
Save Article
Similar Reads