E-Mail:

Moving Directory Trees

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?

 

Want to Start a Blog Here for Free?

Are you an expert in one subject or another? If your goal is to help others and dispense your hard-earned information back to the community, get involved in our community site today! You can write about anything - no matter the topic. Exceptional candidates will be offered the chance to contribute to (and generate revenue from) the main Lockergnome site. Join us today!

Tip - Nov 28, 2007

Quick Tip - Getting Started With GIMP

69 queries / 0.326 seconds.