brettbrewer.com

Programming + Design

Home
How to recursively copy directories in Linux PDF Print E-mail
Written by Brett Brewer   
Monday, 24 March 2008

I had a problem using the Linux "cp" command to recursively copy directories today. Normally you can use the following command to recursively copy directories and overwrite any existing files in the target directory:

cp -Rfd /source/dir  /destination

 however, this will not work on many RedHat based systems because the "cp" command is aliased by default to "cp -i" which forces interactive mode to confirm overwrites of all files. If you don't know this it can be very infuriating. You could simply re-alias the cp command, but it's probably better to create a new alias so you don't alter the expected default system behavior of the cp command in case your system is touched by admins who don't expect the cp command to be re-aliased to allow non-ineractive file overwrites. Instead, you can creatie your own alias to the main cp binary to bypass the aliasing of the cp command entirely and run your own command line switches. Usually cp is located at "/bin/cp" but to find yours you can usually type "which cp" at the command line and it will tell you the full path to the cp binary. Once you know the path, just create a new alias such as:

 alias cprecursive='/bin/cp -Rfd' 

If you want to make this a permanent alias, you might want to add it to the /root/.bashrc  or the corresponding file in your /home/userdir. 

Also, please note, that when copying one directory to another, you specify the source directory normally, but for the target you only specify the directory that should CONTAIN the directory you are copying. So for example if you wanted to copy the images directory from a test site to a live site (and assuming you've already created the above alias) you might do something like this:

cd /home/userdir/public_html

cprecursive mytestsite/images mylivesite

this would copy the "images" directory from the "mytestsite" directory into the "mylivesite" directory, creating the mylivesite/images directory if necessarry and recursively creating/overwriting any files/directories contained within. 

Last Updated ( Tuesday, 25 March 2008 )
 
< Prev

Search

Who's Online


© 2017 www.brettbrewer.com
Joomla! is Free Software released under the GNU/GPL License.