Often you may want to use Bash to add a timestamp to a filename.
You can use the following syntax to do so:
mv teams.txt teams$(date -d "today" +"%Y%m%d%H%M%S").txt
This particular example will add a timestamp to the end of the file in the current directory named teams.txt.
For example, the file will be renamed to something like:
- teams20240817164433.txt.
Note that we use the mv command, which is used to move and rename files, along with the date -d “today” command to rename the existing file with the current date and time at the end of the file.
The following example shows how to use this syntax in practice.
Example: How to Add Timestamp to a Filename in Bash
We can use the ls command to first view all items in our current directory:

We can see that the current directory contains the following filenames:
- managers.txt
- owners.txt
- teams.txt
Suppose that we would like to add a timestamp that represents the current date and time to the end of the file name teams.txt.
We can use the following syntax to do so:
mv teams.txt teams$(date -d "today" +"%Y%m%d%H%M%S").txt
The following screenshot shows how to use this syntax in practice:

Notice that when we use the ls command again to view the updated files in the directory we can see that the file named teams.txt has been renamed to the following:
- teams20240817164433.txt
The current date and time (August 17th, 2024 4:44:33 PM) have been added to the end of the filename.
Feel free to only include +”%Y%m%d” in the formatting of the date command if you only want to add the date to the filename without the time.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Add Prefix to All Files in Directory
Bash: Delete All Files Whose Filename Contains String
Bash: How to Check if File Has Specific File Extension
