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
Related videos on Youtube
Author by
dave
Updated on September 18, 2022Comments
-
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 almost 9 yearsYou might find
cp -a
useful, as it adds to-r
extra options to preserve file attributes in the copied files. Alsocp -ua
will copy only updated files if you need to repeat the copy.
-
-
dave almost 9 yearssee 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 almost 9 years@dave then just specify the paths to the directories... I will update my answer.
-
John1024 almost 9 yearsIf it's a toss-up, give the answer mark to ethanwu10: he was 30 seconds faster than me.
-
John1024 almost 9 yearsThe OP only wanted
folderTwo
copied. This would also copyfolderOne
.