Solved

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

Posted on 2010-09-08
6
539 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now