LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   formatting date output (https://www.linuxquestions.org/questions/linux-newbie-8/formatting-date-output-408888/)

grob115 01-28-2006 05:56 AM

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.

bigrigdriver 01-28-2006 06:08 AM

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.

jschiwal 01-28-2006 08:03 AM

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


All times are GMT -5. The time now is 09:12 AM.