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

Share:
31,027

Related videos on Youtube

Louis B.
Author by

Louis B.

Updated on September 18, 2022

Comments

  • Louis B.
    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 for date, but it seems that I can't really format its output?

    Any help would be appreciated.

  • htorque
    htorque over 12 years
    Damn, 36 seconds too slow. ;-)
  • Louis B.
    Louis B. over 12 years
    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')"
  • Adaephon
    Adaephon over 10 years
    Actually this will print myfilename-date +%d-%m-%Y.