[Validate] Script to remove files olderthan X days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Validate] Script to remove files olderthan X days
# 1  
Old 09-17-2012
[Validate] Script to remove files olderthan X days

Hi,

I have written a small shell script to remove the files olderthan X days (say 30). But I not sure how it acts on the filesystem, as I am using rm.

Code:
if [ $# -ne 2 ]
then
  echo "##############################################"
  echo "Invalid no .of arguments\n"
  echo "Usage:\n$0 PATH No_of_days_older"
  echo "###############################################"
  exit 1
fi;
path=$1
days=$2
find $path -type f -mtime +$days | xargs rm
exit

Can someone please let me know is it valid and how it behaves when it runs from crontab.
The main intension is to remove the files in a purticular directory which are older than X days. ( X is passed as an argument.)

Thanks.
# 2  
Old 09-17-2012
Code:
find $path -type f -mtime +$days -exec rm -f {} \;

in the crontab, ex. every day at night 03:10
Code:
10 3 * * * /some/path/myscript  somepath 30 >> /some/path/mylog.log 2>&1

# 3  
Old 09-17-2012
Replace "rm" by "echo" and put all the files found that way into a logfile. This way you can test as much as you want. Once you are satisfied with the results replace "echo" by "rm" again.

Don't forget to set an environment if you plan to use the script with cron. You might fall for "Cron Problem Number One" otherwise.
I hope this helps.

bakunin
# 4  
Old 09-17-2012
Quote:
Originally Posted by kshji
Code:
find $path -type f -mtime +$days -exec rm -f {} \;

in the crontab, ex. every day at night 03:10
Code:
10 3 * * * /some/path/myscript  somepath 30 >> /some/path/mylog.log 2>&1

Actually my question is not about scheduling in cron. But how it behave on the filesystem. I am very much afraid of rm command. Smilie

I think the script which I posted will remove the files recursively from the path and anyother findings like this ?
# 5  
Old 09-17-2012
Why not try the command with echo instead of rm to see if it would delete more than intended?
# 6  
Old 09-17-2012
Quote:
Originally Posted by Corona688
Why not try the command with echo instead of rm to see if it would delete more than intended?
Yes I tried.
Code:
find $path -type f -mtime +$days

It displayed the files, also recursively.
# 7  
Old 09-17-2012
What's your system? On Linux, you could use -maxdepth 1 with find to prevent it leaving the current folder.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shellscript command to remove files starting with a certain string, and older than 3 days

Hi All, Need help in identifying a shellscript command to remove all files on a server directory, starting with a certain prefix and also older than 3 days. That means files created with that prefix, today or yesterday, shouldn't be removed. Thanks, Dev (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. UNIX for Dummies Questions & Answers

Remove files from tar archive which are more than 1000 days old.

I am not able to extract/remove files older than 1000 days from a tar archive in linux system. #!/usr/bin/perl @file_list = `find /home/x/tmp/ -name *xxMsg* -ctime +7`; $file_name = '/home/x/tmp/new_archive.tar'; for... (1 Reply)
Discussion started by: DannyV
1 Replies

3. Shell Programming and Scripting

Remove files older than 2 days.

Hi All, I am new to the scripting and using solaris 10 OS. Please suggest me from the below script which modifications need to be done to delete the files more that 2days older. Current script is deleting existing file. # Remove old explorer runs if needed DIR=`dirname ${EXP_TARGET}` if ... (2 Replies)
Discussion started by: Navkreddy
2 Replies

4. Shell Programming and Scripting

script to remove files older than 60 days

Hi I need help in the script which looks at a contorl file which has a list of file names like xxxx.12345 and I want to take only xxxxx and search in a specific directory and remove the file if its older than 60 days I have written something like this.. but seems to be wrong... (1 Reply)
Discussion started by: antointoronto
1 Replies

5. Shell Programming and Scripting

How can i remove the files generated on 10 days before!!!

Dear Friends! i am wirking on the IBM AIX version 5.3. and i wrote a script to delete the files whicha re generated on 10 days before to the present day. but iam not able to delete the files with the below script so please check and correct me. dt=`TZ=aaa480 date +%d`... (2 Replies)
Discussion started by: innamuri.ravi
2 Replies

6. Shell Programming and Scripting

Shell script for purging the 3 days old files

Hi all, I try to write shell script to the below requirement. I have Hard coded the oratab location and take the list of databases from oratab and find out archive log locations for each database, and list more than 3 days old files for each location and purge those. ... (2 Replies)
Discussion started by: mak_boop
2 Replies

7. Shell Programming and Scripting

How to tar, compress and remove files older than two days

Hi, I'm Eddy from Belgium and I've the following problem. I try to write a ksh script in AIX to tar, compress and remove the original *.wav files from the directory belgacom_sf_messages older than two days with the following commands. The problem is that I do not find a good combination... (4 Replies)
Discussion started by: edr
4 Replies

8. Shell Programming and Scripting

Remove files which created date before 10 days on HP-UX

Hi All, Could you please let me know if there is any one can help to create a shell script to remove some files which is the created date for them greate than 10 days (sysdate-10) Please try to email me on email removed Thanks in advance, Murad (1 Reply)
Discussion started by: murad_fayez
1 Replies

9. Shell Programming and Scripting

Need to remove files older than 30 days except directories

Hi, I need to remove files (*.trc) which are older than 30 days from one location. My problem is there I do not want to visit any of the directories at that location. I want to search files at that particular location only (need to skip directorys at that location). maxdepth option is there... (6 Replies)
Discussion started by: malaymaru
6 Replies

10. UNIX for Dummies Questions & Answers

Find files older than 5 days and remove tem after listing

need help with this ... Find files older than 5 days and remove tem after listing list "test" file older than 5 days and then remove them (1 Reply)
Discussion started by: ypatel6871
1 Replies
Login or Register to Ask a Question