73

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.

3
  • If not Bash, then what shell are you using?
    – Ryan
    Commented Apr 25, 2016 at 14:25
  • Oh it is bash... I meant bash script*
    – visc
    Commented Apr 25, 2016 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
    Commented Apr 25, 2016 at 14:27

3 Answers 3

127

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
9
  • 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
    Commented Mar 9, 2017 at 10:29
  • 1
    @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
    Commented Mar 10, 2017 at 15:51
  • 1
    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. Commented Mar 10, 2017 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
    Commented Mar 10, 2017 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
    Commented Mar 11, 2017 at 16:55
5

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.

1
  • 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
    Commented Jan 11, 2021 at 11:09
2
for file in *.mp4;
do
    dt=$(date -r "$file" "+%Y%m%d-%H%M%S")
    mv "$file" "$dt - $file"
done

This Bash script is taking all mp4 files in the current folder and adding modification date and time of the file before the original name.

Adapt to your needs.

2
  • Be sure to just execute that once, otherwise you will multiple times attach dates to the files resulting in something like ` 20221130-110753 - 20221130-110753 - 20221130-110753 - 20221130-110753 - 20221130-110753 - 2.mp4`
    – Simeon
    Commented Nov 30, 2022 at 10:36
  • You might want to move the renamed file. e.g. like this #!/bin/bash #creating target directory mkdir dated for file in *.mp4; do dt=$(date -r "$file" "+%Y%m%d-%H%M%S") mv "$file" "dated/ $dt - $file" done
    – Simeon
    Commented Nov 30, 2022 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .