What is the difference between ln, cp, and mv in Linux?
Understand the difference between ln, cp, and mv in Linux: copying files, moving and renaming, and hard vs symbolic links, with clear examples.
Expected Interview Answer
cp copies a file to create an independent duplicate, mv moves or renames a file so only one copy exists in the new location, and ln creates a link (a hard link or, with -s, a symbolic link) that points to an existing file rather than duplicating its data.
cp allocates new storage and produces a separate copy, so editing one does not affect the other. mv relocates or renames the file; within the same filesystem it just updates the directory entry without copying data, but across filesystems it copies then deletes the original. ln with -s creates a symbolic link, a small pointer holding a path that breaks if the target moves, while a hard link (no -s) is another directory entry for the same underlying inode, so the data survives until all hard links are removed.
- cp preserves the original while making a duplicate
- mv renames or relocates without leaving a copy
- ln -s lets many names reference one file
- Hard links share data without extra storage
- Symbolic links can point across filesystems
AI Mentor Explanation
cp is like photocopying the match scorecard so each analyst has their own sheet to mark independently. mv is like handing your only scorecard to the next scorer, keeping just one copy that changed hands. ln -s is like pinning a note on the dressing-room door saying 'scorecard is in the pavilion office' — follow the pointer to the one real sheet.
Step-by-Step Explanation
Step 1
Decide the intent
Do you want a duplicate (cp), to relocate or rename (mv), or to reference an existing file (ln)?
Step 2
Copy with cp
cp source dest creates an independent copy; add -r to copy directories recursively and -p to preserve attributes.
Step 3
Move or rename with mv
mv old new relocates or renames; within a filesystem it just updates the directory entry, across filesystems it copies then deletes.
Step 4
Create a symbolic link
ln -s target linkname makes a pointer holding target's path; it breaks if the target is moved or deleted.
Step 5
Create a hard link
ln target linkname adds another name for the same inode; the data persists until every hard link is removed.
What Interviewer Expects
- cp duplicates, mv relocates, ln links
- Difference between hard and symbolic links
- Understanding inodes behind hard links
- That mv within a filesystem does not copy data
- When a symbolic link becomes dangling
Common Mistakes
- Thinking mv always copies data
- Confusing symbolic links with hard links
- Believing deleting a symlink deletes the target
- Forgetting -r when copying directories with cp
- Assuming hard links can span different filesystems
Best Answer (HR Friendly)
“cp makes a separate copy of a file, mv moves or renames it so there's still just one, and ln makes a link that points to an existing file instead of copying it. Think copy, move, and shortcut.”
Code Example
# Copy a file (independent duplicate)
cp report.txt report-backup.txt
# Copy a directory recursively, preserving attributes
cp -rp project/ project-copy/
# Rename or move a file (only one copy remains)
mv report.txt archive/report-2026.txt
# Create a symbolic link (pointer to a path)
ln -s /usr/local/app/current app-latest
# Create a hard link (another name for the same inode)
ln data.bin data-alias.binFollow-up Questions
- What is the difference between a hard link and a symbolic link?
- Why can't hard links cross filesystem boundaries?
- What happens to a symlink if its target is deleted?
- How does mv behave across different filesystems?
- How do you copy a directory and preserve permissions and timestamps?
MCQ Practice
1. Which command creates an independent duplicate of a file?
cp allocates new storage and produces a separate copy independent of the original.
2. What does ln -s create?
ln -s creates a symbolic link, a pointer that stores the path to the target.
3. Within the same filesystem, moving a file with mv mainly does what?
On the same filesystem mv just updates the directory entry, so no data is physically copied.
Flash Cards
What does cp do? — Creates an independent copy of a file or directory (use -r for directories).
What does mv do? — Moves or renames a file, leaving only one copy.
ln -s vs ln? — ln -s makes a symbolic link (path pointer); ln makes a hard link (another name for the same inode).
When does a symlink break? — When its target is moved or deleted, leaving a dangling link.
Can hard links cross filesystems? — No — hard links share an inode, which is filesystem-specific.