Home > Linux > Copy an Entire Directory in Linux

Copy an Entire Directory in Linux

November 4th, 2005 Tony Leave a comment Go to comments

Yeah, this is simple but I’m forgetful so heres how you copy an entire directory, its subdirectories, and files:

cp -R dirtocopy/ newdir/

Categories: Linux Tags:
  1. Ankush
    June 23rd, 2006 at 13:19 | #1

    Here’s how to do the copy shown in the figure below. This copies the directory /home/jane, with all its files and subdirectories, and creates a subdirectory named jane in the current directory (.) :

    cp -r /home/jane .

  2. Tamim Daiyan
    January 10th, 2007 at 15:01 | #2

    One thing missing, like it is mandatory to give the path name where u want to copy.If u keep blank, it will copy in the same directory.It will be

    cp-rf present/directory /desire/directory

    “f” parameter passed to do it “forcefully”.

    Another command is, if u can’t remember the “r”[recursively] parameter then try with

    cp present/directory/* /desire/derectory

    Also keep in mind, in anycase, use the slash carefully.If u r in root mode then it might give u some pain.

  3. Logan
    January 20th, 2007 at 14:49 | #3

    This just helped me. Thanks!

  4. Tim
    January 21st, 2007 at 03:29 | #4

    Handy info. Thanks.

  5. Prashanth
    March 22nd, 2007 at 23:38 | #5

    Handy info. Thanks

  6. Andy
    March 23rd, 2007 at 09:48 | #6

    Thanks
    You were top on google.co.uk for “linux copy directory”
    Just info I needed

  7. Dennis
    March 29th, 2007 at 11:13 | #7

    Thanks :)

  8. truth machine
    April 27th, 2007 at 05:45 | #8

    “You were top on google.co.uk for “linux copy directory””

    That’s sad.

    cp -a is better, as it preserves timestamps and symlinks. If you also want to preserve hardlinks, use cpio -p or rsync -H

  9. Googler
    May 15th, 2007 at 10:28 | #9

    Thanks!

  10. Andy
    June 11th, 2007 at 20:33 | #10

    Thank u very much :)

  11. vicky
    July 24th, 2007 at 10:37 | #11

    Awesome Dude!

  12. tDizzle
    August 23rd, 2007 at 13:33 | #12

    U kik ass :D

  13. john
    September 23rd, 2007 at 02:27 | #13

    Thank you ^_^

  14. Joost
    November 20th, 2007 at 05:27 | #14

    Top of google, gave me what I needed!

  15. Meina
    November 29th, 2007 at 04:12 | #15

    Top of google! Thanks!

  16. Robert
    December 29th, 2007 at 23:55 | #16

    Awesome! Thanks much!

  17. Mitul
    January 8th, 2008 at 13:25 | #17

    Thanks a lot man.. really helped me…

  18. January 9th, 2008 at 23:28 | #18

    cp –reply=yes -r * folder2(path of folder you want to copy to)

  19. shohn
    January 29th, 2008 at 15:22 | #19

    Wow! Thanks dude! Amazing info! Top of google too! You rock!! :D

  20. February 6th, 2008 at 04:49 | #20

    Spot on! Thanks heaps.
    The definition in “man cp” is a bit confusing.
    I mean, what’s ‘recursively’ meant to mean anyway?

    PS: “cp -r sourcedir .” will copy ’sourcedir’ to the current directory (hence ‘.’) so if you want it put somewhere else you need to specify this instead of ‘.’
    Also, remember that any path beginning with ‘/’ must start in the root directory (ie: “cp -r /folder” will copy the ‘folder’ directory from the root directory ‘/’), otherwise it must be in the current directory (ie: ‘.’)

  21. miles
    February 21st, 2008 at 13:42 | #21

    I need help to!
    i just need the files copied. no create a subdirectory.which
    cp -r /dir1 /dir2 does. dir1 will be a sub in dir2 which I don’t need i just need the non-hidden files copied into dir2. I tried /dir1/*.*
    it didn’ work.Please help.

  22. KRowe
    February 24th, 2008 at 03:34 | #22

    Thanks, this info helped.

    @samjetski
    In programming recursive means anything which calls itself. In this case you are telling cp to call ‘cp sourcepath/curdir/* destpath/curdir/’ on each directory.

  23. March 6th, 2008 at 22:06 | #23

    Thanks, Dude. Saved me a trip to the man page ;-) As others have said, you are the top result for googling “cp directories linux”. Pretty cool!

    truth machine had a point, though: “cp -a” is the best method to recreate the directory structure on a new drive the way it is currently on an old drive (for example).

    that said…

    @tm: the -a argument is best for doing what I said above, but the way notsleepy suggested works for backups, or copying dirs that contain no symlinks

  24. Ketan
    March 19th, 2008 at 20:22 | #24

    Handy info. Thanks

  25. April 4th, 2008 at 12:22 | #25

    Ankush was accurate. The space and the dot at the end of what he typed was the destination directory, aka the current directory.

  26. Mahdi
    May 7th, 2008 at 15:32 | #26

    Thanks for the info!

  27. May 26th, 2008 at 21:58 | #27

    Thanks for the info. thats helped me

  28. Tamim Daiyan
    July 20th, 2008 at 01:02 | #28

    I am glad that i could be at everyones help……the pleasure is all mine….:)

  29. Ammar Mardawi
    August 9th, 2008 at 02:51 | #29

    Will this also copy hidden sub folders?

  30. akkii
    September 6th, 2008 at 09:19 | #30

    thanx buddy…. ;P

  31. Taruna
    January 15th, 2009 at 05:32 | #31

    Thanks, u have answered very precisely

  32. Pnoykalbo
    January 21st, 2009 at 12:25 | #32

    You da man!!! Danka!

  33. jc narasimhan
    March 6th, 2009 at 23:50 | #33

    Truth machine, cp -a is an amazing cool command. thanks

  34. rep71Le
    March 19th, 2009 at 14:42 | #34

    Nice tip, thanks a bunch ^^ Gotta love googling for help when everything else fails :p

  35. September 14th, 2011 at 08:47 | #35

    Well I’ll be, I search on the Google for this, and this website pops up first. Good job on that SEO :)