[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Directory overwriting

On Mon, May 22, 2006 at 12:54:19PM +0200, Rodolfo Medina wrote:
> When I copy a file into the present directory, with:
>  $ cp /path/to/file .
> , if the file already exists it is overwritten, i.e. the `old one'
> is removed and the `new one' takes its place.
> Instead, with directories it is not the same:
> when I do:
>  $ cp -vr /path/to/dir .
> , if the directory already exists it is not removed, but the new one
> just adds files to the old one.
> Would it be possible, and how?, to have with directory overwriting
> the same behaviour we have with file overwriting?
> Thanks,
> Rodolfo

It is not strickly possible to over-write a directory in Unix, even as
the super user (unless you circumvent the filesystem). All you can do
is read them create/delete them, and add/remove files in them.

Your first example copies a file to a directory, and the second
example copies file recursively from one directory to another,
which I think is consistent.

If by 'the same behaviour' you mean you want the new content to
replace rather than just add to the original content, then you
could certainly do that. The basic procedure would be to remove
everything from the target location first, then do the copy.

For example, a script to do this might be something like
	if [ "$#" != "2" ] ;then
	    echo "Usage: cpdir <src> <tgt>"
	    exit 1
	DIRNAME=`basename $1`
	if [ -r $2/$DIRNAME ] ;then
	    rm -r $2/$DIRNAME
	cp -rv $1 $2

With this, the args from your original example:
	$ cpdir /path/to/dir .

would result in a directory called 'dir' in '.' containing the
same information as the original - if that is what you wanted..

Then again you might have wanted '.' to become the copy, rather
than contain the copy. Your specification is not unambiguous.

Digby R. S. Tarvin                                          digbyt(at)digbyt.com

Reply to: