LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-13-2009, 11:34 AM   #1
itmozart
LQ Newbie
 
Registered: Feb 2008
Location: Dublin
Distribution: Ubuntu Jaunty
Posts: 3

Rep: Reputation: 0
Lightbulb GPG - encode file while manipulating output file name


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 11:36 AM. Reason: added tags
 
Old 09-14-2009, 04:25 AM   #2
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
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.
 
Old 10-03-2009, 12:28 PM   #3
itmozart
LQ Newbie
 
Registered: Feb 2008
Location: Dublin
Distribution: Ubuntu Jaunty
Posts: 3

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


Reply

Tags
bash, gpg, sed, stat



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 9 07-30-2018 07:49 AM
manipulating one file resource with multiple threads linuxsavar Programming 1 10-01-2008 03:25 PM
Manipulating Text File with awk or sed kushalkoolwal Programming 2 09-10-2008 07:35 PM
encode text file anhtt Solaris / OpenSolaris 8 04-03-2008 12:03 AM
Manipulating Fields Of Flat File Prasun1 General 10 09-29-2005 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:30 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
Open Source Consulting | Domain Registration