Shell script to decrypt and move files from one directory to another directory

I have a directory and there are several files in it. I am trying to decrypt these files and move them to another directory. I cannot figure out how to set the name of the output file and move it.

So, the directory structure is as follows:

/Applications/MAMP/bin/decryptandmove.sh /Applications/MAMP/bin/passtext.txt /Applications/MAMP/bin/encrypted/test1.txt.pgp /Applications/MAMP/bin/encrypted/test2.txt.pgp /Applications/MAMP/htdocs/www/decrypted/ 

For all the files found in the encrypted directory, I try to decrypt them and then move them to the www / decrypted / directory. I don’t know which file names in the encrypted directory will be ahead of time (this script will ultimately be executed through the cron task), so I just wanted to output decrypted files with the same file names, but without pgp. Thus, the result will be:

 /Applications/MAMP/bin/decryptandmove.sh /Applications/MAMP/bin/passtext.txt /Applications/MAMP/bin/encrypted/ /Applications/MAMP/htdocs/decrypted/test1.txt.pgp /Applications/MAMP/htdocs/decrypted/test2.txt.pgp 

So that’s all I’ve written so far, and it won’t work. FILE and FILENAME are wrong. I didn’t even get to the moving part.

 pass_phrase=`cat passtext.txt|awk '{print $1}'` for FILE in '/Applications/MAMP/bin/encrypted/'; do FILENAME=$(basename $FILE .pgp) gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE done 
+2
shell encryption
source share
3 answers
 #!/bin/bash p=$(<passtext.txt) set -- $p pass_phrase=$1 destination="/Applications/MAMP/htdocs/www/decrypted/" cd /Applications/MAMP/bin/encrypted for FILE in *.pgp; do FILENAME=${FILE%.pgp} gpg --passphrase "$pass_phrase" --output "$destination/$FILENAME" --decrypt "$FILE" done 
+1
source share

This should fix the problems of FILENAME and FILE, although it will only work if you are currently in the "decrypted" directory. If you want to fix this, you will have to add the correct directory to the FILENAME variable :)

 pass_phrase=`cat passtext.txt|awk '{print $1}'` for FILE in /Applications/MAMP/bin/encrypted/*; do FILENAME=$(basename $FILE .pgp) gpg --passphrase $pass_phrase --output $FILENAME --decrypt $FILE done 
0
source share

I like to make cd first:

 cd /Applications/MAMP/bin/encrypted/ 

Then

 for FILE in $(ls); do ... 

or

 for FILE in `ls`; do ... 

I personally prefer:

 ls | while read FILE; do ... 

Then maybe

 cd - 

Important. If you use the ls approach, make sure your file names do not contain spaces. See also the link in the comment ghostdog74 (thanks, BTW) - this page is usually quite useful.

0
source share

All Articles