LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2011, 01:59 PM   #1
Madison00
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Rep: Reputation: 0
delete files when it's older 30 days


I am trying to put together ksh shell and I am new with writing scripts.

How do you write a command to delete any files if it's 30 days old and also it's not currently being locked?

Thanks so much.
 
Old 01-06-2011, 02:02 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you use the find command with the "-mtime +30" you'll get files modified more than 30 days ago. as for being locked... what does locked mean? Currently open? I'd take each file from the find output and use lsof (or fuser) to see if anything has it open within a loop.
 
Old 01-06-2011, 03:47 PM   #3
Madison00
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
below is my script:

#!/bin/ksh
#
ORA_HOME=$ORACLE_HOME
ORA_LOGS=$ORA_HOME/Apache/Apache/logs

#delete log files older than 1 days
find $ORA_LOGS -type f -mtime +180 rm {} \;



below is the error when I run the script:

$ ./cleanup_log.ksh
find: paths must precede expression
Usage: find [path...] [expression]


thanks
 
Old 01-06-2011, 03:51 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by Madison00 View Post
below is my script:

#!/bin/ksh
#
ORA_HOME=$ORACLE_HOME
ORA_LOGS=$ORA_HOME/Apache/Apache/logs

#delete log files older than 1 days
find $ORA_LOGS -type f -mtime +180 rm {} \;



below is the error when I run the script:

$ ./cleanup_log.ksh
find: paths must precede expression
Usage: find [path...] [expression]


thanks
Try it this way:
find $ORA_LOGS -type f -mtime +180 -exec echo rm {} \;

If the output are the files you want to delete then remove the echo. When using 'find' it is a good practice to echo dangerous commands like 'rm' before you actually run them.

Last edited by crts; 01-06-2011 at 03:53 PM.
 
Old 01-06-2011, 03:54 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You missed the -exec option. You can either do
Code:
find $ORA_LOGS -type f -mtime +180 -exec echo rm {} \;
where the echo is for testing purposes, or
Code:
find $ORA_LOGS -type f -mtime +180 -delete
Regarding the locked files issue, I would consider the suggestion by acid_kewpie.
 
Old 01-06-2011, 04:06 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
I personally prefer using xargs instead of -exec:

find $ORA_LOGS -type f -mtime +180 | xargs rm -f

might want -print0 output if your filenames are non standard.
 
Old 01-06-2011, 04:20 PM   #7
Madison00
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thanks for all of the good tips and advises. Certainly helped since I still learn scripting.

one more thing.

how can I add another check condition to be sure these files are not being opened before remove them? In other word, I don't want to remove the files that are current used by the application.

Thanks all.

Last edited by Madison00; 01-06-2011 at 04:29 PM.
 
Old 01-06-2011, 09:57 PM   #8
Madison00
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
any recommendations?
 
Old 01-07-2011, 02:07 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
we've already answered that, lsof.
 
  


Reply



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
[SOLVED] Delete old files older than 7 days anon091 Linux - Newbie 3 09-18-2009 01:15 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM
delete files in server that is older than 30 days using cronjob latheesan *BSD 2 06-14-2005 12:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:53 AM.

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