How To Append Current Date To Filename in Bash Shell

In Bash shell, you can append the current date to a filename using the “date” command and command substitution. Here’s how to do it:

  1. Use the “date” command to generate the current date in a specific format. For example, to generate the current date in the “YYYY-MM-DD” format:
date +%Y-%m-%d

This will output the current date in the “YYYY-MM-DD” format.

  1. Use command substitution to insert the current date into the filename. For example, to append the current date to a file named “example.txt”:
mv example.txt example_$(date +%Y-%m-%d).txt

This will rename the file to “example_YYYY-MM-DD.txt”, where “YYYY-MM-DD” is the current date.

You can customize the format of the date by modifying the format string in the “date” command. For example, to include the time and seconds in the filename:

mv example.txt example_$(date +%Y-%m-%d_%H%M%S).txt

This will rename the file to “example_YYYY-MM-DD_HHMMSS.txt”, where “HH” is the hour, “MM” is the minute, and “SS” is the second.

By using the “date” command and command substitution in Bash shell, you can easily append the current date to a filename for various purposes, such as backups, logging, or versioning.

Leave a Comment