Create a dummy file even if the path is not exist


 
Thread Tools Search this Thread
Operating Systems Linux Create a dummy file even if the path is not exist
# 1  
Old 05-22-2012
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

Code:
# 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.

Code:
# 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

Last edited by snreddy_gopu; 05-22-2012 at 10:51 AM..
# 2  
Old 05-22-2012
Code:
mkdir -p /home/satya/temp_dir/ && 
  echo dummy > /home/satya/temp_dir/file4.txt

# 3  
Old 05-22-2012
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:

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

This User Gave Thanks to Scott For This Post:
# 4  
Old 05-23-2012
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.

Last edited by snreddy_gopu; 05-23-2012 at 03:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

A file or directory in the path does not exist

I'm brand new to AIX and I looked up how to print this file and it was working but now I'm not able to do it all of a sudden. the file name is rom1.txt so this is what i wrote in the command line and I know I'm in the right directory. In bold is what I seem to be messing up with. prod @ root... (3 Replies)
Discussion started by: Dark0Prince
3 Replies

2. Shell Programming and Scripting

Find if a directory exist from a list of the path

Hello, i want to script on sh to check from a path if the directory exist and isn't empty. I explain: path is : /aaa/bbb/ccc/ccc_name/ddd/ Where the cccc_name is in a list, so i think it's $1 My command find -name /aaa/bbb/ccc/$1/ddd/ didn't work because my $1 is the same and not... (5 Replies)
Discussion started by: steiner
5 Replies

3. Shell Programming and Scripting

Set variable to path that does not exist on local host

Can anyone suggest a workaround zone_5.org='/qaz/qwe/path/tns.osn' output /home/bingo/XXX_script.sh: line 180: zone_5.org=/qaz/qwe/path/tns.osn: no parent The path does not exist on the local machine, the allocation used to work till the server was upgraded. Red Hat Enterprise Linux... (2 Replies)
Discussion started by: squrcles
2 Replies

4. Shell Programming and Scripting

find + move if destination path does not exist

hi frnds, please help ... what will happen with below command if destination path does not exist on the system.... find /var/adm/cft* -mtime +1 -exec mv {} /global/ \ in unix its remove all my files from the system from soruce file ... how is it possbile (1 Reply)
Discussion started by: dodasajan
1 Replies

5. Shell Programming and Scripting

Create a dummy file in all directories that include a .jpg

Hello. My latest project has me with the need for the following script. Basically, any directory that includes a .jpg file needs to also have a ".picture" file created (if it doesn't exist). Here's an example of what I need. /mnt/user/Pictures/2011/Hawaii - 2011/.picture... (11 Replies)
Discussion started by: Davinator
11 Replies

6. Shell Programming and Scripting

A shell script for create a a file in the respective path

Hello forum members, I have to create a out file in the current path./aaa/bbb/ccc/hhh. i am writing script below. ###script Begins##### #!/bin/ksh echo "Weclome" if then echo "Hello" rm -rf $aaa/bbb/ccc/hhh #clean the exsisting o/p file echo "no... (2 Replies)
Discussion started by: rajkumar_g
2 Replies

7. AIX

AIX cp error: "A file or directory in the path does not exist"

Hello fellow UNIX fans, I'm running AIX 4.3 and getting an error message “cp: /a/file2.db: A file or directory in the path does not exist” when I run the following command: cp /b/file.db /a/file2.db It stops every time about 95% of the way through the copy process at 1,073,741,312 bits. ... (3 Replies)
Discussion started by: Jackson123
3 Replies

8. Linux

How to create zip file without path?

Hello, I have generated a PHP script that creates files needed for EPUB file. I have a temp directory where these files are created into and then needs to be zipped. The directory structure is: mimetype content.opf index.html stylesheet.css toc.ncx META-INF META-INF/container.xml ... (4 Replies)
Discussion started by: spaze
4 Replies

9. Shell Programming and Scripting

If file not exist create a new one

i want a script to check whether the file name exits or not if not it has to create a new one (3 Replies)
Discussion started by: din_annauniv
3 Replies

10. SCO

Create dummy printer

On SCO Openserver we create a so called dummy printer that directs to /dev/null (we use this in our software to purge some stuff). Now I have SCO UnixWare 7.1.4, but I cannot create a dummy printer. I create the printer exactly as in Openserver by the Printer Manager, - Enter spoolname -... (1 Reply)
Discussion started by: p.vvugt
1 Replies
Login or Register to Ask a Question