How to Copy Entire Directory to another Directory in Linux


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).
Copy Entire Directory Command Example
-


Have Questions? Post them here!

Topics Coverage:


🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram