nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Delete Files In Directory By Date

This is a discussion on Delete Files In Directory By Date within the Getting started tutorials forums, part of the Linux Getting Started category; Hey Friends, As as Systems Administrator for many servers, I always come across the need to delete files in a ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Getting started tutorials

Linux answers from nixCraft.


Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-11-2009, 03:35 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Delete Files In Directory By Date

Hey Friends,

As as Systems Administrator for many servers, I always come across the need to delete files in a directory by date.

e.g.

I have a podcast/video server that I have setup a rsync script on to backup data to another server. On the primary server, I need to perform a search for files and delete them using a date.

I use the find command for this. I am sure there are other ways, however this takes care of business for me.

This command will search and delete files that are 180 days old. This can be adjusted to what ever. So if you change the number in red. You can alter the number of days to find and delete.

Code:


[root@podcast3 ~]# find /this/is/the/directory/ -type f -ctime +180 -exec rm -f {} \; -print

This is basic, However I use it daily.

HTH
Hope. This. Helps. You in your daily tasks.
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #2 (permalink)  
Old 08-11-2009, 08:36 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Nice, -delete option is a bit faster (only works with GNU find version)

Code:
 find /this/is/the/directory/ -type f -ctime +180 -delete
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 08-11-2009, 07:00 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 604
Thanks: 61
Thanked 80 Times in 72 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hey Thanks,

I am using this version of find on rhel4.

Code:
[root@radio5 ~]# find --version
GNU find version 4.1.20
[root@radio5 ~]#
I scanned over the entire man page. I am not seeing the delete flag in there. Also when executed, I get this:

Code:
[root@radio5 split]# find /root/jason -type f -name "*.txt" -delete
find: invalid predicate `-delete'
[root@radio5 split]#
Please Advise Me on the usage.

As always, thank you for you support and opinion.

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #4 (permalink)  
Old 08-11-2009, 11:58 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 246 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I think you got older version. You need GNU find version 4.2.xx or above, which is shipped with RHEL5 and other distro. From my personal RHEL 5.4 box find man page:

Quote:
-delete
Delete files; true if removal succeeded. If the removal failed,
an error message is issued. Use of this action automatically
turns on the ’-depth’ option.
HTH

Oh and there is also tmpwatch command which removes files which haven’t been accessed for a period of time. See man page for details.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
The Following User Says Thank You to nixcraft For This Useful Post:
jaysunn (09-11-2009)
Reply

Tags
find , find -delete , find and delete older files , gnu/find , tmpwatch


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell date command for manipulation and formatting date newbie4 Shell scripting 4 28-10-2008 07:33 PM
To delete files in Trash Ramya CentOS / RHEL / Fedora 1 08-09-2008 08:59 AM
Find Unix Linux File / Directory by date And Then Copy / Move File asim.mcp CentOS / RHEL / Fedora 1 10-08-2008 03:30 AM
Shell Delete Directory chimu Slackware 2 31-05-2008 03:36 PM
delete directory linux chiku Linux software 4 22-07-2006 06:56 PM


All times are GMT +5.5. The time now is 07:10 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38