LFCA 16 ๐ง Moving and Renaming โ mv
mv is the command that moves files and directories from one place to another โ and also the command that renames them. In Unix, those are the same operation: renaming a file is moving it to a new name in the same directory. That duality is the key to understanding mv. It’s fast because it often does no copying at all โ on the same filesystem, it just changes the directory entry, which is why it’s near-instantaneous even for huge files. This chapter covers the syntax, the destination rules, the flags, the surprising behaviors, and how it differs from cp.
Key point: mv SOURCE DEST moves or renames. If DEST is an existing directory, SOURCE moves into it. If DEST is a name (existing or not), SOURCE is renamed to that name. Unlike cp, mv doesn’t copy data when the source and destination are on the same filesystem โ it changes the directory entry, which is why it’s fast for large files. mv deletes the source; that’s the “move” part. Both move and rename use the same command because Unix treats a rename as a move to a new name.
What mv does
mv moves a file or directory from one location to another, or renames it.
$ echo "hello" > original.txt
$ mv original.txt renamed.txt
$ ls
renamed.txt
The file was renamed. The content is unchanged. original.txt no longer exists.
The two operations:
| Operation | Command |
|---|---|
| Rename | mv oldname newname |
| Move | mv file.txt /other/dir/ |
Both use the same command because a rename is a move โ to a new name in the same directory.
What mv does under the hood:
- Same filesystem: Renames the directory entry โ near-instant, regardless of size
- Different filesystems: Copies then deletes the source โ takes time proportional to size
- Across symlinks: Follows the target unless
-Tor careful flags are used
What mv doesn’t do:
- Doesn’t preserve the source โ it’s gone after the move
- Doesn’t copy data on the same filesystem
- Doesn’t ask before overwriting by default
- Doesn’t create parent directories (that’s
mkdir -p)
Why “mv” not “rename”: Because a move and a rename are the same operation in Unix. mv names the general case โ moving. The rename case is a move to the same directory with a different name.
Why this matters for performance: Moving a 10 GB file within the same filesystem is instant. Copying it and deleting the original would take minutes. That difference comes from mv being a metadata operation โ it changes where the directory points, not the file data.
Why the same command for both: A file’s name is part of its directory entry, not the file itself. Changing the name changes the entry. Changing the directory also changes the entry. Both are edits to the same structure. Unix exposes them as one command because they’re one operation.
Renaming files
Renaming is the simplest case.
$ mv report.txt final-report.txt
report.txt is now final-report.txt. No copy happened โ the file’s directory entry changed.
Adding an extension:
$ mv script script.sh
Removing an extension:
$ mv script.sh script
Renaming a directory:
$ mv old-folder new-folder
Directories rename the same way โ the entry changes, contents untouched.
Renaming with a different path:
$ mv ~/notes.txt /tmp/notes.txt
That’s a move and a rename โ to a new directory with the same name. Or:
$ mv ~/notes.txt /tmp/other-name.txt
Different directory, different name.
Why renaming is cheap: A rename on the same filesystem is a change to a directory entry โ a few bytes of metadata. The file’s inode and data are untouched. That’s why renaming a 100 GB directory is instant.
Why the syntax is symmetric: mv old new โ the source and destination are both names. The command doesn’t care whether they’re the same directory or different; it just moves the source to the destination name.
Why “inode” matters: On most filesystems, a file has an inode (which holds the metadata and points to the data) and one or more names in directories. Renaming changes the name in the directory โ the inode and data stay. That’s why renaming is a metadata operation.
Moving files
Moving a file into a directory is the other common case.
$ mv report.txt /tmp/
$ ls /tmp/report.txt
/tmp/report.txt
The file moved into /tmp, keeping its name.
The destination-is-a-directory rule:
$ mkdir archive
$ mv file1.txt file2.txt file3.txt archive/
$ ls archive/
file1.txt file2.txt file3.txt
When the destination is an existing directory, sources move into it.
Moving multiple files:
$ mv *.txt /tmp/
The shell expands *.txt; mv moves each into /tmp/.
The last argument must be a directory when moving multiple files:
$ mv a.txt b.txt dest-dir/
If the last argument isn’t a directory and there’s more than one source, mv errors.
Moving with a new name in the destination:
$ mv report.txt /tmp/summary.txt
The file is moved to /tmp and renamed summary.txt.
Why the destination rules matter: The same three cases as cp:
- Destination is a directory โ source moves into it, keeping its name
- Destination is a name (not an existing directory) โ source is renamed to that name
- Multiple sources โ the last argument must be a directory
Why moving is fast on the same filesystem: The file’s data isn’t touched. Only the directory entries change โ one in the source directory, one in the destination. It’s a couple of metadata writes.
Why the destination-is-a-directory rule is uniform: It applies to
mv,cp, andinstall. Any command that copies or moves multiple sources needs a directory as the destination. The rule is consistent across tools.
Moving directories
Directories move the same way as files.
$ mv old-folder /tmp/
$ ls /tmp/old-folder/
(contents)
The directory and everything inside it moves.
Renaming a directory:
$ mv old-name new-name
The directory’s name changes; contents untouched.
Moving a directory into another:
$ mv project /opt/apps/
$ ls /opt/apps/project/
(contents of project)
If /opt/apps/project doesn’t exist, project becomes it. If it does exist, project nests inside as /opt/apps/project/project.
The subtle nesting rule: Same as cp. If the destination exists as a directory, the source nests inside.
Moving directories across filesystems: mv copies recursively then deletes. Slower, but works.
Why directories are just entries: A directory is a special kind of file that lists other entries. Moving it changes its parent directory’s entry. The contents don’t need to change.
Why cross-filesystem moves are slower: Different filesystems have separate directory structures. mv can’t just change a pointer โ it has to recreate the file at the destination and delete the original.
Why the same nesting rule for directories: A directory is a file with entries. When you move it into an existing directory, the parent’s entry points to the moved directory. The nesting follows from how directory entries reference their targets.
mv -i, -n, -u โ handling existing files
Like cp, mv has flags for existing destinations.
-i โ interactive:
$ mv -i source.txt dest.txt
mv: overwrite 'dest.txt'? y
Prompts before overwriting.
-n โ no clobber:
$ mv -n source.txt dest.txt
Doesn’t overwrite. Silently skips if the destination exists.
-u โ update only:
$ mv -u source.txt dest.txt
Moves only if the source is newer than the destination.
When to use which:
| Flag | Use case |
|---|---|
-i | Interactive, confirm each overwrite |
-n | Scripting, never overwrite |
-u | Sync-like, move only if newer |
The alias trap: Many systems alias mv to mv -i. In interactive shells, mv prompts. In scripts, aliases don’t apply โ mv overwrites silently.
$ alias mv
alias mv='mv -i'
To bypass: \mv, command mv, or /bin/mv.
Why -i is often aliased: Accidental overwrites are a real hazard. The alias protects against them interactively but doesn’t affect scripts.
Why -n is safer in scripts: No prompt, deterministic behavior. If the script needs to overwrite, don’t use -n.
Why -u exists: When moving files from one directory to another, sometimes you only want newer ones. -u covers that case.
Why
cpandmvshare these flags: They answer the same question โ “what if the destination exists?” The flags are consistent across the tools. Learn them once, use them everywhere.
mv vs cp
The two commands look similar but differ in one crucial way.
| Aspect | cp | mv |
|---|---|---|
| Source after | Still exists | Gone |
| Same filesystem | Copies data | Renames entry |
| Cross-filesystem | Copies data | Copies then deletes |
| Speed | Proportional to size | Instant on same FS |
| Recursive flag | -r required | Not needed |
| Preserve metadata | -p, -a | Always (same FS) |
The key difference: cp preserves the source; mv removes it. After mv, the source is gone.
When to use which:
| Need | Use |
|---|---|
| Keep the original | cp |
| Move or rename | mv |
| Backup | cp |
| Reorganize | mv |
| Change a name | mv |
| Duplicate | cp |
Why mv doesn’t need -r for directories: Since mv doesn’t copy on the same filesystem, it doesn’t need to recurse. On a different filesystem, it recurses automatically.
Why mv preserves metadata automatically: On the same filesystem, the file’s inode is unchanged โ metadata is preserved by definition. Cross-filesystem mv preserves what the copy preserves; for metadata like timestamps, it may not.
Why this distinction is important: Using mv when you meant cp loses the original. Using cp when you meant mv leaves a duplicate. The intent โ keep or remove โ is the deciding factor.
Why
mvis faster: A rename on the same filesystem doesn’t touch the file data.cpreads and writes the entire file. For large files, that’s the difference between milliseconds and minutes. If you only need to change a name or location,mvis always the right tool.
Cross-filesystem moves
When mv crosses filesystems, it behaves like cp + rm.
$ mv /home/alice/video.mp4 /mnt/external/video.mp4
/home and /mnt/external are on different filesystems. mv copies the file to the destination, then deletes the source.
What this means:
- The move takes time proportional to the file size
- The destination is a new file (new inode)
- Metadata may not be preserved exactly
- Interrupting the move leaves a partial copy
How to tell: df shows the filesystem for each path.
$ df /home/alice/video.mp4
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 500000000 200000000 300000000 40% /
$ df /mnt/external
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sdb1 2000000000 500000000 1500000000 25% /mnt/external
Different Mounted on values mean different filesystems. A move between them copies.
Why this matters: If you mv a 100 GB file across filesystems, it takes minutes and needs free space at the destination. On the same filesystem, it’s instant.
Why no error is raised: mv handles both cases seamlessly. The user doesn’t have to know whether the move is same-filesystem or cross-filesystem โ mv figures it out.
Why interrupting is risky: If you cancel a cross-filesystem mv mid-copy, the destination has a partial file and the source still exists. Nothing is atomic. For critical files, use a copy tool with verification or a filesystem with snapshots.
Why the same command works for both: The filesystem layer handles the difference.
mvcallsrename(2)if the filesystems match, and copies otherwise. To the user, it’s one operation with one syntax.
A full example
Renaming, moving, and organizing files.
# ============================================
# PART 1: RENAME A FILE
# ============================================
cd ~
echo "draft" > report.txt
mv report.txt final-report.txt
ls
# final-report.txt
# ============================================
# PART 2: MOVE A FILE
# ============================================
mkdir archive
mv final-report.txt archive/
ls archive/
# final-report.txt
# ============================================
# PART 3: MOVE WITH RENAME
# ============================================
mv archive/final-report.txt archive/2025-report.txt
ls archive/
# 2025-report.txt
# ============================================
# PART 4: MOVE MULTIPLE FILES
# ============================================
touch a.txt b.txt c.txt
mkdir texts
mv a.txt b.txt c.txt texts/
ls texts/
# a.txt b.txt c.txt
# ============================================
# PART 5: RENAME A DIRECTORY
# ============================================
mv texts text-files
ls
# archive text-files
# ============================================
# PART 6: INTERACTIVE OVERWRITE
# ============================================
echo "old" > dest.txt
echo "new" > source.txt
mv -i source.txt dest.txt
# mv: overwrite 'dest.txt'? y
cat dest.txt
# new
# ============================================
# PART 7: NO-CLOBBER
# ============================================
echo "source" > source2.txt
echo "dest" > dest2.txt
mv -n source2.txt dest2.txt
cat dest2.txt
# dest
ls source2.txt
# source2.txt โ still exists
# ============================================
# PART 8: UPDATE ONLY
# ============================================
touch -d "2024-01-01" old.txt
touch new.txt # today
mv -u old.txt target.txt # old.txt older, no move if target newer
mv -u new.txt target2.txt # new.txt newer, moves
# ============================================
# PART 9: MOVE ACROSS FILESYSTEMS
# ============================================
# Find the filesystems
df /tmp
# /dev/sda2 ...
df /mnt/data 2>/dev/null || echo "no /mnt/data"
# If different filesystems, mv copies
mv /tmp/bigfile /mnt/data/ 2>/dev/null
# ============================================
# PART 10: MOVE A DIRECTORY INTO AN EXISTING DIRECTORY
# ============================================
mkdir project
mkdir parent
mv project parent/
find parent
# parent
# parent/project
Each part exercises a mv use case. The move-and-rename duality shows up in parts 1, 2, 3, and 5.
Why this session: It covers the practical range โ rename, move, move with rename, multiple sources, directory rename, the three flags, cross-filesystem, and the nesting rule. Once these are habits,
mvis second nature.
Complete Example Session
# ============================================
# PART 1: BASIC RENAME
# ============================================
echo "content" > oldname.txt
ls
# [ oldname.txt ]
mv oldname.txt newname.txt
ls
# [ newname.txt ]
# ============================================
# PART 2: MOVE TO DIRECTORY
# ============================================
mkdir archive
mv newname.txt archive/
ls archive/
# [ newname.txt ]
ls
# [ archive ]
# ============================================
# PART 3: MOVE AND RENAME
# ============================================
mv archive/newname.txt archive/final.txt
ls archive/
# [ final.txt ]
# ============================================
# PART 4: MULTIPLE SOURCES
# ============================================
touch one.txt two.txt three.txt
mkdir all
mv one.txt two.txt three.txt all/
ls all/
# [ one.txt three.txt two.txt ]
# ============================================
# PART 5: WILDCARD
# ============================================
touch more1.txt more2.txt
mv more*.txt all/
ls all/
# [ more1.txt more2.txt one.txt three.txt two.txt ]
# ============================================
# PART 6: RENAME DIRECTORY
# ============================================
mv all archive-all
ls
# [ archive archive-all ]
# ============================================
# PART 7: NESTING
# ============================================
mkdir existing
mv archive-all existing/
find existing -type d
# [ existing ]
# [ existing/archive-all ]
# ============================================
# PART 8: -i INTERACTIVE
# ============================================
echo "target" > target.txt
echo "src" > src.txt
mv -i src.txt target.txt
# [ mv: overwrite 'target.txt'? ]
# Type y
# Moved
cat target.txt
# [ src ]
# ============================================
# PART 9: -n NO CLOBBER
# ============================================
echo "keep" > keep.txt
echo "ignored" > ignored.txt
mv -n ignored.txt keep.txt
cat keep.txt
# [ keep ]
ls ignored.txt
# [ ignored.txt ] โ still exists
# ============================================
# PART 10: -u UPDATE
# ============================================
touch -d "2020-01-01" older.txt
touch target2.txt
mv -u older.txt target2.txt
# older.txt stays โ target2.txt is newer
# If older.txt were newer, it would move and overwrite
# ============================================
# PART 11: SAME FILESYSTEM โ INSTANT
# ============================================
# Create a large file
dd if=/dev/zero of=big.bin bs=1M count=100 2>/dev/null
time mv big.bin big-renamed.bin
# real 0m0.001s โ near-instant
# ============================================
# PART 12: CROSS-FILESYSTEM
# ============================================
# /tmp and /home may be on different filesystems
df /tmp | tail -1
# /dev/sda2 ... /
df /home | tail -1
# /dev/sda2 ... / โ same filesystem, mv is instant
# If different:
time mv big-renamed.bin /tmp/
# real 0m0.5s โ takes time to copy
# ============================================
# PART 13: RECAP
# ============================================
ls
# [ archive existing keep.txt src.txt target.txt target2.txt ... ]
# mv is used for both rename and move
# Same filesystem โ instant (metadata)
# Different filesystem โ copy + delete
Every part exercises a mv feature. The same-filesystem speed is visible in the time outputs.
Quick Reference
mv Syntax
| Form | Meaning |
|---|---|
mv SRC DEST | Rename or move |
mv SRC DIR/ | Move into directory |
mv SRC1 SRC2 DIR/ | Multiple sources |
mv -i SRC DEST | Interactive |
mv -n SRC DEST | No overwrite |
mv -u SRC DEST | Only if newer |
Common Flags
| Flag | Effect |
|---|---|
-i | Interactive โ prompt before overwrite |
-n | No clobber โ never overwrite |
-u | Update โ only if source newer |
-v | Verbose |
-f | Force โ no prompt |
-t DIR | Target directory first |
-T | Treat DEST as a normal file, not a directory |
Destination Rules
| Destination | Result |
|---|---|
| Doesn’t exist | Rename to that name |
| Existing file | Overwrite (unless -i, -n) |
| Existing directory | Move into it |
| Multiple sources | Last must be a directory |
mv vs cp
| Aspect | cp | mv |
|---|---|---|
| Source after | Exists | Gone |
| Same-FS speed | Copy time | Instant |
| Cross-FS speed | Copy time | Copy time |
Needs -r for dirs | โ | โ |
| Preserves metadata | With -p/-a | Always (same FS) |
Same-FS vs Cross-FS
| Case | Behavior |
|---|---|
| Same filesystem | Rename โ instant |
| Different filesystems | Copy then delete |
| Detect with | df PATH |
| Interrupted cross-FS move | Partial destination |
Common Patterns
| Task | Command |
|---|---|
| Rename file | mv old.txt new.txt |
| Move file | mv file.txt /tmp/ |
| Move and rename | mv file.txt /tmp/new.txt |
| Move multiple | mv *.txt /tmp/ |
| Rename directory | mv old-dir new-dir |
| Move directory | mv project /opt/ |
| Interactive | mv -i src dest |
| No overwrite | mv -n src dest |
| Update only | mv -u src dest |
Error Cases
| Message | Cause |
|---|---|
No such file or directory | Source doesn’t exist |
Permission denied | No write on target dir |
cannot move X to Y: Directory not empty | Trying to replace a non-empty dir |
cannot overwrite directory | Type mismatch |
same file | Source and destination are the same |
When to Use mv
| Situation | Use |
|---|---|
| Rename | mv |
| Reorganize files | mv |
| Move to another disk | mv |
| Backup (keep original) | cp |
| Duplicate | cp |
| Atomic change of name | mv |
Safety Flags
| Flag | When |
|---|---|
-i | Interactive use |
-n | Scripts (never overwrite) |
-u | Sync-like moves |
-f | Force in scripts that must overwrite |
The Alias Trap
| Context | Behavior |
|---|---|
Interactive shell with alias mv='mv -i' | Prompts |
| Script (aliases not loaded) | Overwrites silently |
| Bypass alias | \mv, command mv, /bin/mv |
Metadata on Move
| Case | Preserved |
|---|---|
| Same filesystem | All metadata (inode unchanged) |
| Cross filesystem | Depends on copy; may lose timestamps |
| Permissions | Preserved |
| Ownership | Preserved |
Interaction with Symlinks
| Operation | Behavior |
|---|---|
| Move symlink | The symlink moves |
| Move target | The symlink points to the new location (if path stays) |
mv -T | Treat destination as file, not dir |
Performance
| Case | Time |
|---|---|
| Small file same FS | Instant |
| Large file same FS | Instant |
| Large file cross FS | Proportional to size |
Verification
| Command | Shows |
|---|---|
ls | Files after move |
df PATH | Filesystem of a path |
find . -name X | Where a file ended up |
stat | Metadata of moved file |
cmp SRC DST | Compare after a cross-FS move |
Best Practices
โ Do This:
# Use mv for rename and move
mv old-name.txt new-name.txt # โ
# Move into a directory
mv file.txt /tmp/ # โ
# Use -i interactively
mv -i source.txt dest.txt # โ
# Use -n in scripts
mv -n source.txt dest.txt # โ
# Use -u for update-only moves
mv -u /src/* /dest/ # โ
# Rename directories with mv
mv old-folder new-folder # โ
# Verify the destination after
ls /tmp/file.txt # โ
# Check the filesystem for large moves
df /tmp # โ
# Move a large file quickly within the same FS
mv bigfile.bin backup/ # โ
# Move multiple files into a directory
mv *.log logs/ # โ
โ Don’t Do This:
# Don't mv when you meant cp
mv file.txt backup/ # โ ๏ธ original gone // โ ๏ธ
# Don't overwrite silently in scripts
mv source.txt dest.txt # โ ๏ธ overwrites without warning // โ ๏ธ
# Don't assume cross-FS moves are instant
mv huge.bin /mnt/external/ # โ ๏ธ takes minutes // โ ๏ธ
# Don't move into a name that's a file
mv file.txt existing-file.txt # โ ๏ธ overwrites existing-file // โ ๏ธ
# Don't interrupt a cross-FS move
# Ctrl+C mid-copy leaves a partial file // โ ๏ธ
# Don't try to replace a non-empty directory with mv
mv file.txt non-empty-dir/ # โ ๏ธ moves file into the dir // โ ๏ธ
# Don't forget the -r confusion
mv dir1 dir2 # โ
mv handles dirs without -r // โ
# (this is fine โ just noting that cp needs -r, mv doesn't)
# Don't rely on alias in scripts
mv old new # โ ๏ธ alias doesn't apply // โ ๏ธ
# Don't move files across filesystems casually
# Check df first // โ ๏ธ
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
Confusing mv with cp | Original gone | Confirm intent |
| Overwriting silently | Lost file | Use -i or -n |
| Cross-FS slow | Unexpected delay | Check df first |
| Interrupted cross-FS | Partial file | Use robust copy tools |
| Alias not in script | Overwrites | Use full path |
| Same file | same file error | Different name |
| Multiple sources to non-dir | Error | Make last a directory |
| Moving to a nonexistent parent | Error | mkdir -p first |
| Moving into itself | Nested directory | Use mv dir/* dest/ |
| No permission | Permission denied | Check target dir |
Real-World Examples
1. Rename a file
mv report.txt final-report.txt
2. Move to a directory
mv notes.txt ~/Documents/
3. Move and rename
mv report.txt /tmp/summary.txt
4. Move multiple files
mv *.log /var/log/archive/
5. Rename a directory
mv old-project new-project
6. Move a directory
mv project /opt/apps/
7. Interactive
mv -i source.txt dest.txt
8. No overwrite
mv -n source.txt dest.txt
9. Update-only
mv -u /newer/* /older/
10. Verbose
mv -v *.txt /tmp/
11. Same-filesystem rename
mv /home/alice/bigfile.bin /home/alice/backup.bin
Instant, no copy.
12. Cross-filesystem move
mv /home/alice/video.mp4 /mnt/external/video.mp4
Copies then deletes.
13. Move into an existing directory
mv project/ /opt/apps/
/opt/apps/project results.
14. Move directory contents
mv source-dir/* dest-dir/
Moves each item, not the dir itself.
15. Rename with extension change
mv script script.sh
16. Move with wildcard
mv *.txt docs/
17. Safe move in script
mv -n source.txt dest.txt || echo "not moved"
18. Move to a specific name with target directory
mv -t /tmp/ file.txt
Same as mv file.txt /tmp/.
19. Force overwrite in a script
mv -f source.txt dest.txt
20. Move across filesystems safely
rsync -av /home/alice/data /mnt/external/ && rm -rf /home/alice/data
More robust than mv for critical data.
Visual: What mv Does
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Before: โ
โ /home/alice/ โ
โ โโโ report.txt โ
โ โโโ backup/ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ mv report.txt backup/
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ After: โ
โ /home/alice/ โ
โ โโโ backup/ โ
โ โโโ report.txt โ
โ โ
โ Same filesystem โ instant โ
โ report.txt's inode unchanged โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Rename = Move
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Rename: โ
โ โ
โ /home/alice/old.txt โ
โ โ โ
โ โ mv old.txt new.txt โ
โ โผ โ
โ /home/alice/new.txt โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Move to a new name in a new directory: โ
โ โ
โ /home/alice/old.txt โ
โ โ โ
โ โ mv old.txt /tmp/new.txt โ
โ โผ โ
โ /tmp/new.txt โ
โ โ
โ Same operation โ the destination differs โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Same FS vs Cross FS
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Same filesystem โ
โ โ
โ /home/alice/file.bin โ
โ โ โ
โ โ mv /home/alice/file.bin /home/alice/backup/โ
โ โผ โ
โ /home/alice/backup/file.bin โ
โ โ
โ โข Rename only โ
โ โข Instant โ
โ โข No copy โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Different filesystems โ
โ โ
โ /home/alice/file.bin (sda) โ
โ โ โ
โ โ mv /home/alice/file.bin /mnt/external/โ
โ โผ โ
โ /mnt/external/file.bin (sdb) โ
โ โ
โ โข Copy then delete โ
โ โข Proportional to size โ
โ โข New inode at destination โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Destination Semantics
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv file.txt newname.txt โ
โ โ
โ โ file.txt renamed to ./newname.txt โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv file.txt /tmp/ โ
โ โ
โ โ file.txt moves to /tmp/file.txt โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv file.txt existing-file.txt โ
โ โ
โ โ existing-file.txt overwritten โ
โ โ unless -i or -n โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Directory Nesting Rule
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Case 1: Destination doesn't exist โ
โ โ
โ $ mv project /opt/apps โ
โ โ
โ /opt/apps/ โ
โ โโโ (project's contents) โ
โ โ
โ apps becomes the project โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Case 2: Destination exists as a directory โ
โ โ
โ $ mv project /opt/apps/ โ
โ โ
โ /opt/apps/ โ
โ โโโ project/ โ
โ โโโ (project's contents) โ
โ โ
โ project nested inside apps โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: -i vs -n vs -u
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Default: silent overwrite โ
โ โ
โ $ mv src dst โ
โ (dst replaced) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ -i: interactive โ
โ โ
โ $ mv -i src dst โ
โ mv: overwrite 'dst'? _ โ
โ โ
โ y โ overwrite โ
โ n โ skip (source stays) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ -n: no clobber โ
โ โ
โ $ mv -n src dst โ
โ (dst not overwritten, source stays) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ -u: update only โ
โ โ
โ $ mv -u src dst โ
โ If src newer than dst โ move โ
โ Otherwise โ skip โ
โ If dst missing โ move โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: mv vs cp โ Source Survival
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv source dest โ
โ โ
โ Before: โ
โ source dest โ
โ โ
โ After: โ
โ (gone) dest' โ
โ โ
โ Source is gone โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ cp source dest โ
โ โ
โ Before: โ
โ source dest โ
โ โ
โ After: โ
โ source dest' โ
โ โ
โ Source still exists โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Cross-Filesystem Detection
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ $ df /home/alice โ
โ Filesystem Mounted on โ
โ /dev/sda2 / โ
โ โ
โ $ df /mnt/external โ
โ Filesystem Mounted on โ
โ /dev/sdb1 /mnt/external โ
โ โ
โ Different "Mounted on" โ different FS โ
โ mv will copy โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Directory Move
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv old-dir /new-parent/ โ
โ โ
โ Before: โ
โ old-dir/ โ
โ โโโ a.txt โ
โ โโโ b.txt โ
โ โ
โ /new-parent/ โ
โ (empty) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ After: โ
โ /new-parent/ โ
โ โโโ old-dir/ โ
โ โโโ a.txt โ
โ โโโ b.txt โ
โ โ
โ The whole tree moved โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Visual: Decision Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Rename or move a file? โ
โ โโโ mv โ
โ โ
โ Keep the original? โ
โ โโโ cp, not mv โ
โ โ
โ Destination exists as a file? โ
โ โ โ
โ โโโ Overwrite safely โโโบ mv -i โ
โ โ โ
โ โโโ Never overwrite โโโบ mv -n โ
โ โ
โ Only if newer? โ
โ โโโ mv -u โ
โ โ
โ Large file? โ
โ โ โ
โ โโโ Same FS โโโบ instant โ
โ โ โ
โ โโโ Cross FS โโโบ copy + delete โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Concept | Meaning |
|---|---|
mv | Move or rename |
| Same-FS move | Renames โ instant |
| Cross-FS move | Copies then deletes |
| Rename | mv old new |
| Move | mv file /dir/ |
-i | Interactive |
-n | No overwrite |
-u | Update only |
| Source after | Gone |
Key takeaways:
mvmoves or renames โ the same operation- Renaming is a move to a new name in the same directory
- Destination is a directory โ source moves into it, keeping its name
- Destination is a name โ source is renamed to that name; existing files are overwritten
- Same-filesystem moves are instant โ only the directory entry changes
- Cross-filesystem moves copy then delete โ proportional to size
- No
-rneeded for directories โmvhandles them by default -iprompts before overwrite;-nnever overwrites;-umoves only if newermvremoves the source โ usecpif you need to keep it- Aliases only affect interactive shells โ scripts see plain
mv - Interrupted cross-FS moves leave partial copies
mvpreserves metadata on the same filesystem (inode unchanged)- Check
dfbefore large moves to know if they’ll be instant
Remember: mv is the move-and-rename command. Same filesystem? Instant, because only the directory entry changes. Cross filesystem? A copy then delete. Use -i interactively, -n in scripts. Remember the destination rules โ directory means “into,” a name means “rename.” And remember that mv deletes the source โ if you want to keep the original, use cp. That’s the whole command: one operation, two names, one move.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!