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!
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:
- How to create a New Project in Visual Studio Code (VS Code) - HowTos
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier. - Git
- Ubuntu: How to search for a package to install using apt in Terminal - Ubuntu
- How to Check Python Version in Jupyter Notebook - Python
- Check installed Python version in Windows, Linux & macOS - Python
- [Notepad++] Remove CR Carriage Return and LF Line feed (CRLF) - NotepadPlusPlus
- Git Revision Questions Before the Interview - Git
- How to delete all text after a character or string in Notepad++ - NotepadPlusPlus