Solved

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

Posted on 2010-09-08
6
543 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
  • 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 500 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need To Change Centos 6 Timestamp Form In Log File 24 70
str_replace not working in php script 4 26
Dynamic varibles 5 32
Google Maps API and PHP 25 27
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 is …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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