Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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?


  2. #2
    SuperMod (Back again) devils casper's Avatar
    Join Date
    Jun 2006
    Chandigarh, India
    Use -r option.
    cp -r <source> <destination>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    It worked out. Thanks a lot

  4. $spacer_open
  5. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Nottingham, England
    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.
    Linux user #126863 - see

  6. #5
    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.:

    rsync -av /source/ /destination/
    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.

    There are gazillions of options for rsync (see "man rsync") so you can customize your rsync commands heavily.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts