write a file, append if it exists otherwise create in bash write a file, append if it exists otherwise create in bash bash bash

write a file, append if it exists otherwise create in bash


Use the touch command:

touch $fileLogecho "$Logstring" >> $fileLog


   #! /bin/bash   VAR="something to put in a file"   OUT=$1   if [ ! -f "$OUT" ]; then       mkdir -p "`dirname \"$OUT\"`" 2>/dev/null   fi   echo $VAR >> $OUT   # the important step here is to make sure that the folder for the file exists   # and create it if it does not. It will remain silent if the folder exists.

$ sh out hello/how/are/you/file.outgeee: ~/src/bash/moo$ sh out hello/how/are/you/file.outgeee: ~/src/bash/moo$ sh out another/file/lol.hmzgeee: ~/src/bash/moo$ find . ../out./another./another/file./another/file/lol.hmz./hello./hello/how./hello/how/are./hello/how/are/you./hello/how/are/you/file.outgeee: ~/src/bash/moo$ cat ./hello/how/are/you/file.outsomething to put in a filesomething to put in a filegeee: ~/src/bash/moo$ cat ./another/file/lol.hmz something to put in a file

the escaped " for dirname are needed if the folder of file has spaces in the name.