Removing files automatically from a directory after 30 days.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Removing files automatically from a directory after 30 days.

Hello;

I have a directory that is collecting log and act files. I need to write a script that will remove these files once they are 30 days old. I have read through a number of threads on this site that have given me a great deal of information. However I have what seems to be a unique situation. I can't use the -atime or the -mtime commands because not all the files that haven't been modified or accessed can be deleted. There are non log or act files in this directory. One thing that may help is that the log and act files use yyyymmdd.log or yyyymmdd.act file naming system. IE 20050623.log or 20050623.act. How can I use this to generate my script? Thank you very much in advance for your knowledge and help.
# 2  
Do this first to make sure it's getting what you want.

Code:
find /path/to/dir -type f -name "*.log" -a -name "*.act" -mtime +30 -exec ls -l {} \;


Then do this if it goes according to your criteria:

Code:
find /path/to/dir -type f -name "*.log" -a -name "*.act" -mtime +30 -exec rm -f {} \;

# 3  
There are a fw problems with that find.

1. "*.act and *.log" will not match anything
2. It will start an rm process per file

Something like the following would be better:

Code:
find /path/to/dir -type f \( -name "*.log" -o -name "*.act" \) -mtime +30 -print0 |
xargs -r0 rm

Note I don't understand the OPs comment about mtime not working.
Note mtime is initialised with the date the file was created.
# 4  
Quote:
Originally Posted by pixelbeat
There are a fw problems with that find.

1. "*.act and *.log" will not match anything
2. It will start an rm process per file

Something like the following would be better:

Code:
find /path/to/dir -type f \( -name "*.log" -o -name "*.act" \) -mtime +30 -print0 |
xargs -r0 rm

Note I don't understand the OPs comment about mtime not working.
Note mtime is initialised with the date the file was created.
mtime is changed if the file is modified after the creation date ... you could have a file that has been there 5 years but modified twice daily so it's mtime will always be "-1" ...

IMUO --- the only difference in most cases between using "|xargs" as compared to "-exec" is the use of 2 commands instead of 1 ... in a directory with a small number of files, the use of either form is dictated more by the user's preference than anything else ...
# 5  
xargs is a more general/scalable solution to many problems.

Having reread the OP, I think he meant "have" rather than "haven't" in the following:

"I can't use the -atime or the -mtime commands because not all the files that haven't been modified or accessed can be deleted."

So how about using -ctime ?
I know c means change not create, but that should
be the same.
# 6  
MySQL

Try this piece of code if you want to be 100% sure.

for filename in list
do
file_dt=`echo $filename | awk '{print substr($1,length($1)-12,8)}'`
today_dt=`date '+%Y%m%d'`
(( diff=$today_dt-$file_dt))

if [ $diff -gt 30 ]; then
do whatever u want
....
fi
done

using find and rm combination will be more fast.
but if you are not too sure, u can use this.
# 7  
Thank You

I have it working. I had to make a couple of minor tweaks but it works great now. I would like to thank everyone who replied. Everyone's responses where very clear and made this little task very easy. Again thank you all very very much.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #830
Difficulty: Medium
JSON-RPC is a remote procedure call (RPC) protocol built on JSON, as a replacement for XML-RPC or SOAP.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script for Deleting files older than 14 days automatically

we need to have periodic clean up implemented in our sap directory \\sapds\PR1\int\scm\snp\outbound\snapshot. which needs unix script for Deleting files older than 14 days automatically in sap system unix os. (1 Reply)
Discussion started by: kvkreddy_b2w
1 Replies

2. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

3. UNIX for Dummies Questions & Answers

Removing certain files after certain days..

Hi guys.. Currently me using AIX 5.3.0.0 I have these files generated by Oracle in /tmp folder.. # date Mon Sep 26 11:53:31 BEIST 2011 # pwd /tmp # df -g . Filesystem GB blocks Free %Used Iused %Iused Mounted on /dev/hd3 10.00 3.65 64% 62479 7% /tmp... (2 Replies)
Discussion started by: mushr00m
2 Replies

4. AIX

Deleting files older than 14 days automatically

Hi In my aix server under the location "/usr/sap/SAPXI/extract", I have a lot of log files. I need a script which is to be added in crontab so that the files and directories older than 14 days should get deleted automatically from the location "/usr/sap/SAPXI/extract". Please advise me.... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

5. UNIX for Advanced & Expert Users

removing files older than n days

Hi, on AIX 6.1, is there any commande line to remove the files older than n days in a directory ? Thanks. (2 Replies)
Discussion started by: big123456
2 Replies

6. Shell Programming and Scripting

help with removing files which are n days old

Hi , I need to remove files from a specific directory which are 7 days old using folowing find comand as: find /home/dir1/log -name "run*.log" -mtime +6 -prune -exec rm -f {} \; but my script should traverse to /home/dir1/log and them delete the specified files in the directory. i.e... (2 Replies)
Discussion started by: Navatha
2 Replies

7. Shell Programming and Scripting

Automatically Load data from all files in directory

Im new in Unix shell scripting and i need someone to help me to make Script that run all time watching my directory that files uploaded to it via FTP (/mydir/incoming_files), if any files exists in it then (if many files exists, then sort files and load them ascending) itll checks the size of the... (1 Reply)
Discussion started by: m_fighter
1 Replies

8. UNIX for Dummies Questions & Answers

Removing files older than 7 days

Script help, I need to delete files that are older than 7 days. I do that automatically but I know that a cron job can do the job for me. Any help is greatly appreciated, as you can see, I am a DOS or WINDOWS guy. Little on UNIX. Thanks (3 Replies)
Discussion started by: texasoeb
3 Replies

9. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

removing files that are 60 days old

Hello guys I'm writing a script and I need some help. I'm almost done but I think I have a problem with my ffind command. I need the scripts to delete files are 60 days old and then append the filenames to a text file on the days they were deleted. If someone can help me I would be thankful. (5 Replies)
Discussion started by: aojmoj
5 Replies

Featured Tech Videos