LinuxQuestions.org
Review your favorite Linux distribution.
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 06-23-2008, 01:46 AM   #1
Winanjaya
Member
 
Registered: Sep 2003
Posts: 209

Rep: Reputation: 32
Delete files based on current date -1


Dear All,

How to delete files (formatted myfiles_yyyymmdd.tar ie. myfiles_20080620.tar) based on current date -1, I tried to use below codes.. but it does not work..


/bin/rm -f /home/guest/myfiles_`date - 1 +%Y%m%d`.tar

Please help

Thanks & Regards
Winanjaya
 
Old 06-23-2008, 02:43 AM   #2
tajamari
Member
 
Registered: Jul 2007
Distribution: Red Hat CentOS Ubuntu FreeBSD OpenSuSe
Posts: 252

Rep: Reputation: 32
Quote:
Originally Posted by Winanjaya View Post
Dear All,

How to delete files (formatted myfiles_yyyymmdd.tar ie. myfiles_20080620.tar) based on current date -1, I tried to use below codes.. but it does not work..


/bin/rm -f /home/guest/myfiles_`date - 1 +%Y%m%d`.tar

Please help

Thanks & Regards
Winanjaya
use find command, then after finding the files to be deleted execute rm, something like this
find /path/to/dir -depth -mindepth 1 -mmin -1 -exec rm -rf '{}' \;
 
Old 06-23-2008, 03:31 AM   #3
Winanjaya
Member
 
Registered: Sep 2003
Posts: 209

Original Poster
Rep: Reputation: 32
Sorry, I meant I am using cronjob to delete yesterday files using below command:

/bin/rm -f /home/guest/myfiles_`date - 1 +%Y%m%d`.tar

but it doesnot work..

could you please advise what I missed?

TIA
 
Old 06-23-2008, 04:38 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
It is not the correct syntax. Use the -d option
Code:
echo /home/guest/myfiles_`date -d yesterday +%Y%m%d`.tar
and if you want to subtract one day from any date the syntax is
Code:
date=20080608
echo /home/guest/myfiles_`date -d "$date 1 day ago" +%Y%m%d`.tar
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete mail based on date gquiring Linux - General 8 05-08-2013 09:24 AM
ow do I delete all files which were created on a specific date marsguy Linux - General 9 08-21-2007 08:15 AM
delete files by date noir911 Programming 4 10-04-2006 07:42 AM
Delete files based on date stefaandk Linux - General 3 06-17-2005 02:20 AM
delete files date wise hardeep_ubhi Linux - General 1 11-16-2004 02:07 AM

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

All times are GMT -5. The time now is 07:51 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration