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!
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
More Posts related to Linux,
- Command to know the Available Memory on Linux System
- How to install curl on Alpine Linux
- How to backup a file in Linux/Unix
- Install Java Runtime Environment (Oracle or open JRE) on Ubuntu
- What is the Default Admin user and Password for Jenkins
- How to tar.gz a directory or folder Command
- Copy entire directory using Terminal Command [Linux, Mac, Bash]
- Fix: bash: ipconfig: command not found on Linux
- Command to check Last Login or Reboot History of Users and TTYs
- Linux: Create a New User and Password and Login Example
- ls command to list only directories
- bash: cls: command not found
- How to exit from nano command
- Installing and using unzip Command to unzip a zip file using Terminal
- What does apt-get update command does?
- ls command: sort files by name alphabetically A-Z or Z-A [Linux/Unix/macOS/Bash]
- How to remove or uninstall Java from Ubuntu using apt-get
- scp: ssh: connect to host xxxx port 22: Connection refused Error
- Sort ls command by last modified date and time
- Create Nested Directories using mkdir Command
- How to Exit a File in Terminal (Bash/Zsh)
- Command to know the installed Debian version?
- How to connect to SSH port other than default 22
- How to save a file in Nano Editor and Exit
- Install OpenSSL on Linux/Ubuntu
More Posts:
- Special character & not working with SharePoint REST API - SharePoint
- [fix] Execution failed for task :core:compileJava - Could not find tools.jar - Gradle
- [Fix] Java - Exception in thread main java.lang.IllegalThreadStateException - Java
- [Fix] Docker Run unknown shorthand flag: 'r' in -rm - Docker
- How to Set JAVA_HOME PATH to Mac .zshrc profile file - Java
- How to create a Java Project as a Git Repository with IntelliJ - Java
- Open Large File in Notepad++ (Fix: File is too big to be opened by Notepad++) - NotepadPlusPlus
- Right Align Text in Bootstrap framework - Bootstrap