h1

using tar to copy files

July 24, 2007

I found the ”tar“ command quite useful in copying a directory (including all its files and sub-directories) to another directory of my choice:

cd /from-stuff/
tar cf – . | (cd /to-stuff; tar xvf -)

/from-stuff/: the directory from which you want to copy the files
/to-stuff/: the directory to which you want to copy your stuff

I have not figured out the tar command yet, so i am currently using this like a black box with input(from-stuff) and an output(to-stuff)

Advertisement

4 comments

  1. Tar is really useful command :)
    Here is short explanation :
    tar cf – . | (cd /target; tar xvf -)
    so: tar everything in current dir (.) and put the tar file to standard output (-), then change dir to /target and un-tar to current dir everything from standard input (-).
    In this case standard output from first tar is piped to standard input of second tar.


  2. cool! someone tool the trouble to explain it… thanx dude


  3. A simpler way of doing this would be to use cp instead of tar. The -R flag enables you to copy recursively (i.e. the directory/files you specify, and everything underneath (in) them in the directory hierarchy. The new command would be:
    cp -R /from-stuff /to-stuff
    Good luck!


  4. Though tar follows and keeps (broken) symlinks, permissions, etc. With cp is hassle…



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.