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).

-
Have Questions? Post them here!
More Posts related to Linux,
- [Fix] Linux - bash: useradd: command not found
- Calculate days between dates using dateutils ddiff command
- pwd Command - Print Working Directory
- Rename a directory using Linux/Unix command
- How to go to the End of File in Nano Editor
- How to tar.gz a directory or folder Command
- How to remove/delete a directory in Linux/macOs
- How to Display content of a file in Terminal Screen?
- ls command sort by file size [Linix/UNIX/macOS/bash]
- How to Copy Entire Directory to another Directory in Linux
- 3 ways to clear screen on Linux Terminal
- Command to check Last Login or Reboot History of Users and TTYs
- How to check uptime of Linux/Unix/macOS system/server using console command?
- How to backup a file in Linux/Unix
- Command to know the installed Debian version?
- Check SSH/OpenSSH version Command
- 3 Commands to stop Nginx Server
- Linux: Create a New User and Password and Login Example
- How to install npm on Ubuntu
- How to List All Users in Linux
- How to Get the List of Shells on Linux
- Fix - ssh-copy-id no identities found error
- Installing and using unzip Command to unzip a zip file using Terminal
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- zsh hello world example
More Posts:
- Struts 2 Hello World Example in Eclipse - Java
- Read JSON File in Python Program - Python
- Open Large File in Notepad++ (Fix: File is too big to be opened by Notepad++) - NotepadPlusPlus
- Install AWS CLI using Brew Command - AWS
- Know Bash shell version command - Bash
- Mac - Chrome Open a new window in Incognito mode - Chrome
- Perform an Empty Commit in Git without anything in Staging Area - Git
- List of Java Major Minor Version Numbers - Java