Copying a directory
I want to copy the entire contents of a directory from one location to another. The source directory consists of a number of subdirectories in a tree like structure.
The normal 'cp source destination' command only copies the contents that directly fall into the source directory and skips those in the subdirectories.
Is there any command that can automatically create the subdirectories at the destination and copy the files correspondingly from the source location (ie create a replica of the source directory at the destination location).
Could anyone help me with this?
Use -r option.
cp -r <source> <destination>
It worked out. Thanks a lot :)
Aye, 'cp -r ...' is the way to do this. The 'cp' tool is quite powerful in what it can do, take a look at its manual page - which you can see on your terminal by typing 'man cp'.
You might also want to investigate the use of 'cp -a <src> <dest>' which also copies all the file permissions and ownership of the files being copied. It produces a replica of the directory tree suitable as a backup, which may be more than you're trying to achieve.
just as an FYI, if you're doing this copying of the same source dir to the same destination dir repeatedly, you can look into rsync, e.g.:
that will copy all the files and subdirs, along with their permission (like the "cp -a" above) from the source dir to the destination (creating the destination dir, if it does not already exist), but will have the added benefit of being more efficient on the subsequent runs, as it will only copy over new files and files that have changed in the source.
rsync -av /source/ /destination/
There are gazillions of options for rsync (see "man rsync") so you can customize your rsync commands heavily.