Assuming that LOGFILE contains the file name you want to rename
if [ -e $LOGFILE ]; then
TIMESTAMP=$(date -r $LOGFILE "+%Y-%m-%d_%H_%M_%S")
mv $LOGFILE $LOGFILE.$TIMESTAMP
fi
And it is sufficient.
You can get the modification time of the file specified by filename with date -r filename.
" +% Y-% m-% d_% H_% M_% S " is in the format of returning with date -r.
You can assign the execution result of command to a variable by using$ (command).
Perform the actual renaming with mv.
https://stackoverflow.com/questions/16391208/print-a-files-last-modified-date-in-bash/16391221
Recommended Posts