Tuesday, March 8, 2016

How can I add datetime to an existing file name in Linux? – unix.stackexchange.com #JHedzWorlD


I want to add date and time to the file name, for example: 08032016out.log.zip


This is what I try to do:


_now=$(date +"%m_%d_%Y") _file="$_nowout.log" touch $_file.txt # creating new file with the timedate 

How can I create the new file with the datetime?




You have created a variable named _now, but later you reference a variable named _nowout. To avoid such issues, use curly braces to delimit variable names:


_now=$(date +"%m_%d_%Y") _file="$_nowout.log" touch "$_file.txt" 

Note that I have left "$_file.txt" as is, because . is already a variable names delimiter. When in doubt, "$_file.txt" could be used just as well.




You need to specify the boundaries of the variable name.


_file will be set to $_nowout.log but the shell can not determine if you mean $_now or $_nowout.


Use


_file="$_nowout.log" 

to explicitly use the $_now variable.


So the script becomes:


_now=$(date +"%m_%d_%Y") _file="$_nowout.log" touch "$_file.txt" 

Which, for the record can be shortened to:


touch "$(date +%m_%d_%Y)out.txt" 


Source link




How can I add datetime to an existing file name in Linux? – unix.stackexchange.com #JHedzWorlD

No comments:

Post a Comment