LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-12-2014, 03:47 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
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
 
Old 06-12-2014, 04:19 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 8,021

Rep: Reputation: 801Reputation: 801Reputation: 801Reputation: 801Reputation: 801Reputation: 801Reputation: 801
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.
 
Old 06-12-2014, 04:51 AM   #3
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
Hey,

Thanks for the recommendation however it does not work.

Any other ideas?

J
 
Old 06-12-2014, 05:25 AM   #4
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-12-2014, 06:40 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
Possibly
Code:
gpg -r email@email.com < $LATEST
?
 
Old 06-12-2014, 07:06 AM   #6
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 280

Rep: Reputation: 88
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.
 
1 members found this post helpful.
Old 06-12-2014, 08:28 AM   #7
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-12-2014, 09:36 AM   #8
cepheus11
Member
 
Registered: Nov 2010
Location: Germany
Distribution: Gentoo
Posts: 280

Rep: Reputation: 88
Quote:
Originally Posted by jonnybinthemix View Post
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

Last edited by cepheus11; 06-12-2014 at 09:39 AM. Reason: typo
 
Old 06-12-2014, 09:37 AM   #9
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-12-2014, 09:38 AM   #10
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-12-2014, 09:47 AM   #11
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-12-2014, 09:53 AM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,204

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
remove the extra semicolon in "$i;"
 
Old 06-13-2014, 03:08 AM   #13
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-13-2014, 03:27 AM   #14
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-13-2014, 04:05 AM   #15
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is is possible/practical to encrypt large attachments with GPG? abefroman Linux - Security 3 10-16-2009 03:58 PM
What is the best way to encrypt emails? GPG? abefroman Linux - Security 5 08-28-2009 04:54 AM
LXer: Encrypt Your Email With GPG and Thunderbird LXer Syndicated Linux News 0 04-24-2009 07:30 PM
GPG wont encrypt file. acidblue Linux - Software 3 05-07-2006 05:05 PM
GPG encrypt text in terminal max-ebb Linux - Security 3 10-06-2004 06:51 PM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration