How To Append Current Date To Filename in Bash Shell

h‮:sptt‬//www.lautturi.com
How To Append Current Date To Filename in Bash Shell

To append the current date to a filename in a Bash shell script, you can use the date command and the mv command.

Here is an example of a script that appends the current date to the file input.txt:

#!/bin/bash

# Get the current date in the format YYYY-MM-DD
date=$(date +%Y-%m-%d)

# Append the date to the file name
mv input.txt input-$date.txt

To run this script, you can save it to a file (e.g., append_date.sh) and make it executable using the chmod command:

chmod +x append_date.sh

Then, you can run the script using the ./ command:

./append_date.sh

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

Overall, the date and mv commands are useful tools for appending the current date to a filename in a Bash shell script. They allow you to easily customize the filename based on the current date and perform other tasks based on the modified filename.

Created Time:2017-10-28 21:38:49  Author:lautturi