LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   GPG Encrypt without filename (https://www.linuxquestions.org/questions/linux-newbie-8/gpg-encrypt-without-filename-4175507820/)

jonnybinthemix 06-12-2014 03:47 AM

GPG Encrypt without filename
 
Hey Guys,

Is it possible to pass the filename of a file to be encrypted to the GPG Command through a variable?

I need to encrypt the latest file within a directory.

So, to asscertain the filename of the latest file I've got:

Code:

find . -type f -print0|xargs -0 ls -drt|tail -n 1 > $LATEST
And then I'd have thought something like:

Code:

gpg -r email@email.com -e $LATEST
However that does not work, it just encrypts the file that contains the location of the latest file.

Is there a way that I could print or echo the contents of this variable to the end of the command?

Thanks in advance.

Jon

business_kid 06-12-2014 04:19 AM

instead of
Quote:

gpg -r email@email.com -e $LATEST
, maybe

Code:

cat $LATEST | gpg -r email@email.com  -
the '-' being a symbol for stdin. It may work - it's probably worth a try.

jonnybinthemix 06-12-2014 04:51 AM

Hey,

Thanks for the recommendation however it does not work.

Any other ideas? :)

J

jonnybinthemix 06-12-2014 05:25 AM

Is there anything generic that can be done to append the contents to the end?

I've read something about 'sed' but I don't know enough about it to know how to manipulate it for what I want.

chrism01 06-12-2014 06:40 AM

Possibly
Code:

gpg -r email@email.com < $LATEST
?

cepheus11 06-12-2014 07:06 AM

You don't need an intermediate variable. This should work:

Code:

gpg -r email@email.com -e "$(find . -type f -print0|xargs -0 ls -drt|tail -n 1)"
The quotes are necessary only if the filename might contain spaces.

jonnybinthemix 06-12-2014 08:28 AM

Cephus you're a legend, that's exactly how I wanted it to work :)

I've actually changed the way I get the file name now so I've amended the code towards the end but the result is perfect :)

I've got

Code:

DATE=$(date +%d%m%y)

gpg -r user@domain.com -e "$(find . -type f -name *.csv | grep $DATE)"

As the filename will contain the date I thought it best to search for the filename as opposed to the newest file.

I do have another question while we're on a role...

So at the moment that's going to find the file in the list of files with todays date in it, and then encrypt it. It works and does it fine.

What if there are 2 or 3 files with todays date? Would I be able to create a 'for loop' where it will run the command for all files it finds with todays date in?

Thanks
Jon

cepheus11 06-12-2014 09:36 AM

Quote:

Originally Posted by jonnybinthemix (Post 5186996)
Would I be able to create a 'for loop' where it will run the command for all files it finds with todays date in?

Yes, this should do it:

Code:

find . -type f -name "*.csv" | grep $DATE | while read i; do gpg -r user@domain.com -e "$i"; done
The technique from the previous post "$(...)" is called "command substitution", while this here pipes the output between commands and a while loop, which uses the variable $i in each run, containing the file path. Gpg is called multiple times if you have multiple files.

And if the $DATE is part of the filename, you can incorporate it into the find command and get rid of the additional pipe to grep. Also, it will not falsely find subdirectories with the $DATE in their name:

Code:

find . -type f -name "*$DATE*.csv" | while read i; do gpg -r user@domain.com -e "$i"; done

jonnybinthemix 06-12-2014 09:37 AM

I've managed to adjust things again to get rid of the use for a variable... which I'm happier with:

Code:

find . -type f -name "*.csv" | grep $(date +%d%m%y)
But I guess I need to run this command and then put all results into a loop and get the loop to process the encrypt command for all entries it finds?

jonnybinthemix 06-12-2014 09:38 AM

Apologies Cepheus I've just noticed your response.. just reading it now.

Thanks for your help, will let you know how I get on.

Thanks
Jon

jonnybinthemix 06-12-2014 09:47 AM

This is getting close, I can feel it :) lol

I've adjusted it as follows:

Code:

find . -type f -name "*$(date +%d%m%y)*.csv" | while read i; do

gpg -r user@domain.com -e "$i;"

done

However, it results in the error:

gpg: can't open `./120614101530.csv;': No such file or directory
gpg: ./120614101530.csv;: encryption failed: No such file or directory

Like it's expecting to find a directory?

Have I done something wrong?

szboardstretcher 06-12-2014 09:53 AM

remove the extra semicolon in "$i;"

jonnybinthemix 06-13-2014 03:08 AM

Ah yes that works great.. :)

I just have one more question.. lol :-)

I had originally written a script that would encrypt a single file (by filename) and upload it to a different server via FTP. With this method in mind, could I use a similar loop to upload the files it's just encrypted?

What I've got so far looks like this:

Code:

HOST=ftp.server
USER=ftp.user
PASS=ftp.pass
FTPLOG=/tmp/ftplogfile

find /sitsimp -type f -name "*$(date +%d%m%y)*.csv" | while read i; do

gpg -r server@bathspa.ac.uk -e "$i"; done

ftp -inv $HOST <<! > $FTPLOG

quote USER $USER

quote PASS $PASS

put $LATEST

bye
!

if fgrep "226 Transfer complete" $FTPLOG ; then
        echo "FTP TRANSFER SUCCESS"
else
        echo "FTP TRANSFER ERROR."

fi

exit 0


jonnybinthemix 06-13-2014 03:27 AM

I took a punt on something like this...

Code:

find /sitsimp -type f -name "*$(date +%d%m%y)*.csv.gpg | while read i do put $i; done
but it didn't work.

Am I on the right kind of track or would I need to take a different approach to the initial find command?

jonnybinthemix 06-13-2014 04:05 AM

Sorry to bombard the thread.. but trying to keep it updated with the things I'm trying out, and the ideas I've been having.

My latest thought is something along the lines of:

Code:

find /sitsimp -type f -name "*$(date +%d%m%y)*.csv.gpg" > $FILES

ftp -inv $HOST <<! > $FTPLOG

quote USER $USER

quote PASS $PASS

put $FILES

bye
!

It doesn't appear to work.. but it doesn't error.. just nothing gets uploaded. If I look at the contents of the variable it does contain the list of files needed though...


All times are GMT -5. The time now is 02:46 PM.