Nathan Pk
Nathan Pk

Reputation: 749

Having Issue with file name appended with date -shell scripting

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)

Upvotes: 3

Views: 502

Answers (2)

sampson-chen
sampson-chen

Reputation: 47267

The problem is with shell's interpertation of / in your date +"%m-%d-%Y/%T".

Change it to a - instead (or something else, as long as it's not / or another meta character that will make the files difficult to work with in the future)

Upvotes: 0

William Pursell
William Pursell

Reputation: 212238

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.

Upvotes: 2

Related Questions