To copy an entire directory that contains files and sub-directories in it, make use of the cp (Copy) command with a -r or --recursive option.
# tree -L 3 mydir
mydir
├── dir-1
│ └── fil1.txt
├── dir-2
├── file2.txt
└── file3.csv
Copy entire directory Example
# cp -r mydir mydir-copy
In the above example we have simply created a clone or a copy of the directory mydir at the same location.
# ls -ltrh
total 0
drwxr-xr-x 6 code2care staff 192B Nov 7 17:21 mydir
drwxr-xr-x 6 code2care staff 192B Nov 7 17:24 mydir-copy
We can confirm from the above that the size of both directories is the same, so it was entirely copied.
# tree -L 3 mydir-copy
mydir-copy
├── dir-1
│ └── fil1.txt
├── dir-2
├── file2.txt
└── file3.csv
Note: If you forget to pass the -r flag, you will get an error and the directory will not get copied.
# cp dir1 dir2
cp: dir1 is a directory (not copied).

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!