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-28-2006, 05:56 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
formatting date output


Hello,

I have been using the "date +%m%d%Y%T" command to name my database dump files. I also want to be able to have the script automatically delete backup files that are older than x number of days. How can I specify this? I tried using "date +%m%d-5%Y-1%T" but it only shows "-5" as part of the output, instead of deducting 5 off the generated date.
 
Old 01-28-2006, 06:08 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Try testing the creation time of the backup file. See man ctime. An if-then structure to compare ctime of existing backup files to todays date (part of the data structure used by ctime is the Julian date rather than day/month/year, which ought to make the selection easier). If the selection is true, delete, else, ignore for now.
 
Old 01-28-2006, 08:03 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using find <directory> -mtime
would be a better idea then using the -ctime test, in the case where you may have moved the files to another directory or filesystem.
If you are storing them in a filesystem where -mtime doesn't work, there is still a problem with your general idea of extracting the day from the filename, and simply subtracting 5 from it. What if the backup was performed on the 3rd of the month?

However, to answer your general question about extracting information from the filename:

fn=$("date +%m%d%Y%T")
echo $fn
0128200607:17:55
> day=${fn:2:2}
> echo ${day}
28
>echo $(( ${fn:2:2} - 5 ))
23

The $(( )) means that this is an arithmetic expression. This way the substraction is carried out.
Compare with:
> echo ${fn:2:2} - 5
28 - 5

Here, the expression is a string of characters.

You probably need to write a bash function that will take the filename and return age of the file.

The date command is very interesting, and the coreutils manual has some interesting examples, such as:
date --date='5 days ago'

I would recomend that you read the "coreutils" manual and the "Finding Files" manual.
http://www.gnu.org/software/findutil...find.html#Time
http://www.gnu.org/software/coreutil...ate-invocation

Last edited by jschiwal; 01-29-2006 at 02:55 AM.
 
  


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
CLI....ls (or similar command)...formatting output Basslord1124 Linux - Newbie 2 02-27-2005 05:13 PM
Formatting output of ls doodar Linux - Newbie 29 07-29-2004 01:25 PM
use date as output filename? wijnands Linux - Newbie 1 05-12-2004 03:39 PM
Bash Select Function: Formatting Output mooreted Linux - General 2 03-28-2004 06:26 AM
need hlp formatting output from ls test* Evan P. Programming 1 11-06-2003 08:40 PM

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

All times are GMT -5. The time now is 12:18 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