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.
- Implementing Directory Management using Shell Script
- How to check if a directory or a file exists in system or not using Shell Scripting?
- Introduction to Linux Shell and Shell Scripting
- Bash shell script to find sum of digits
- Bash shell script to swap two numbers
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
- String Operators | Shell Script
- Bash shell script to find out the largest value from given command line arguments
- Difference Between Single and Double Quotes in Shell Script and Linux
- Automated Login For Captive Portals in Linux
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Create a password generator using shell scripting
- Getting System and Process Information Using C Programming and Shell in Linux
- How to find time taken by a command/program on Linux Shell?
- Making your own Linux Shell in C
- A Shell program To Find The GCD | Linux
- Reverse a String | Shell Programming
- Array Basics in Shell Scripting | Set 1
- Developing a Linux based shell
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.