Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Using the command-line, I want to create a log file with today's date in the name (for example, today is 05/17/2011, so the filename would have to be log051711).

I know how to create the file (touch filename), but I don't know how to get today's date. I have looked at the manual for date, but it seems that I can't really format its output?

Any help would be appreciated. :)

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

You can format the output using the '+FORMAT' parameter, e.g.

touch "log$(date +'%m%d%y')"

See the manpage for what sequences you can use in FORMAT.

share|improve this answer
    
Damn, 36 seconds too slow. ;-) –  htorque Oct 17 '11 at 12:03
    
Thank you, exactly what I was looking for. :) –  Louis Bataillard Oct 17 '11 at 12:17
    
I just figured out that to use this in a cron job, I had to escape the %-signs, so that it read: touch "log$(date +'\%m\%d\%y')" –  Louis Bataillard Oct 18 '11 at 19:06
add comment

One of the possible soultions:

date +log%y%m%d | xargs touch

creates log111017

share|improve this answer
add comment

I'm sure somebody else has a better way to do this but assuming you want month-day-year this should work:

touch log`date +%m%d%y`  

and you can reorder the %m, %d, %Y to reflect the ordering you want. The man page for date tells you more about additional formats.

share|improve this answer
add comment

echo "myfilename-"date +"%d-%m-%Y"

Ans: myfilename-21-02-2014

share|improve this answer
    
Actually this will print myfilename-date +%d-%m-%Y. –  Adaephon Feb 21 at 6:03
    
True.. MyFileName is just a prefix.. If needed we can keep it, else.. echo date +"%d-%m-%Y" this is enough... it will print only date 21-02-2014 –  Sreedhar GS Feb 21 at 6:10
    
Ah, now I see: I got confused because the ` did not appear in your answer. This is because they are used by askubuntu to indicate code blocks. You should mark code by either surrounding it with backticks or by indenting the paragraph with the code with 4 spaces. For starters you probably should use the menu above the editor for that and check your post in the preview below the textbox before submitting. –  Adaephon Feb 21 at 6:25
    
I agree.. Thanks for suggestion..:) –  Sreedhar GS Feb 21 at 7:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.