How to write a script for text file encryption?

Hi All,

I have zero knowledge in UNIX, so i desperately need the help from you guys.

I need a script which can encrypt all the text files in one shot in a source directory.
After all the text files are encrypted, they will be put into a separate folder.

Command involve:
gpg with -a, --armor

eg.
Before encyption:
/main/project/sorce/file1.txt
/main/project/sorce/file2.txt
/main/project/sorce/file3.txt

Before encyption:
/main/project/target/file1.txt.gpg
/main/project/target/file2.txt.gpg
/main/project/target/file3.txt.gpg

You can process multiple files with the following option to gpg:

gpg -r "somename" --encrypt-files `ls /main/project/sorce/*.txt`

once you get the *.gpg files move them

mv /main/project/sorce/*.gpg /main/project/target/

If you want you can put the above 2 commands into a file and make it into a shell script, but not needed.

Thanks.

How to make it into shell script?
Is there any script that is able to automatically encypt whenever there are new txt files are dropped into the /source folder?

Please advise.

Sample code - the script handles one file at a time.
Enhance it for your need.

#!/bin/bash
#Script Name : encrypt.sh
#run the script in the background and forget about it i.e. ./encrypt.sh &
old_file_count=0
while true; do
  curr_file_count=`ls | wc -l`
  if [ $curr_file_count -gt $old_file_count ]; then
    old_file_count=$curr_file_count
    new_file=`ls -rt | tail -1`
    echo "New file found : $new_file : do whatever you want now"
    #gpg -r $new_file.gpg --encrypt-files $new_file
    #mv $new_file.gpg /where/ever/you/want
  fi
  #Change the sleep timer to your need
  sleep 5
done

You can also add this as a cronjob with necessary changes.

--ahamed

The following is our manual command to encypt those txt files in source folder:
gpg --verbose --armour --encrypt --recipient "IMS_L2" --output Jan_Payment.pgp Jan_Payment.txt

How can I integrate it into the new shell script?

...
gpg --verbose --armour --encrypt --recipient "IMS_L2" --output $new_file.pgp $new_file.txt
...

Like this?

--ahamed

Im testing this code in a development environment.
As far as i know, in the production it need a public key to do the encryption.
Where can i find the public key?
Is this the public key "IMS_L2"?

---------- Post updated at 07:32 AM ---------- Previous update was at 07:27 AM ----------

"

gpg :encryption of 'ls /home/taurus/project/source/*.txt failed:public key not found

"
This is one of the msg I encountered,