?
Solved

Bash Script to Encrypt All GZ files and Then Remove them from SubFolders

Posted on 2010-09-08
6
Medium Priority
?
551 Views
Last Modified: 2013-12-21
Hi Experts.

I need a Linux BASH script to find all of the "*.tar.gz" files in all of the immediate sub-folders and then run "gpg" to encrypt them, (see command example below) and afterwards remove them if the encryption is succssfully completed. Ialso  need to this to be able to run unattended as a CRON job.

Cheers,
Keith.
gpg --no-secmem-warning --verbose --encrypt-files -r user@domain.com /home/account/backup/folder1/*.tar.gz

Open in new window

0
Comment
Question by:NetSoftDS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 33634048
> "*.tar.gz" files in all of the immediate sub-folders
You mean in all subfolders of given folder, or in this very given folder only(excuse me my poor English understanding)?
first case
 find /home/account/backup/folder1/ -name '*.tar.gz' | while read f; do gpg -options "$f" && rm -fv "$f"; done
latter case
 find /home/account/backup/folder1/ -maxdepth 1 -name '*.tar.gz' | while read f; do gpg -options "$f" && rm -fv "$f"; done
0
 

Author Comment

by:NetSoftDS
ID: 33644574
Hi ravenpl,

The First Option is the one I'm after.

Could you clarify yoyr anser a little for me and include all of the gpg options that I showed in my original question thanks?

gpg --no-secmem-warning --verbose --encrypt-files -r user@domain.com /home/account/backup/folder1/*.tar.gz

Cheers
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 33644851
find /home/account/backup/folder1/ -type f -name '*.tar.gz' | while read f; do gpg --no-secmem-warning --verbose --encrypt-files -r user@domain.com "$f" && rm -fv "$f"; done

find /home/account/backup/folder1/ -type f -name '*.tar.gz' # finds all files matching '*.tar.gz' in given folder, that output is piped to
while read f; do CMD; done # which reads line by line(filenames one by one) and feeds to CMD
gpg --no-secmem-warning --verbose --encrypt-files -r user@domain.com "$f" && rm -fv "$f" # does the encryption and removes the file only if encrypting hasn't failed
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 

Author Comment

by:NetSoftDS
ID: 33644894
Very neat... can you confirm that "find" will search all of the sub-folders below "/home/account/backup/" if I call it as "find /home/account/backup/"?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 33644905
Yes, it will list all names in all subfolders.
Note: I added "-type f" to list regular files only
0
 

Author Comment

by:NetSoftDS
ID: 33644910
Don't worry just confirmed it myself... nice script. Thanks very much. I've awarded the points.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question