Create file with content if doesn't exist Create file with content if doesn't exist unix unix

Create file with content if doesn't exist


Introduce a test for file's existence:

[ -f test.json ] || echo "{}" > test.json

[ -f test.json ] will check if the file exists and is a regular file. If the file does not exists (||), echo "{}" > test.json will be run.


echo '{}' >> test.json

Double arrow will append instead of overwriting the file.

Or if you mean to check file non existence before creation

if [ ! -f test.json ]; then  echo "{}" > test.jsonfi