LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 09-13-2009, 12:34 PM   #1
itmozart
LQ Newbie
 
Registered: Feb 2008
Location: Dublin
Distribution: Ubuntu Jaunty
Posts: 3
Thanked: 0
Lightbulb GPG - encode file while manipulating output file name


[Log in to get rid of this advertisement]
I'd want to encrypt some files with GPG, named, as example "1.txt" and "2.txt".

So far so good, I can use something like:

Code:
find /tmp -name '*.txt' | xargs -I {} gpg -r test@test.com --output {}.enc --encrypt {}
Problem is, that I want to add the date to the filename.

Now, with a quite awkward stat+sed I can generate the filename (excerpt):

Code:
stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/'
(note: the '.' in '.txt' is inappropriate but acceptable)

How can I incorporate this last statement as output file name in the find/xargs/gpg output parameter?

If I do something like:

Code:
find /tmp -name '*.txt' | xargs -I {} gpg -r test@test.com --output $(stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/') --encrypt {}
The $() inputs the {} literally.

This won't work either:

find /tmp -name '*.txt' | xargs -I {} sh -c "cat {} | gpg -r test@test.com > $(stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/')"

Is there a simple[r] oneliner for doing this operation?

Thanks!
Saverio

Last edited by itmozart; 09-13-2009 at 12:36 PM.. Reason: added tags
linuxubuntu itmozart is offline  
Tag This Post , , ,
Reply With Quote
Old 09-14-2009, 05:25 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Thanked: 136
Quote:
Originally Posted by itmozart View Post
I'd want to encrypt some files with GPG, named, as example "1.txt" and "2.txt".

So far so good, I can use something like:

Code:
find /tmp -name '*.txt' | xargs -I {} gpg -r test@test.com --output {}.enc --encrypt {}
Problem is, that I want to add the date to the filename.

Now, with a quite awkward stat+sed I can generate the filename (excerpt):

Code:
stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/'
(note: the '.' in '.txt' is inappropriate but acceptable)

How can I incorporate this last statement as output file name in the find/xargs/gpg output parameter?

If I do something like:

Code:
find /tmp -name '*.txt' | xargs -I {} gpg -r test@test.com --output $(stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/') --encrypt {}
The $() inputs the {} literally.

This won't work either:

find /tmp -name '*.txt' | xargs -I {} sh -c "cat {} | gpg -r test@test.com > $(stat -c%n%y {} | sed -r 's/(.*).txt(\w+)-(\w+)-(\w+).*/\1_\2\3\4.txt/')"

Is there a simple[r] oneliner for doing this operation?

Thanks!
Saverio
You know, writing scripts should not be an obfucscation contest. Maybe it would be better to stop trying to see whether any problem can be solved using a long, incomprehensible command line. Please reconsider, open a script file and type some clear, comprehensible instructions:

Code:
path="/path/of/interest"

find $path | egrep "\.txt$" | while read inpath
do
   date=`date`
   date=${date// /_}
   outpath="${inpath//.txt}_${date}.enc"
   gpg --encrypt-files $inpath --recipient test@test.com --output $outpath 
done
Notice that your one-liner, once it actually worked, would be longer than this formal, transparent, easily understood, solution to your problem.

And please understand that the ultimate victim of miraculous one-liners is ... you, six months from now, when you have to try to decipher what the one-liner actually does.

I will never understand why people insist on using "xargs" and then can't understand why it won't give them what they want. The method I posted above takes longer to run (because it creates a separate process for each file), but overall it's faster because it saves development time.

One more comment. In the future, when parallel processing is fully mature, methods like this (that spawn one process per file) will actually run faster than the alternatives.
linuxfedora lutusp is offline     Reply With Quote
Old 10-03-2009, 01:28 PM   #3
itmozart
LQ Newbie
 
Registered: Feb 2008
Location: Dublin
Distribution: Ubuntu Jaunty
Posts: 3
Thanked: 0

Original Poster
Quote:
Originally Posted by lutusp View Post
You know, writing scripts should not be an obfucscation contest. Maybe it would be better to stop trying to see whether any problem can be solved using a long, incomprehensible command line. Please reconsider, open a script file and type some clear, comprehensible instructions:
Well, ok, I give up the obfuscation contest ;-)
One lines had some advantages, but in this case you're right, it's pushing too further.

Thanks!
Saverio
linux itmozart is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
[SOLVED] gpg: WARNING: unsafe permissions on configuration file `/home/b/.gnupg/options' gpg: widda Mandriva 8 09-05-2009 10:37 AM
manipulating one file resource with multiple threads linuxsavar Programming 1 10-01-2008 04:25 PM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 08:35 PM
encode text file anhtt Solaris / OpenSolaris 8 04-03-2008 01:03 AM
Manipulating Fields Of Flat File Prasun1 General 10 09-29-2005 12:42 PM


All times are GMT -5. The time now is 02:31 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration