How to create a file in a shell script

I need to write a shell script where I read a variable from the environment. If the file specified by him does not exist, I want to create it.

This file path may contain some intermediate non-existent directories, so you must also create them. Thus, neither mkdir -p works here, nor a simple click works here.

What is the workaround?

Thanks!

+4
source share
2 answers
mkdir -p "`dirname $foo`" touch "$foo" 

dirname works on arbitrary paths; it does not check if the path is used (indicates the file specified in it).

+8
source

Why not combine both? mkdir -p /directories/.... && touch /directories/file

0
source

All Articles