Get today’s date and use it in filename
31,027
Solution 1
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.
Solution 2
Running the command
echo "myfilename-"`date +"%d-%m-%Y"`
gives this as the output:
myfilename-21-02-2014
Solution 3
One of the possible soultions:
date +log%y%m%d | xargs touch
creates log111017
Related videos on Youtube
Author by
Louis B.
Updated on September 18, 2022Comments
-
Louis B. almost 2 years
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 fordate
, but it seems that I can't really format its output?Any help would be appreciated.
-
htorque over 12 yearsDamn, 36 seconds too slow. ;-)
-
Louis B. over 12 yearsI 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')"
-
Adaephon over 10 yearsActually this will print
myfilename-date +%d-%m-%Y
.