Create a dummy file even if the path is not exist

hi,

i want to create a dummy file even if the path to that file is not exist as follows.
suppose, in my working directory 2 files are there. and
i create one more file which is exist as follows

# pwd
/home/satya
# ls
file1.txt  file2.txt
# echo dummy > /home/satya/file3.txt
# ls
file1.txt  file2.txt  file3.txt

now i want to create a file which path is not exist.

# echo dummy > /home/satya/temp_dir/file4.txt
-bash: /home/satya/temp_dir/file4.txt: No such file or directory

we can create directory and then create dummy file.
but there is a problem when there are more directories to be created.

i need these things with in a script.
input to the script is names of the files with full path in which some directories are not exist like
/dir1/dir2/dir3/dir4/file1.txt
/dir1/dir2/dir5/dir6/file2.txt

mkdir -p /home/satya/temp_dir/ && 
  echo dummy > /home/satya/temp_dir/file4.txt

You could write a function which takes an argument (the path to the file) and uses it to create the directory and then the file:

CreateDummy() {
  mkdir -p ${1%/*} || return 1
  touch $1 || return 2
}
1 Like

thanks scott and radoulov for your useful replies.

---------- Post updated 05-23-12 at 11:52 AM ---------- Previous update was 05-22-12 at 07:49 PM ----------

scott , can you please explain those two lines

i mean "|| return 1" and "|| return 2".

i am thinking that if the directory is not exist then it creates that otherwise it goes to next line.