Computing.Net > Forums > Unix > Create directory while copying

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Create directory while copying

Reply to Message Icon

Name: pnbalaji
Date: February 6, 2008 at 11:44:52 Pacific
OS: AIX 5.3
CPU/Ram: 8GB
Product: IBM
Comment:

Hi,

I have a copy statement that needs to execute from a shell script. A sample statement is given below.

cp /data/a/le/lecfil.dbd /bkup2/incr/a/le/lecfil.dbd

The problem is the directory /bkup2/incr/a/le doesn't exist though /bkup2/incr exists. I want my cp command to create the directory if necessary and copy the file to destination.

Can some one help?

Thanks,
Balaji.



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: February 6, 2008 at 12:14:40 Pacific
Reply:

Prior to the copy command, you can do a mkdir command to create any missing directories (assuming you have write permissions on the directories):

mkdir -p /bkup2/incr/a/le

And if your destination fullpath name is in a variable such as $filename, you could do:

mkdir -p $(dirname $filename)


0
Reply to Message Icon

Related Posts

See More


need help for sed Aix reboot hangs



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Create directory while copying

File and Directory.. www.computing.net/answers/unix/file-and-directory/4788.html

Restrict Unix User to a Single Directory www.computing.net/answers/unix/restrict-unix-user-to-a-single-directory/3565.html

Trouble with copying files www.computing.net/answers/unix/trouble-with-copying-files/4501.html