0

Iam trying to append date to a file that iam creating but it results in no such file or directory found error. If i remove the date from the below code it works fine.

fileName="other.txt."$(date "+%D %T") echo "hello" >> $fileName

  • You are crafting a file name with a space in it, plus, with some locales, slashes. This is not a very reasonable choice. Prefer a more predictable, and shell-friendly name, like `fileName=$(date +"other.txt.%Y%m%d.%H%M%S")`, for instance. – Renaud Pacalet Aug 29 '18 at 12:15
  • [When to wrap quotes around a shell variable?](https://stackoverflow.com/q/10067266/608639), [When is double-quoting necessary?](https://unix.stackexchange.com/q/68694/56041), [How to handle spaces in filenames using double quotes in a Bash script](https://stackoverflow.com/q/32767951/608639), [Listing files in date order with spaces in filenames](https://stackoverflow.com/q/4583801/608639), etc. – jww Aug 29 '18 at 12:48

1 Answers1

1

Here you go,

filename=blah.txt.`date "+%D%T"`

output:

$ echo $filename
blah.txt.08/29/1808:05:01

I would suggest changing the format to something without slashes like:

filename=blah.txt.`date "+%d%m%y%T"`
slm
  • 15,396
  • 12
  • 109
  • 124
kenlukas
  • 3,616
  • 9
  • 25
  • 36