E-Mail:
Get our new Windows 7 eBook (PDF) for $7 with 70+ Tips. Download Now!

Moving Directory Trees

  • No Related Post

Moving Directory Trees

Sometimes you need to copy every file in a directory to another. The copying needs to include symbolic links, permissions, etc. As could be expected, you have several options in Linux for accomplishing this task.

First, you could use the cp command, adding the -r option to assure that everything is copied recursively. That doesn’t really guarantee that you’ll get everything, though. cp has a difficult time with symlinks. Here’s a better command using tar that will accomplish precisely what you want to accomplish:

    cd /home/tony/newfiles
    tar cf - . | (cd ./archive; tar xvf -)

What exactly does this magical two lines of tar goop do? Easy question. First, you’re changing to the newfiles subdirectory in my home directory. Then, tar [c]reates a [f]ile from the entire contents of the /home/tony/newfiles directory. This file never actually gets stored anywhere, though, as it’s piped directly to a subshell. The subshell changes directories [cd] to the archive subdirectory within /home/tony/newfiles [./archive] and untars [x] [v]erbosely the [f]ile passed to it from the original tar command. You end up with a precise copy of everything from /home/tony/newfiles in /home/tony/newfiles/archive, symlinks, permissions and all.

It’s almost like running a whole block without ever touching the ground.

What Do You Think?

 
35 queries / 0.369 seconds.