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

2

1

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.

dave

Posted 2015-09-20T20:02:37.867

Reputation: 673

1You 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. – AFH – 2015-09-20T20:30:43.320

Answers

6

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

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

ethanwu10

Posted 2015-09-20T20:02:37.867

Reputation: 931

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.. – dave – 2015-09-20T20:12:48.133

@dave then just specify the paths to the directories... I will update my answer. – ethanwu10 – 2015-09-20T20:13:35.957

4

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

John1024

Posted 2015-09-20T20:02:37.867

Reputation: 13 893

If it's a toss-up, give the answer mark to ethanwu10: he was 30 seconds faster than me. – John1024 – 2015-09-20T20:15:16.480

0

Simplification for rsync:

rsync -r someDirA/ someDirB

Note there is a trailing slash (/) at the end of the first argument; it is necessary to mean "the contents of someDirA".

Edit:

rsync -r --exclude 'folderOne' someDirA/ someDirB 

Yekoof

Posted 2015-09-20T20:02:37.867

Reputation: 1

The OP only wanted folderTwo copied. This would also copy folderOne. – John1024 – 2015-09-20T20:33:53.157