Automated Recursive Encryption in a Directory Using Shell Script Last Updated : 21 Nov, 2019 Comments Improve Suggest changes Like Article Like Report 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? If no arguments are given, throw an error and exit the program. Read the password to use for encrypting the files in a variable. Find the path of the given argument by realpath. Create a function to check the provided argument is a directory or a file. 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. 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. If a file a is given, do step 8 Loop through each file and encrypt the file with the builtin 'gpg' utility with the given password. 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 s1.sh. 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 s1.sh to come over this problem. BASH #!/bin/bash #In this line we check that an #argument is provided or not if [ -z "$1" ]; then echo "No file provided" >&2 exit 1 fi #reading the password which would #be used for encryption read -p "Enter the password" pass function func() { file_name=$1 #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 len=${#array[*]} i=0 #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++ done fi #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 else #deleting the original file rm $file_name #displaying the gpg created echo " $file_name.gpg " fi fi } func $1 Output: Comment More infoAdvertise with us Next Article Automated Recursive Encryption in a Directory Using Shell Script A Aman884036 Follow Improve Article Tags : Linux-Unix Similar Reads 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 functi 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 prov 4 min read Linux Shell Script to Backup Files and Directory Backing up our important files and directories is crucial to safeguarding our data. In this article, weâll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script weâll build allows us to specify which 5 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 scr 2 min read 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 2 min read Like