Linux - cp command in linux
What Is cp?
The cp command is used to copy files and directories from one location to another.
Basic Syntax
cp [options] source destination
source: The file or directory you want to copydestination: The target location or filename
Examples
Copy a file
cp file1.txt file2.txt
This copies the contents of file1.txt into a new file called file2.txt.
Copy a file into a directory
cp file1.txt /home/user/documents/
Copies file1.txt into the documents directory.
Copy multiple files
cp file1.txt file2.txt /home/user/documents/
Copies both files into the specified directory.
Copy a directory recursively
cp -r myfolder /home/user/backup/
Copies the entire myfolder and its contents into the backup directory.
Common Options
| Option | Description |
|---|---|
-i |
Prompts before overwriting existing files |
-v |
Displays the files being copied |
-u |
Copies only if the source is newer than the destination |
-p |
Preserves file attributes like timestamps and permissions |
-r or -R |
Recursively copies directories |
Tip
Use cp -iv for a safer copy operation that shows what’s happening and asks before overwriting.
Would you like to see how to use wildcards with cp, like copying all .txt files at once?