tags:

views:

2321

answers:

3

Here is what I tried but failed:

[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
[root@file nutch-0.9]#

besides,any way to make ">" work that way,say to create a directory when need?

[root@file nutch-0.9]# echo test > /nosuchtest/hi.txt
-bash: /nosuchtest/hi.txt: No such file or directory
+5  A: 

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

You can do it with mkdir ..

mkdir -p /var/path/to/your/dir
Christian
lhunath
+2  A: 

There is no such option. What you can do is to run mkdir -p before copying the file

I made a very cool script you can use to copy files in locations that doesn't exist

#!/bin/bash
if [ ! -d "$2" ]; then
    mkdir -p "$2"
fi
cp -R "$1" "$2"

Now just save it, give it permissions and run it using

./cp-improved SOURCE DEST

I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you

victor hugo
Quote your parameter expansions, please. Or you'll suffer bugs introduced by wordsplitting and pathname expansion. Put "" around all your $foo's.
lhunath
+1  A: 

As far as I know, you can only created directories if they exists at the source, so: cp -R /usr/local /usr/local-backup

will create directories further down the hierarchy.

Peter Sankauskas