Solved

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

Posted on 2010-09-08
6
547 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

730 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