LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2017, 09:28 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 91

Rep: Reputation: Disabled
Filtering Boolean fun and confusion


I'm writing a bash script to backup scripts and have written it in such a way that it backs them up 3 times a day, 00:00 13:00 & 19:00. This way if I screw us a script then I have a backup less than a few hours old to roll back too.
So after running the backup script over time I end up with a file names like this:

Code:
-rw-r--r-- 1 root root 2.1K Dec  5 00:00 p5e_root_scripts_171205_0000.7z
-rw-r--r-- 1 root root 2.1K Dec  6 00:00 p5e_root_scripts_171206_0000.7z
-rw-r--r-- 1 root root 2.1K Dec  6 19:00 p5e_root_scripts_171206_1900.7z
-rw-r--r-- 1 root root 2.1K Dec  7 00:00 p5e_root_scripts_171207_0000.7z
-rw-r--r-- 1 root root 2.1K Dec  7 13:00 p5e_root_scripts_171207_1300.7z
Every month I backup all these scripts, however it's a lot of them.
So I remove any files older than a day and not ending in 0000.7z like this:
Code:
find /home/tim/Documents/$HOSTNAME/scripts/$THEUSER/ -type f ! -iname "*0000.7z" -mmin +1440 -exec rm -f {} \
;

That greatly reduces the amount of backups. I would like to go a step further and remove any files older than a week and that were created at 0000 (and here comes the hard part) but NOT on the 1,6,12,18 & 24th of each month. I think the code below will do it:

Code:
find /home/tim/Documents/$HOSTNAME/scripts/$THEUSER/ -type f -iname "*0000.7z" -mtime +7 ! \( -iname \*01_????.7z -o -iname \*06_????.7z -o -iname \*12_????.7z -o -iname \*18_????.7z -o -iname \*24_????.7z \)-exec rm -f {} \;
Therefore I should end up at the end of each month with 4 or 5 backups ending in 0000.7z each 5 days apart and two or so ending 1300.7z & 1900.7z

I don't want to run it in case I accidentally delete more than I bargained for. Does that last bit of script look like it will work as intended?

Many thanks for any help
Tim

Last edited by Entropy1024; 12-07-2017 at 09:31 AM.
 
Old 12-07-2017, 09:56 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,574

Rep: Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929
Quote:
Originally Posted by Entropy1024 View Post
I don't want to run it in case I accidentally delete more than I bargained for.
So run it with the rm replaced with ls and see if it gives you the list of files you want rid of.

Also, when you're ready, why not use -delete at the end than -exec rm
 
1 members found this post helpful.
Old 12-07-2017, 10:07 AM   #3
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 91

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
So run it with the rm replaced with ls and see if it gives you the list of files you want rid of.

Also, when you're ready, why not use -delete at the end than -exec rm
That's a great tip thanks. So obvious in hindsight

Is there any difference in the -exec rm method to -delete? I'm guessing it does exactly the same thing but easier to type. The method I use is simply the method I was shown.

Cheers
Tim
 
Old 12-07-2017, 10:10 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,574

Rep: Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929Reputation: 929
Quote:
Originally Posted by Entropy1024 View Post
That's a great tip thanks. So obvious in hindsight
Yeah, isn't it

Quote:
Originally Posted by Entropy1024 View Post
Is there any difference in the -exec rm method to -delete?
Using -exec calls the external rm command, -delete does it "internally". Using exec may need the full path to /bin/rm if used in a script or cron job.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
content filtering with layer7- filtering rose1366m Linux - Networking 1 05-04-2011 12:10 PM
<fun> The Windows Crash </fun> Simon Bridge General 6 08-26-2007 08:46 PM
boolean question! xemous General 3 03-18-2005 04:40 AM
Sendmail Spam filtering and Virus filtering MrJoshua Linux - General 2 04-03-2003 11:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration