LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2005, 10:10 AM   #1
Garp
Member
 
Registered: Jul 2003
Location: Oahu, Hawaii, USA
Distribution: Ubuntu, Debian, RHES
Posts: 57

Rep: Reputation: 15
Date in a filename.


I'm trying to write a script to automate archiving some data. What I'd like it to do is put the date into the filename, for example "backup-08-12-2005.tar.bz2" if it was backed up today.
Is there any automated way of doing this? I'm drawing up blanks so far after googling for a solution.
 
Old 12-08-2005, 10:50 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
The "date" command not only allows you to set and display system time it allows you to manipulate how it is displayed.

Generally: date +%<field>.

The + tells it how to format and each % is a specific piece of information. In the format you can insert any other text you want - it just gets typed in literally without a % sign. (In this case the dashes "-" are literal text.

In your example you want:
%m = Numeric Month
%d = Day of Month
%Y = Full year with century (4 digits as opposed to 2).

You can then use the back ticks (NOT single quotes) to use this within your line. The back ticks tell it to execute whatever is between them first then use its output in the rest of the line.

So just to make it print:
backup-12-08-2005.tar.bz

Type:
echo "backup-`date +%m-%d-%Y`.tar.bz2"

The above does what you asked but I'd like to make a couple of suggestions:

1) Do NOT put the "-" in your date format as it has special meaning in Unix/Linux so might cause you problems down the road. If you need to separate use underscore "_" instead.

2) Put the year before month and day - later listings (especially with the sort command will give you the correct numeric order.

i.e. echo "backup_date_`date +%Y_%m_%d`.tar.bz2" to make it print:
backup_2005_12_08.tar.bz
 
Old 12-08-2005, 04:01 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
I always prefer using something like this:

date=$(date +%F)

Then you can plug this into your filename in your script:

backup_$date.tar.bz2
 
Old 12-09-2005, 03:12 AM   #4
Garp
Member
 
Registered: Jul 2003
Location: Oahu, Hawaii, USA
Distribution: Ubuntu, Debian, RHES
Posts: 57

Original Poster
Rep: Reputation: 15
Hmmm... nice suggestion jlightener and it matches something I read elsewhere but couldn't get to work. Same thing happens when I follow your suggestion. This is on a RedHat Enterprise Server 4 box, for what its worth:

Quote:
#echo "backup_'date +%Y_%m_%d'.tar.bz2"
backup_'date +%Y_%m_%d'.tar.bz2
I can run the date command manually:

Quote:
# date +%Y_%m_%d
2005_12_09
Trickykid, thanks for your solution that one seems to do the trick!
 
Old 12-09-2005, 05:09 AM   #5
PDock
Member
 
Registered: Aug 2004
Distribution: Slack10 & curr. tried numerous
Posts: 189

Rep: Reputation: 37
Quote:
Originally Posted by Garp
Hmmm... nice suggestion jlightener and it matches something I read elsewhere but couldn't get to work. Same thing happens when I follow your suggestion. This is on a RedHat Enterprise Server 4 box, for what its worth:
For what its worth the following is in Americian-ize:
Quote:
Originally Posted by jlightner
You can then use the back ticks (NOT single quotes) to use this within your line. The back ticks tell it to execute whatever is between them first then use its output in the rest of the line.

So just to make it print:
backup-12-08-2005.tar.bz

Type:
echo "backup-`date +%m-%d-%Y`.tar.bz2"
 
Old 12-09-2005, 10:57 AM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Garp your post makes it clear you used single quotes INSTEAD of back ticks as I'd emphasized you should NOT do.

The command works just fine (assuming you're using bash or ksh). Version of Linux doesn't matter because its a shell syntax not a kernel system call. I've never run across a UNIX, BSD or Linux in which this syntax would fail.

As noted the back ticks (on my keyboard this is the symbol on the same key as the tilde (~).
Single quote = '
Back tick = `

The back ticks as mentioned previously tell it to execute the command that falls between them then use only its output within the rest of the command.

Pdock I didn't understand your post at all.
 
Old 12-09-2005, 01:34 PM   #7
Garp
Member
 
Registered: Jul 2003
Location: Oahu, Hawaii, USA
Distribution: Ubuntu, Debian, RHES
Posts: 57

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlightner
Garp your post makes it clear you used single quotes INSTEAD of back ticks as I'd emphasized you should NOT do.
Doh! *slaps forhead and hides away in the corner*

Quote:
The command works just fine (assuming you're using bash or ksh). Version of Linux doesn't matter because its a shell syntax not a kernel system call. I've never run across a UNIX, BSD or Linux in which this syntax would fail.

As noted the back ticks (on my keyboard this is the symbol on the same key as the tilde (~).
Single quote = '
Back tick = `

The back ticks as mentioned previously tell it to execute the command that falls between them then use only its output within the rest of the command.

Pdock I didn't understand your post at all.
kk.. I'll have a stab at that on Monday. Its working using the other method, but I hate to have something that should work not
 
Old 12-09-2005, 01:57 PM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
No problem.

I just find back ticks very useful like pipes for using output of one command for another especially in defining variables or in for loops. Example:

Code:
for FILE in `ls`
do echo "Renaming file $FILE as ${FILE}.old"
   mv $FILE ${FILE}.old
done
Would rename every file found by ls to the same name with a .old suffix.

The above is a fairly simple example. You can have a whole pipe line encapsulated. Example:

Code:
TTL=0
for VAL in `ls -l |awk '{print $5}'`
do TTL=`expr $TTL + $VAL`
   echo $TTL
done
The above would pipe the ls -l output into awk which would print the 5th field (file size in bytes). It would then add that value ($VAL) to the total ($TTL) and print the new total after each value is added. By doing this you could get the size of all your files (of course du would work as well but this is just an example).

Notice in the above I use the back ticks in the expr command as well. Also notice that the awk command has single quotes for the print syntax so you have both single quotes and back ticks in this example.

FYI: TTL has to be set to 0 before the loop so it will know its inital value the first time it adds a value.

Last edited by MensaWater; 12-09-2005 at 01:58 PM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
logrotate with date in filename ptp Linux - Newbie 1 04-14-2011 10:55 AM
How do I add the current date to a filename? sammysrefuge Linux - Newbie 2 03-18-2005 02:43 PM
put date as part of filename box_l Programming 3 07-27-2004 06:17 AM
use date as output filename? wijnands Linux - Newbie 1 05-12-2004 03:39 PM
how to add date to filename x2000koh Programming 1 07-08-2003 09:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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