Copy a File
Copy a File
Before correcting a student's mark, the teacher wants to keep the earlier report. A regular-file copy gives her a separate snapshot of its contents. She can then edit the working report without automatically changing the backup. This is the everyday job of cp.
The word 'copy' is important here. It does not mean a live connection between the two reports. If the original changes tomorrow, today's backup stays as it was unless you copy again or use a separate synchronization tool.
What you’ll learn
- Create a separate backup with cp.
- Explain why editing the original does not update its copy.
A backup stays independent after an edit
Original
The mark list begins with the first value.
marks.txt: 72Copy
cp creates a separate regular file containing the current bytes.
backup.txt: 72Later edit
Replacing the original leaves that existing copy unchanged.
marks.txt: 80; backup.txt: 72
Back up the marks before changing the original
Prepare the example
Use a new practice directory so neither marks.txt nor backup.txt already exists. We will write the first mark, make a copy, deliberately change the original, then read the backup. All four steps are in the block below.
Bash · Linux terminal
printf 'Diya: 72\n' > marks.txt
cp marks.txt backup.txt
printf 'Diya: 80\n' > marks.txt
cat backup.txtExpected result
Diya: 72First, marks.txt receives Diya's mark of 72. cp marks.txt backup.txt reads those bytes and creates the separate destination file. The following printf writes a corrected mark of 80 to the original using >.
Why does the final line still print 72? It reads backup.txt, which is an independent regular-file copy in this example. Nothing asked cp to keep watching the source. This is useful for preserving a version, but it is also a reason to check the date and contents of a backup before assuming it reflects the latest work. A copy command can overwrite an existing destination, so use confirmation when needed.
Copying into a file or an existing directory
The cp command creates another copy of a file. The original stays where it is, which makes cp useful for backups, duplicate working files and moving data into another folder without removing the source.
Command We'll Use: cp
The command to copy a file in Linux is:
cp source_file destination_file
Let’s walk through it with some examples.
Example 1: Copy a File in the Same Directory
Suppose you have a file called notes.txt and you want to create a backup of it called notes_backup.txt. Here’s what you do:
cp notes.txt notes_backup.txt
This will create a new file called notes_backup.txt in the same folder.
Expected output for this example:
No message is shown if successful. Use 'ls' to confirm the copy.
Example 2: Copy a File to Another Directory
Let’s say you want to copy photo.jpg into a folder called backup/:
cp photo.jpg backup/
This places photo.jpg inside the backup folder.
Expected output for this example:
No output means it worked. Run 'ls backup' to verify.
Destination names, errors and verbose feedback
Example 3: Copy a File and Rename It in the Destination
You can also copy and rename at the same time:
cp report.docx archive/report_2024.docx
This puts a renamed copy of the file into the archive/ directory.
If the source file cannot be found
If you try to copy a file that doesn’t exist, you’ll get an error like:
cp: cannot stat 'fakefile.txt': No such file or directory
Always double-check the file name using ls or pwd if you’re not sure where you are.
Use -v for Verbose Mode
Want to see what’s happening as you copy? Use the -v flag (v for verbose):
cp -v old.txt new.txt
'old.txt' -> 'new.txt'
Copy files without losing the source
- Use
cp source destinationto copy files - Add
-vto see what’s being copied - Use
lsto confirm your file is there - Always check for typos to avoid errors
Try it, then check the worked answer
Your task: Explain why backup.txt still contains 72 after the original becomes 80.
Use a new practice directory so neither marks.txt nor backup.txt already exists. We will write the first mark, make a copy, deliberately change the original, then read the backup. All four steps are in the block below.
Bash · complete example for this question
printf 'Diya: 72\n' > marks.txt
cp marks.txt backup.txt
printf 'Diya: 80\n' > marks.txt
cat backup.txtExpected result
Diya: 72Worked answer
It is a separate regular-file copy.
The backup still contains 72 because it captured the earlier bytes. To see the correction, read marks.txt instead. Copying again would update the backup explicitly, but then it would no longer preserve the earlier version you wanted to keep.
Check your decision
A file copy captures a moment, not future edits
Use cp when you want the source to remain and a separate copy to appear. Decide whether the destination is a new file, an existing file or a directory before running it. A snapshot is valuable precisely because later edits do not silently update it.
Reaching the recap and completing the quizzes are separate milestones.