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

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Linux,
- Rename a directory using Linux/Unix command
- ls command to list only directories
- How to Restart or Reload Nginx Server Service on Linux
- 3 ways to clear screen on Linux Terminal
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: sudo: unable to open Read-only file system
- Create Hidden File or Directory using Shell Command
- Command to know the installed Debian version?
- The Date Command and its usage [Linux - Unix - macOS]
- Fix - bash: man: command not found
- How to tar.gz a directory or folder Command
- How to Display content of a file in Terminal Screen?
- How to change bash terminal prompt string and color
- Sort ls command by last modified date and time
- Execute .bin and .run file Ubuntu Linux
- zsh hello world example
- How to check uptime of Linux/Unix/macOS system/server using console command?
- [Fix] Linux - bash: useradd: command not found
- Command to check Last Login or Reboot History of Users and TTYs
- How to install and Configure sar sysstat tools in Ubuntu Linux
- How to use SCP Command to Copy Directory
- Linux Remove or Delete Files and Directories using Terminal Commands
- How to connect to SSH port other than default 22
- Install OpenSSL on Linux/Ubuntu
More Posts:
- Replace new line with comma in Sublime Text Editor - Sublime-Text
- CSS Background Opacity with Examples - CSS
- Fix: ReferenceError: require is not defined in ES module scope [Node] - JavaScript
- Convert Map to List in Java 8 using Stream API - Java
- Use Netbeans keyboard shortcuts in Android Studio - Android-Studio
- How to Restart Apple Silicon based Mac (M1/M2 Chip) in Safe Mode - MacOS
- Android Studio Change SDK Path - Android
- Change TextEdit File Encoding - MacOS