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!
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to check uptime of Linux/Unix/macOS system/server using console command?
- How to create tar.gz file using Terminal Command
- How to List All Users in Linux
- How to check file permissions for your file using Linux/Unix/macOS Terminal Command
- How to exit from nano command
- How to go to the End of File in Nano Editor
- ls command sort by file size [Linix/UNIX/macOS/bash]
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- How to tar.gz a directory or folder Command
- Linux: Create a New User and Password and Login Example
- How to install npm on Ubuntu
- Create and write file in single Linux/macOS command
- 3 ways to clear screen on Linux Terminal
- How to ls command to output one entry per line [macOS/Linux/Bash]
- Execute .bin and .run file Ubuntu Linux
- How to install and Configure sar sysstat tools in Ubuntu Linux
- Fix - ssh-copy-id no identities found error
- Terminal display next month Calendar
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- Fix - bash: man: command not found
- How to install curl on Alpine Linux
- Command to unzip or extract tar.gz file on Linux or macOS?
- How to Display content of a file in Terminal Screen?
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Open Current Working Directory in Finder using Mac Terminal - MacOS
- Whats new in Python 3.10 Pre-release - Python
- How to know the current version of Java - Java
- How to make a dummy phone call from Android Emulator device - Android
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager - Android
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement - Java
- PHP Base64 encoding decoding a variable - PHP
- How to Whitelist IP Address on GoDaddy Hosting? - HowTos