In a shell script, you can simply:
mkdir -p /opt/test && touch /opt/test/test.txt
mkdir -p will not work (and will not do anything) if the directory already exists.
In perl, use make_path from the File::Path module, then create the file make_path you want. make_path also does nothing if the directory already exists, so you don't need to check yourself.
source share