How do you put date and time in a file name?

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