Move files around local filesystem with tar without wasting space using an intermediate tarball.

( cd SOURCEDIR && tar cf - . ) | (cd DESTDIR && tar xvpf - )

By: res0nat0r
2009-06-26 19:13:51

3 Alternatives + Submit Alt

What Others Think

Why would you do this as opposed to cp -r? Is this faster? I'm too lazy to try it out...
blue_beastie · 678 weeks and 4 days ago
Well, for starters it preserves ownership, mode, timestamps, and links. Then again, so does cp -a, so I really don't know.
TheMightyBuzzard · 678 weeks and 4 days ago
cp's -a switch does --recursive, --preserve=mode,ownership,timestamps,links and --no-dereference I fail to see the point in using tar like this. Anyone? It'd make more sense if the extract command was an arg to ssh, to happen on a remote machine, and tar was set to compress the data.
tremby · 678 weeks and 2 days ago
Depending on where the source and destinations live (same partition, same disk, same machine) this approach can have merit sometimes because it introduces a buffering via the pipe and the reading and writing are driven by two different tar processes rather than a single cp process. If it is faster that way, I think that would be why. However, on the same machine I don't think this is likely to be faster, but I have definitely verified that when copying files between machines in this way (via putting an ssh user@host in front of either cmd) can be way faster than a single scp command, due to the buffering.
bwoodacre · 678 weeks and 1 day ago
Oh, the old classic ;-)
oernii2 · 678 weeks and 1 day ago
That way more reliable when using nfs. I didn't believe it, but it happened to me that on some systems (tru64, aix, solaris) cp-ing tonns of files to nfs3 fails while tar | tar - works smoothly... Anyway it could be that on newer release of nfs those issues won't arise.. but if you want to be sure...tar is for free :P
ioggstream · 678 weeks ago
No need to imagine a pat on the back
yeoyeo · 45 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: