-   Programming (
-   -   bash simple cleanup script then curl (

sir-lancealot 02-02-2011 12:33 PM

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.*

choogendyk 02-02-2011 01:59 PM

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.

All times are GMT -5. The time now is 09:15 PM.