0
0
Linux-cliHow-ToBeginner · 3 min read

How to Copy File in Linux: Simple Command Guide

To copy a file in Linux, use the cp command followed by the source file and the destination path. For example, cp source.txt destination.txt copies the file named source.txt to destination.txt.
📐

Syntax

The basic syntax of the cp command is:

  • cp [options] source destination

Here, source is the file you want to copy, and destination is where you want the copy to be placed. Options can modify the behavior, like preserving file attributes or copying directories.

bash
cp source_file destination_file
💻

Example

This example copies a file named notes.txt to a new file named backup_notes.txt in the same directory.

bash
cp notes.txt backup_notes.txt
ls -l backup_notes.txt
Output
-rw-r--r-- 1 user user 1234 Apr 27 12:00 backup_notes.txt
⚠️

Common Pitfalls

One common mistake is forgetting that cp will overwrite files without warning. Also, copying directories requires the -r option to copy recursively.

Wrong way (copy directory without -r):

cp myfolder backupfolder

This will fail with an error.

Right way (copy directory recursively):

cp -r myfolder backupfolder
📊

Quick Reference

OptionDescription
-rCopy directories recursively
-iPrompt before overwrite
-vShow files being copied
-pPreserve file attributes

Key Takeaways

Use cp source destination to copy files in Linux.
Add -r option to copy directories recursively.
Be careful as cp overwrites files without warning by default.
Use -i option to get a prompt before overwriting files.
Use ls -l to verify the copied file details.