32

I'm trying to execute a command and would like to put the date and time in the output file name.

Here is a sample command I'd like to run.

md5sum /etc/mtab > 2016_4_25_10_30_AM.log

The date time format can be anything sensible with underscores. Even UTC if the AM and PM can't be used.

  • If not Bash, then what shell are you using? – Ryan Apr 25 '16 at 14:25
  • Oh it is bash... I meant bash script* – visc Apr 25 '16 at 14:26
  • You're not piping, you're redirecting. And the question would be the same if you weren't redirecting. Updating title. – Mikel Apr 25 '16 at 14:27
70

If you want to use the current datetime as a filename, you can use date and command substitution.

 $ md5sum /etc/mtab > "$(date +"%Y_%m_%d_%I_%M_%p").log"

This results in the file 2016_04_25_10_30_AM.log (although, with the current datetime) being created with the md5 hash of /etc/mtab as its contents.

Please note that filenames containing 12-hour format timestamps will probably not sort by name the way you want them to sort. You can avoid this issue by using 24-hour format timestamps instead.

If you don't have a requirement to use that specific date format, you might consider using an ISO 8601 compliant datetime format. Some examples of how to generate valid ISO 8601 datetime representations include:

 $ date +"%FT%T"
 2016-04-25T10:30:00

 $ date +"%FT%H%M%S"
 2016-04-25T103000

 $ date +"%FT%H%M"
 2016-04-25T1030

 $ date +"%Y%m%dT%H%M"
 20160425T1030

If you want "safer" filenames (e.g., for compatibility with Windows), you can omit the colons from the time portion.

Please keep in mind that the above examples all assume local system time. If you need a time representation that is consistent across time zones, you should specify a time zone offset or UTC. You can get an ISO 8601 compliant time zone offset by using "%z" in the format portion of your date call like this:

 $ date +"%FT%H%M%z"
 2016-04-25T1030-0400

You can get UTC time in your date call by specifying the -u flag and adding "Z" to the end of the datetime string to indicate that the time is UTC like this:

 $ date -u +"%FT%H%MZ"
 2016-04-25T1430Z
  • 3
    Underscore is a weird character. It is preferred to use ISO-8601 like $(date +%FT%T) which will give 2016-04-25T10:30:00. – hschou Mar 9 '17 at 10:29
  • @hschou Thanks for pointing out the lack of ISO-8601. I was in a bit of a rush when I originally wrote this answer, so I answered specifically to what the question asked ("The date time format can be anything sensible with underscores."). Also, colons are illegal characters for filenames on Windows, if OS interoperability was a concern. In that case, you would omit the colons, which ISO-8601 allows. I'll try to edit my answer to cover these cases. – Ryan Mar 10 '17 at 15:51
  • Nice answer. I'd also point out that using 12 hour format is a bad idea in that file names wouldn't sort the same in chronological and lexical orders. (10pm would sort before 11am is ls output for instance), not to mention the cultural confusions for 12am vs 12pm. – Stéphane Chazelas Mar 10 '17 at 17:44
  • @StéphaneChazelas I'll edit in a note about the sorting issues of the 12 hour time format. I'm not aware of any cultural confusions regarding 12am vs. 12pm, just general confusion. Care to enlighten me with an example? Regarding your edit with quotes, it actually works fine for me in GNU Bash 4.2.25(1)-release with or without those quotes. – Ryan Mar 10 '17 at 18:18
  • @Ryan You are right about Windows and colon. I think the most common format on Windows is YYYYmmdd_HHMMSS (yes, they use one underscore). If in doubt then consult xkcd.com/1179 – hschou Mar 11 '17 at 16:55
-2

Just adding illustration of the (complicated) syntax needed to insert a chosen date string into the middle of a path/filename. As this is usable with any command, I show for copying a file to a new location with timestamped addition to the filename:

cp filename.txt /path/to/copyfile/$(date +"%FT%H%M")-newfilename.txt

Note that quotes around the expression for the newfilename are not required, despite the presence of a space in date +"%F . Indeed, inclusion of such quotes prevents the command from working if included as a crontab line, due to the way shell parsing works.

  • Welcome to the site, and thank you for your contribution. Please note that your answer seems to reiterate what was already stated in the accepted answer. You may want to consider expanding it so that the difference to that answer becomes more visible; otherwise it woul be best paced as a comment to that answer (once you have sufficient reputation). – AdminBee Jan 11 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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