LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-30-2008, 10:41 AM   #1
villumanati
Member
 
Registered: Jun 2008
Posts: 38

Rep: Reputation: 15
Delete Files in directory


Are there any scripts/programs available that can automatically run and delete files older than say 45 days in a directory? I


FYI - I am running Red Hat Enterprise Linux 5
 
Old 12-30-2008, 10:51 AM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
find is what you need - check out its man page or have a look at this article
 
Old 12-30-2008, 11:00 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,581
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Quote:
find /path_to_files -mtime 45 -exec rm {} \;
This will delete all the files in the path_to_files that were modified 45 days ago. For more information, see find's man pages
 
Old 12-30-2008, 11:05 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by sycamorex View Post
This will delete all the files in the path_to_files that were modified 45 days ago. For more information, see find's man pages
This is exactly correct, although I suggest the first time you run it you change the command to echo rm {} just to get an idea of what is really going to be deleted... and of course, if it's important please have a good back up.
 
Old 12-30-2008, 11:15 AM   #5
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,581
Blog Entries: 1

Rep: Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033Reputation: 1033
Quote:
Originally Posted by rweaver View Post
This is exactly correct, although I suggest the first time you run it you change the command to echo rm {} just to get an idea of what is really going to be deleted... and of course, if it's important please have a good back up.
It goes with saying

Code:
find /path -mtime 45 -exec echo {} \;
or

Code:
find /path -mtime 45 -exec ls {} \;
You can find out which files will be modified. It's very easy to make a mistake here and loose some crucial files so always back up your files.
 
Old 12-30-2008, 11:45 AM   #6
GazL
Senior Member
 
Registered: May 2008
Posts: 3,480

Rep: Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016Reputation: 1016
-mtime 45 will find files that were modified exactly 45 days ago (rounding any partial days out)

-mtime +45 is probably what you want.

The -daystart option may also be of use to you in this case.


Also, find has a built in delete. No need for the -exec rm {} \;

Code:
find /directory -mtime +45 -delete

As others have said, be very, very careful with these. and test with a -print option before you start doing it for real to ensure it going to delete what you think it is.
 
Old 12-30-2008, 12:12 PM   #7
villumanati
Member
 
Registered: Jun 2008
Posts: 38

Original Poster
Rep: Reputation: 15
Is there a way I can put the above command in a script so it can be executed daily? I am not to familiar with scripting in Linux.


I know how to add files as a Cron task, but not how to write a script.
 
Old 12-30-2008, 02:18 PM   #8
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,033

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Quote:
I am not to familiar with scripting in Linux.
Scripting in linux is easy (and fun) !
Start the script with the line
Code:
#!/bin/bash
So it knows to run bash to interpret the script
Then add the commands, one to each line, exactly as you would type them at a terminal, except, as it will be called by cron, and cron's $PATH is sometimes strange, give the absolute (full) path to your command to be on the safe side eg
Code:
/usr/bin/find /directory -mtime +45 -delete
not just
Code:
find /directory -mtime +45 -delete
Now it looks like this:
Code:
#!/bin/bash
# This is a comment
# Code to delete old files
/usr/bin/find /directory -mtime +45 -delete
Make the script executable:
Code:
chmod +x /path/to/scriptname
Have cron call the script (you say you already know how to do this)
Test it.

Last edited by tredegar; 12-30-2008 at 02:21 PM. Reason: Code tags
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 05:56 AM
Script to delete week old files in a directory ilyavishnyakov Linux - General 11 02-13-2007 05:37 PM
How to delete once whole files in a remote directory? backpacker Linux - Networking 1 03-10-2006 06:04 AM
rm can't delete 999999 files in one directory yelo Linux - Software 10 04-10-2004 03:00 PM
How can I delete a directory and its files? OrganicX Linux - General 6 09-01-2002 05:25 AM


All times are GMT -5. The time now is 09:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration