Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-02-2011, 12:33 PM   #1
Registered: Aug 2007
Posts: 346

Rep: Reputation: 31
bash simple cleanup script then curl

Not a programmer, but did manage to get some good rsync scripts, some scp things, each is wrapped around an if then where if things work, I curl a webpage which will ack the process.

I now want a cleanup script on the server that cleans x amount of day's and upon completion of all the cleanups, simply do the same, but a little lost in the tree, and I am sure it's something simple.

So I am using the following example;
if find /backup/server/folder1 -type f -mtime +3;
 then find /backup/server/folder1 -type f -mtime +3 -exec rm -rf {};
elseif find /backup/server/folder2 -type f -mtime +3
 then find /backup/server/folder2 -type f -mtime +3 -exec rm -rf {};
The server already checks to see if anything is late, so I don't need a check here for that, just want to say cleanup folder1, folder2 if no errors, run the curl script or just drop out.

*note: I'm sure there is a way to use the results from line 1 in the delete line rather than a re-find. Things are small and quick, but if there is way to say $1 rm -rf, etc. feel free to throw that out as well.*

Last edited by sir-lancealot; 02-02-2011 at 12:34 PM.
Old 02-02-2011, 01:59 PM   #2
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,194

Rep: Reputation: 105Reputation: 105
Not sure what your objective is or whether the logic of your "if" is what you want.

What you are doing: If there is anything in folder1 that was last modified at least 3 days ago, then remove it and drop out of that whole segment of script you are showing. If (and only if) nothing was found in folder1, then -> if there is anything in folder2 that was last modified at least 3 days ago, then remove it, do the curl command, and drop out of that whole segment of script you are showing. If nothing is found in either folder, do nothing.

If your intention is to remove things older than three days from both folders, then getting rid of the whole "if" structure would simplify things. Is the curl just to say that you did it? If so, then the following would work.

find /backup/server/folder1 -type f -mtime +3 -exec rm -rf {} \;
find /backup/server/folder2 -type f -mtime +3 -exec rm -rf {} \;
You could even change those two finds to one line

find /backup/server/folder1 /backup/server/folder2 -type f -mtime +3 -exec rm -rf {} \;
Unfortunately, if you want to know whether anything was found, it gets more complicated. The linux man page for find indicates the following about the exit status:

find exits with status 0 if all files are processed successfully, greater than 0 if 
errors occur. This is deliberately a very broad description, but if the return value 
is non-zero, you should not rely on the correctness of the results of find.
Playing around with some test cases indicates that your "if" structure will always execute the first case and never check folder2 or do the curl, unless, of course, your script encounters an error, such as access denied to folder1.

Last edited by choogendyk; 02-02-2011 at 02:01 PM.


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
Simple bash script help zok Linux - General 4 12-15-2006 12:56 PM
Simple bash script lp449 Linux - Networking 3 08-02-2005 05:25 PM
Simple bash script Soulful93 Programming 1 06-04-2005 08:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 08:35 AM
Help with simple bash script - please tw001_tw Linux - Software 5 12-19-2004 10:02 PM > Forums > Non-*NIX Forums > Programming

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

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