nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

How to Delete a file on 46 th day from the day of creation

This is a discussion on How to Delete a file on 46 th day from the day of creation within the Shell scripting forums, part of the Development/Scripting category; hi I am expecting a script that delete the files from a directory. The file should be deleted on 46th ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 24-12-2008, 10:02 AM
Junior Member
User
 
Join Date: Dec 2008
OS: Redhat
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
knvpavan is on a distinguished road
Question How to Delete a file on 46 th day from the day of creation

hi
I am expecting a script that delete the files from a directory. The file should be deleted on 46th day from the date of creation. It's better if you use "crontab -e"

Input: file1,file2,file3 created on Dec23 say. These files should be deleted on feb10 approxmately. Note that my script should run everyday at Midnight12:05 and check the files in a directory specified.

Any help is really appreciated. Its really urgent.....Plz
Thanks,
Pavan.
Reply With Quote
  #2 (permalink)  
Old 28-12-2008, 12:53 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 244 Times in 183 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

You can't as Linux / UNIX do not store date of creation for any file.
__________________
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 02-01-2009, 09:47 AM
Junior Member
User
 
Join Date: Dec 2008
OS: Redhat
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
knvpavan is on a distinguished road
Default

Thanks for the reply.
Reply With Quote
  #4 (permalink)  
Old 04-01-2009, 06:00 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 244 Times in 183 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

However, you can find a file last access tim,e using find command. Like display list of files 46 days old using find command. From the man page:
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.
Code:
find /path/to/dir -atime +45 "*.txt" -print
__________________
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
  #5 (permalink)  
Old 08-01-2009, 04:22 PM
Junior Member
User
 
Join Date: Dec 2008
OS: Redhat
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
knvpavan is on a distinguished road
Default

Hi vivek,
This is working fine if the accesstime is greater than 45 days. I have done this before using mtime.Thanks for the reply.

pavan.
Reply With Quote
  #6 (permalink)  
Old 12-02-2009, 12:05 AM
Member
User
 
Join Date: Feb 2009
OS: Debian / Red Hat
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
cjredding is on a distinguished road
Default

Usually what I would do is create a script like so:

#!/bin/sh
#
# This will remove all files older than 46 days
find /path/to/files/ -mtime +46 -exec rm {} \;
#
Done
Reply With Quote
  #7 (permalink)  
Old 12-02-2009, 12:07 AM
Member
User
 
Join Date: Feb 2009
OS: Debian / Red Hat
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
cjredding is on a distinguished road
Default

Then in the cron tab it should look like this

05 0 * * * root /path/to/script
Reply With Quote
Reply

Tags
find , finding file , linux , unix


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


Similar Threads

Thread Thread Starter Forum Replies Last Post
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31-05-2008 06:44 PM
Shell Script to Automatically Delete a File via Cron Job kakarla Shell scripting 2 29-01-2008 08:54 AM
how to delete 0 bytes file puppen Linux software 2 15-05-2007 09:45 PM
Shell script to delete a file with a dialog utility shankar100 Shell scripting 4 02-03-2007 10:22 AM
mysql account creation Linux software 1 28-01-2006 11:17 AM


All times are GMT +5.5. The time now is 05:30 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