If you want to copy a file or a directory from one path to another using the Linux Terminal, you can make use of the cp (copy) command.
But, when you have to copy directories that in them have multiple other files and directories, then you need to make use of the -r option to copy them recursively.
Let's take a look at an example.
cp -r Command Syntax:
cp -r <source-directory> <destination-directory>
cp -r command example
cp -r dir1 dir2
Let's see whats there in dir2
% ls dir2
dir1
% cd dir1
ls -ltrh
% ls -ltrh
total 0
drwxr-xr-x 7 c2ctechtv staff 224B Aug 4 13:58 temp
-rw-r--r-- 1 c2ctechtv staff 0B Aug 4 13:58 data.csv
-rw-r--r-- 1 c2ctechtv staff 0B Aug 4 13:58 2023.txt
-rw-r--r-- 1 c2ctechtv staff 0B Aug 4 13:58 data.txt
More Examples:
If you want to see the process of the cp -r command, and the -v flag to the command.
% cp -rv dir1 dir2
dir1 -> dir2
dir1/temp -> dir2/temp
dir1/temp/a -> dir2/temp/a
dir1/temp/c -> dir2/temp/c
dir1/temp/d -> dir2/temp/d
dir1/temp/e -> dir2/temp/e
dir1/temp/b -> dir2/temp/b
dir1/data.csv -> dir2/data.csv
dir1/2023.txt -> dir2/2023.txt
dir1/data.txt -> dir2/data.txt
If you want to preserve the file's attributes such as timestamps, ownership makes use of the -p flag.
cp -rp dir1 dir2
To copy the hidden files and folders as well,
cp -rp dir1/. dir2
To exclude cetain files.
cp -rp dir1 dir2 --exclude=data.csv
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- 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
- Install SonarLint on Visual Studio Code - HowTos
- Enable Cloud Based Clipboard for Images and Text on Windows 10/11 - Windows
- Microsoft Office Excel - Couldnt Open the Workbook - The workbook cannot be opened. - Microsoft
- Online XML Code Formatter (Prettify) Tool - Tools
- 21: Program to Delete File or Folder in Python - Python-Programs
- When Does macOS Sonoma 14 Release? 26 September 2023 - MacOS
- How to create a Task List in SharePoint Online - SharePoint
- [fix] Error response from daemon: conflict unable to remove repository reference ubuntu container is using its referenced image - Docker