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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap