Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-28-2006, 05:56 AM   #1
Registered: Oct 2005
Posts: 540

Rep: Reputation: 32
formatting date output


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
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
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
> day=${fn:2:2}
> echo ${day}
>echo $(( ${fn:2:2} - 5 ))

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.

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


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 (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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:16 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration