Solved

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

Posted on 2010-09-08
6
544 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

838 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