Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By devils casper
  • 1 Post By Roxoff
  • 1 Post By atreyu
Hi, 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. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10

    Copying a directory


    Hi,

    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?

    Thanks.

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    Use -r option.
    Code:
    cp -r <source> <destination>
    dhivya34 likes this.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    10
    It worked out. Thanks a lot

  4. $spacer_open
    $spacer_close
  5. #4
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,932
    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.
    dhivya34 likes this.
    Linux user #126863 - see http://linuxcounter.net/

  6. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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.:

    Code:
    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.
    dhivya34 likes this.

Posting Permissions

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