how to copy a directory (folder and its contents) to another directory?

27,784

Solution 1

cp -r /path/to/someDirA/folderTwo /path/to/someDirB/

The -r option to cp tells it to recurse on directories, copying its contents.

Solution 2

You can use cp to copy files and directories:

cp -r /path/to/someDirA/folderTwo /path/to/someDirB

The -r option is needed when copying directories.

Alternatively, you can use rsync:

rsync -a  /path/to/someDirA/folderTwo /path/to/someDirB
Share:
27,784

Related videos on Youtube

dave
Author by

dave

Updated on September 18, 2022

Comments

  • dave
    dave almost 2 years

    Here is my file structure:

    - someDirA
      -  folderOne
      -  folderTwo
         - somefile.txt
         - someotherfile.txt
    
    - someDirB
      -  somefolder
    

    What I want 'someDirB' to look like:

    - someDirB
      - somefolder
      - folderTwo
        - somefile.txt
        - someotherfile.txt
    

    I just want to copy someDirA's folderTwo(folder and all its contents) into someDirB. Both directories are in separate paths.

    • Admin
      Admin almost 9 years
      You might find cp -a useful, as it adds to -r extra options to preserve file attributes in the copied files. Also cp -ua will copy only updated files if you need to repeat the copy.
  • dave
    dave almost 9 years
    see thats the problem, i the folder i'm copying is not in the same directory as the folder i'm trying to copy too. Both someDirA/folderTwo and someDirB are in separate paths...just like how i posted above..
  • ethanwu10
    ethanwu10 almost 9 years
    @dave then just specify the paths to the directories... I will update my answer.
  • John1024
    John1024 almost 9 years
    If it's a toss-up, give the answer mark to ethanwu10: he was 30 seconds faster than me.
  • John1024
    John1024 almost 9 years
    The OP only wanted folderTwo copied. This would also copy folderOne.