LFCS 11 ๐ง Moving and Renaming โ mv
The mv command moves a file or directory from one location to another. It is also the command for renaming. On a Unix system, moving and renaming are the same operation: the file’s name changes, or its directory entry changes, or both. The mv command is the tool, and it is one of the most frequently used commands in the shell. The LFCS exam expects it to be automatic.
Key point: mv source dest moves or renames. If dest is an existing directory, the source is moved into it with the same name. If dest is a path that does not exist, the source is renamed to that path. The command overwrites the destination if it exists, unless -i or -n is used. The -i prompts, the -n skips, and the -v shows what happened. Unlike cp, the mv does not need a recursive flag; a directory moves with its contents by default.
Why mv matters
The rename problem. A file named draft.txt becomes final.txt. The content does not change; the name does. The mv draft.txt final.txt is the operation. The same command renames a directory.
The move problem. A file in the home directory belongs in /srv/data. The mv ~/file.txt /srv/data/ moves it. The file’s name is the same; the directory changes.
The overwrite problem. The mv overwrites the destination by default. On a production system, this can destroy data. The -i option prompts before the overwrite, the -n option skips the existing files, and the alias mv='mv -i' is common on the interactive systems.
The atomicity problem. Within a single filesystem, the mv is an atomic rename. The file’s inode does not change, the data is not copied, and the operation is instantaneous. Across filesystems, the mv copies the file and removes the original. The difference matters for the running processes: a process that holds the file open keeps the inode when the move is within the filesystem, but the cross-filesystem move creates a new file.
The exam problem. The LFCS tasks often require moving a file to a specific location or renaming it. The mv is the command. The options and the overwrite behavior are the details that decide whether the task succeeds.
a. The basic move and rename
The mv takes a source and a destination.
$ mv source.txt dest.txt
The source.txt is renamed to dest.txt. The content is unchanged. The source no longer exists under the old name.
If the destination is a directory, the source is moved into it.
$ mv file.txt /tmp/
$ ls /tmp/file.txt
/tmp/file.txt
The file.txt is moved to /tmp/. The name is unchanged. The source is no longer in the current directory.
If the destination is a directory and the file already exists there, the mv overwrites it.
$ mv file.txt /tmp/
$ mv file.txt /tmp/
# no error, the file is overwritten
The -i option prompts before the overwrite.
$ mv -i file.txt /tmp/
mv: overwrite '/tmp/file.txt'? y
The -n option skips the existing files without prompting.
$ mv -n file.txt /tmp/
# no prompt, no overwrite
The -v option prints each move.
$ mv -v file.txt /tmp/
renamed 'file.txt' -> '/tmp/file.txt'
Multiple sources can be moved to a directory.
$ mv file1.txt file2.txt file3.txt /tmp/
The last argument is the destination directory. The preceding arguments are the sources. If the destination is not a directory, the command fails with the “target is not a directory” error.
The -t option specifies the destination first.
$ mv -t /tmp/ file1.txt file2.txt file3.txt
The -t form is useful when the source list is generated by a glob or a command.
A glob can be used as the source.
$ mv *.txt /tmp/
The shell expands the glob to the matching files, and the mv moves each. The hidden files are not matched by *. Use .* or find for the hidden files.
$ mv .*.txt /tmp/ # hidden .txt files
b. The directory move
The mv moves a directory and its contents by default. The recursive flag is not needed.
$ mv projects /tmp/
$ ls /tmp/projects
The projects directory is moved to /tmp/, and the contents move with it. The cp command requires the -r for the directory, but the mv does not. The move is the rename of the directory entry, and the contents are unaffected.
If the destination is an existing directory, the source directory is moved into it.
$ mv projects /tmp/backup/
$ ls /tmp/backup/projects
The projects is moved inside the /tmp/backup/ directory. The result is /tmp/backup/projects.
If the destination is a path that does not exist, the source directory is renamed.
$ mv projects archive
$ ls archive
The projects is renamed to archive. The contents are unchanged.
The mv of the directory across the filesystems copies the contents and removes the source. The operation is slower than the within-filesystem rename.
$ mv /home/user/projects /mnt/external/projects
The /home/user/projects is on one filesystem, and the /mnt/external/ is on another. The mv copies each file and removes the original. The inode changes, and the running processes that hold the files open are affected.
The mv of the directory into its own subdirectory fails.
$ mv projects projects/subdir
mv: cannot move 'projects' to a subdirectory of itself, 'projects/subdir'
The error prevents the recursive move that would loop. The fix is to use the different destination or the cp and the rm.
The -T option treats the destination as a normal file or directory, not as a directory to move into.
$ mv -T source dest
The -T is for the cases where the destination is the directory and the source should replace it, not be moved into it.
c. The overwrite behavior and the backups
The mv overwrites the destination by default.
$ mv source.txt dest.txt
# dest.txt is overwritten
The -i option prompts.
$ mv -i source.txt dest.txt
mv: overwrite 'dest.txt'? y
The -n option does not overwrite.
$ mv -n source.txt dest.txt
# no overwrite
The -b option makes a backup of the destination before the overwrite.
$ mv -b source.txt dest.txt
# dest.txt~ is created with the old dest.txt
The -S option sets the backup suffix.
$ mv -b -S .bak source.txt dest.txt
# dest.txt.bak is created
The --backup=numbered makes the numbered backups.
$ mv --backup=numbered source.txt dest.txt
# dest.txt.~1~ is created
The -f option forces the overwrite without the prompt. It overrides the -i from the alias.
$ mv -f source.txt dest.txt
# no prompt, overwrite
The -u option moves only if the source is newer than the destination.
$ mv -u source.txt /tmp/
The -u is for the incremental updates where only the changed files should be moved.
The alias mv='mv -i' is common on the interactive systems. The alias makes the mv prompt before the overwrite. The -f option overrides the alias when the script needs the forced overwrite.
$ alias mv
alias mv='mv -i'
$ mv -f source.txt dest.txt # no prompt
The --strip-trailing-slashes removes the trailing slashes from the source arguments.
$ mv --strip-trailing-slashes source/ dest
The option is for the compatibility with the systems that treat the trailing slash differently.
The -Z option sets the SELinux security context of the destination to the default type.
$ mv -Z source.txt /srv/data/
The SELinux is the security module on the RHEL-family distributions. The -Z is for the cases where the context matters.
Complete Example Session
# ============================================
# PART 1: BASIC RENAME
# ============================================
mv draft.txt final.txt
# ============================================
# PART 2: MOVE TO DIRECTORY
# ============================================
mv file.txt /tmp/
ls /tmp/file.txt
# ============================================
# PART 3: OVERWRITE PROMPT
# ============================================
mv -i file.txt /tmp/
# mv: overwrite '/tmp/file.txt'?
# ============================================
# PART 4: NO OVERWRITE
# ============================================
mv -n file.txt /tmp/
# ============================================
# PART 5: VERBOSE
# ============================================
mv -v file.txt /tmp/
# renamed 'file.txt' -> '/tmp/file.txt'
# ============================================
# PART 6: MULTIPLE SOURCES
# ============================================
mv file1.txt file2.txt file3.txt /tmp/
# ============================================
# PART 7: DIRECTORY MOVE
# ============================================
mv projects /tmp/
ls /tmp/projects
# ============================================
# PART 8: DIRECTORY RENAME
# ============================================
mv projects archive
ls archive
# ============================================
# PART 9: BACKUP
# ============================================
mv -b source.txt dest.txt
# dest.txt~ is created
# ============================================
# PART 10: FORCE OVERWRITE
# ============================================
mv -f source.txt dest.txt
The ten parts covered the basic rename, the move to a directory, the overwrite prompt, the -n, the verbose, the multiple sources, the directory move, the directory rename, the backup, and the force overwrite.
Quick Reference
mv Options
| Option | Effect |
|---|---|
mv src dest | Move or rename |
mv src dir/ | Move into directory |
mv -i | Prompt on overwrite |
mv -n | Do not overwrite |
mv -f | Force overwrite |
mv -v | Verbose |
mv -u | Move if newer |
mv -b | Backup destination |
mv -t dir src | Destination first |
mv -T | Treat dest as normal |
Backup Options
| Option | Effect |
|---|---|
-b | Backup with ~ suffix |
-S .bak | Custom suffix |
--backup=numbered | Numbered backups |
Common Patterns
| Pattern | Purpose |
|---|---|
mv file.txt file.txt.bak | Backup a file |
mv *.txt /tmp/ | Move all text files |
mv dir /tmp/ | Move a directory |
mv dir newname | Rename a directory |
mv -i * /tmp/ | Prompt on each |
mv -u * /tmp/ | Incremental move |
Error Cases
| Error | Cause |
|---|---|
cannot stat | Source does not exist |
not a directory | Destination is not a directory |
cannot move to a subdirectory of itself | Directory loop |
Permission denied | No write access |
overwrite | Destination exists (with -i) |
Best Practices
โ Do This:
# Rename a file
mv draft.txt final.txt # โ
# Move to a directory
mv file.txt /tmp/ # โ
# Use -i in the interactive shells
alias mv='mv -i' # โ
# Use -n in the scripts to avoid the overwrite
mv -n file.txt /tmp/ # โ
# Backup before the overwrite
mv -b important.txt important.txt.bak # โ
# Use the verbose for the confirmation
mv -v file.txt /tmp/ # โ
# Use the glob for the multiple files
mv *.txt /tmp/ # โ
โ Don’t Do This:
# Don't mv a directory into its own subdirectory
mv projects projects/subdir # error # โ
# Don't overwrite without checking
mv file.txt /etc/ # may overwrite # โ ๏ธ
# Don't forget the -f to override the alias
mv source.txt dest.txt # with the alias, prompts # โ ๏ธ
# Don't expect the hidden files to be matched by *
mv * /tmp/ # misses the .hidden files # โ ๏ธ
# Don't move across the filesystems without the awareness
mv /home/user/big /mnt/external/ # copies, slow # โ ๏ธ
# Don't use the trailing slash inconsistently
mv source/ dest # may fail on the some systems # โ ๏ธ
# Don't assume the atomicity across the filesystems
mv /fs1/file /fs2/file # not atomic # โ ๏ธ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Overwrite without prompt | Default behavior | Use -i or -n |
| Directory loop | Move into subdirectory | Use a different destination |
| Slow cross-filesystem move | Copy + remove | Expected, use cp then rm |
| Hidden files missed | Glob does not match | Use .* or find |
| Alias prompts in the script | The alias is active | Use -f or \mv |
| Permission denied | No write access | Check the permissions |
| Source still in use | The process holds the file | Check with lsof |
Real-World Examples
1. Rename
mv old.txt new.txt
2. Move to Directory
mv file.txt /srv/data/
3. Rename Directory
mv olddir newdir
4. Backup
mv -b config.yml config.yml.bak
5. Move Multiple
mv *.log /var/log/archive/
6. Incremental Move
mv -u *.txt /tmp/
7. Prompt on Overwrite
mv -i file.txt /tmp/
8. Force Move
mv -f file.txt /tmp/
9. Move with Verbose
mv -v file.txt /tmp/
10. Move to the Destination First
mv -t /tmp/ file1.txt file2.txt
Visual
Move vs Rename
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ RENAME โ
โ โ
โ mv draft.txt final.txt โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ โdraft.txtโ โโโโโโโถ โfinal.txtโ โ
โ โโโโโโโโโโโ โโโโโโโโโโโ โ
โ Same directory, new name. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ MOVE โ
โ โ
โ mv file.txt /tmp/ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โfile.txt โ โโโโโโโถ โ/tmp/file.txtโ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ New directory, same name. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Within vs Across Filesystems
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WITHIN THE SAME FILESYSTEM โ
โ โ
โ mv /home/user/file /home/user/other/ โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The inode does not change. โ โ
โ โ The directory entry is renamed. โ โ
โ โ Atomic, instantaneous. โ โ
โ โ The running processes keep the file. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ ACROSS THE FILESYSTEMS โ
โ โ
โ mv /home/user/file /mnt/external/file โ
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ The file is copied to the new location. โ โ
โ โ The original is removed. โ โ
โ โ The inode changes. โ โ
โ โ Slow for the large files. โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Overwrite Behavior
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DEFAULT โ
โ โ
โ mv source.txt dest.txt โ
โ โ dest.txt is overwritten โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ -i PROMPT โ
โ โ
โ mv -i source.txt dest.txt โ
โ โ mv: overwrite 'dest.txt'? โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ -n NO OVERWRITE โ
โ โ
โ mv -n source.txt dest.txt โ
โ โ no overwrite, no prompt โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ -b BACKUP โ
โ โ
โ mv -b source.txt dest.txt โ
โ โ dest.txt~ is created, then dest.txt is overwritten โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Directory Move
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mv projects /tmp/ โ
โ โ
โ Before: โ
โ ~/projects/ โ
โ โโโ file1 โ
โ โโโ file2 โ
โ โ
โ After: โ
โ /tmp/projects/ โ
โ โโโ file1 โ
โ โโโ file2 โ
โ โ
โ The directory and the contents move. โ
โ No recursive flag is needed. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Basic | mv source dest |
| Rename | mv old new |
| Move to directory | mv file /dir/ |
| Directory move | mv dir /target/ |
| Overwrite default | Yes |
| Prompt | -i |
| No overwrite | -n |
| Force | -f |
| Verbose | -v |
| Backup | -b |
| Destination first | -t |
| Incremental | -u |
| Recursive flag | Not needed |
| Atomic within FS | Yes |
| Atomic across FS | No |
Key takeaways:
- The
mvmoves or renames in one operation. On a Unix system, the move and the rename are the same: the directory entry changes. Themv old newrenames, and themv file /dir/moves into the directory. - The directory moves with its contents by default. The
cprequires the-rfor the directory, but themvdoes not. The directory move is the rename of the directory entry, and the contents are unaffected. - The
mvoverwrites the destination by default. The-iprompts, the-nskips, and the-bbacks up. Thealias mv='mv -i'is common on the interactive systems, and the-foverrides the alias. - The within-filesystem move is atomic. The inode does not change, and the operation is instantaneous. The across-filesystem move copies and removes. The difference matters for the running processes and the performance.
- The
mvdoes not move a directory into its own subdirectory. The error prevents the loop. Use a different destination or thecpand therm. - The hidden files are not matched by the
*glob. The.*or thefindis the tool for the hidden files. - The
-toption specifies the destination first. The-tform is useful when the source list is generated by a glob or a command.
Remember: The mv is the move and the rename. It is the command the LFCS tasks use for the file relocation and the renaming. The options decide the overwrite behavior, and the overwrite behavior decides whether the task succeeds. Use -i in the interactive shells, -n in the scripts, and -f to override the alias. Use the -b for the backups, and the -v for the confirmation. And remember the within-filesystem atomicity: the move is free until the filesystems differ.
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!