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.
- How to find time taken by a command/program on Linux Shell?
- Create Directory or Folder with C/C++ Program
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Introduction to Linux Shell and Shell Scripting
- Write a bash script to print a particular line from a file
- Making your own Linux Shell in C
- A Shell program To Find The GCD | Linux
- Reverse a String | Shell Programming
- Array Basics Shell Scripting | Set 2 (Using Loops)
- Array Basics in Shell Scripting | Set 1
- Bash shell script to find sum of digits
- Developing a Linux based shell
- Bash shell script to swap two numbers
- Looping Statements | Shell Script
- Conditional Statements | Shell Script
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.