Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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:
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
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.