Create directory while copying

February 6, 2008 at 11:44:52
Specs: AIX 5.3, 8GB

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?


See More: Create directory while copying

Report •

February 6, 2008 at 12:14:40
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)

Report •
Related Solutions

Ask Question