Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a file in shell script

I need to write a shell script where I read a variable from environment. If the file pointed by it doesn't exist, I want to create it.

This file path could contain some intermediate non-existent directories, so these also need to be created. So neither mkdir -p works here nor simple touch works here.

What is the workaround?

Thanks!

like image 287
xyz Avatar asked Feb 23 '23 15:02

xyz


1 Answers

mkdir -p "`dirname $foo`"
touch "$foo"

dirname works on arbitrary paths; it doesn't check whether the path is in use (whether the file pointed to exists).

like image 78
Fred Foo Avatar answered Feb 26 '23 21:02

Fred Foo