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

Re: Copying a whole subdirectory possible?



On Fri, Jan 11, 2002 at 05:52:55PM -0800, Tim locke decreed:
> I need to copy a whole subdirectory to another
> subdirectory...possible? (i.e. cp /home/user1/file
> /home/user2/file) as a regular user.
---end quoted text---

For the most accurate reproduction of a directory tree consider using
the tar command:

    tar cf - . | (cd "$1"; tar xvf -)

The command above assumes you want to copy from the working directory to
another that replaces the $1 symbol.

The advantage of tar is that it preserves permissions, links, etc.
perfectly.  cp does not make as perfect a copy.

The attached script is a wrapper for the tar command above that will
prompt to create the target directory, etc.  Caveat - use at your own
risk.  But there's not much to it, and I use it all the time.

Cheers,
Steve

-- 

                  \_O<  \_O<  \_O<
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Steve Cooper          Redmond, WA
#!/bin/sh

function yesno()
{
	typeset yn
	echo -n "$1 (y/N)? "
	read yn
	typeset -i b
	if [ "$yn" = "y" -o "$yn" = "Y" -o "$yn" = "yes" ]
	then
		return 0
	else
		return 1
	fi
}

typeset -i nf

if [ $# -ne 1 ]
then
	echo "Usage: tarclone <destination directory>"
	exit 1
fi

if [ ! -d "$1" ]
then
	echo "Destination \"$1\" does not exist"
	if ( yesno "Create it" )
	then
		mkdir "$1"
	else
		exit 2
	fi
fi

nf=$('ls' "$1" | wc -l)
if [ $nf -ne 0 ]
then
	echo "Destination \"$1\" is not empty"
	if ( yesno "Copy anyway" )
	then
		echo "Overwriting \"$1\""
	else
		exit 3
	fi
fi

echo -n "Ready to clone $(pwd) to $1 (y/N)? "
read yn
if [ "$yn" = "y" -o "$yn" = "Y" -o "$yn" = "yes" ]
then
	tar cf - . | (cd "$1"; tar xvf -)
fi

Reply to: