Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Issue with file name appended with date -shell scripting

Tags:

bash

shell

I tried to append the current day and time to the existing file name in shell scripting and I found my command is not working as expected.

For example, if my file name is f1.log and I nees to append it along with current time. This appended version must be used for further processing of the file.

I tried with the following script but getting an error

    now=$(date +"%m-%d-%Y/%T")
    echo hi >>time.log
    mv "time.log" "time.$now.log" (error here : file or directory not found)
    echo hello >> time.log$now   (have to continue processing with new file)
like image 353
Nathan Pk Avatar asked Nov 19 '25 22:11

Nathan Pk


1 Answers

You cannot have a / character in a filename. The mv command is looking for a directory named with the minute, day, and year of the output of date and trying to create a file named by the time. Just change your format to not include / in the filename.

like image 57
William Pursell Avatar answered Nov 22 '25 15:11

William Pursell