Alex Rivera | Logout

How to have the cp command create any necessary folders for copying a file to a destination

Asked 2009-06-04T00:11:41.257
640

When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried:

[root@file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
Edit
Report

1 Answer

137

I didn't know you could do that with cp.

You can do it with mkdir ..

mkdir -p /var/path/to/your/dir

EDIT See lhunath's answer for incorporating cp.

answered 2009-06-04T00:15:52.107

Your Answer