cron job for cleaning files
I want to write a script, which run as a cronjob.
It looks for a folder (/tmp/cache) every hour. If any files's one day old, delete them. I'm new to script. My questions are: 1. how to check time stamp of a file? 2. how to make the folder (/tmp/cache) configurable? read from properties file? Thanks in advance |
Quote:
Code:
find /tmp/cache -mtime +1 -exec rm -f {} \; As to question 2, I am not sure I understand what you mean. Edit: the find command that I mentioned before will find files that +1 day old and DELETE them. |
Thank you for the quick response!
the +1 after -mtime is number of days, right? Instead, how to specify in hours, like 24 hours? I googled the following result. Any differece? /usr/bin/find /tmp/cache -type f -mtime +1 | xargs -r /bin/rm Question 2 folder /tmp/cache is configured in a properties file. A Java program looks at this value from the properties file and write files to the location. I can't hard code /tmp/cache in the script (otherwise it gonna fail when "/tmp/cache" is changed in the properties file.). Can you suggest how to deal with this? |
Quote:
1. -mmin 5 - it would find files modified exactly 5 minutes ago 2. -mmin +5 - it would find files modified at least 5 minutes ago 3. -mmin -5 - less than 5 minutes ago Check 'man find' for all other options. As to the differences between xargs and exec, I believe xargs will be a more efficient option as -exec will create a new process for each file that is sent to it by 'find'. When it comes to the property file, have you got access to it? How does it look like? Perhaps, there's a way of extracting the cache folder path from the property folder through sed or awk. Can you post the output of the property file (or the relevant part of it)? |
Thanks
Now I understand -mtime, -mmin, and xargs is better. ---------- Question 2: The properties file (called config.properties) has the format: servername.cachetype1.folder=/tmp/cachetype1 servername.cachetype2.folder=/tmp/cachetype2 servername.cachetype3.folder=/tmp/cachetype3 Java program loads the properties and set name/value pairs to System Properties. In Java, I can access the value by Code:
System.getProperty("servername.cachetype1.folder"); |
I don't know if that's exactly what you want:
Code:
#!/bin/bash Then it will enter each of those directories: /tmp/cachetype1 /tmp/cachetype2 /tmp/cachetype3 and remove selected files in them. EDIT: Test it on some temporary directories/files first!!! What's the whole output of the properties file? |
# ------------- properties -----------
metric.jdbc.url=jdbc:oracle:thin:@10.12.23.190:1521:METRIC metric.jdbc.driver=oracle.jdbc.driver.OracleDriver metric.jdbc.user=pw metric.jdbc.pwd=pw vista.jdbc.url=jdbc:oracle:thin:@10.14.49.103:1521:VISTA vista.jdbc.driver=oracle.jdbc.driver.OracleDriver vista.jdbc.user=pw vista.jdbc.pwd=pw vista.returned.logfile.folder=/tmp/vista nss.jdbc.url=jdbc:oracle:thin:@10.23.56.189:1521:NSS nss.jdbc.driver=oracle.jdbc.driver.OracleDriver nss.jdbc.user=pw nss.jdbc.pwd=pw nss.returned.logfile.folder=/tmp/nss # ---------------- End of properties ------------ |
Is it the whole output? Where are the lines with the tmp/cachetype? Also, can you add the quote tags to the output? It will make it more readable.
EDIT: oh, are the lines in bold the ones in question? |
In that case, you'll need to change the awk command to:
Code:
awk -F= '/returned.logfile.folder/ { print $2 }' |
Thanks a lot.
You anwsered all my questions! I'd appreciate it very much. |
All times are GMT -5. The time now is 04:51 AM. |