| |

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

OptionEffect
mv src destMove or rename
mv src dir/Move into directory
mv -iPrompt on overwrite
mv -nDo not overwrite
mv -fForce overwrite
mv -vVerbose
mv -uMove if newer
mv -bBackup destination
mv -t dir srcDestination first
mv -TTreat dest as normal

Backup Options

OptionEffect
-bBackup with ~ suffix
-S .bakCustom suffix
--backup=numberedNumbered backups

Common Patterns

PatternPurpose
mv file.txt file.txt.bakBackup a file
mv *.txt /tmp/Move all text files
mv dir /tmp/Move a directory
mv dir newnameRename a directory
mv -i * /tmp/Prompt on each
mv -u * /tmp/Incremental move

Error Cases

ErrorCause
cannot statSource does not exist
not a directoryDestination is not a directory
cannot move to a subdirectory of itselfDirectory loop
Permission deniedNo write access
overwriteDestination 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

PitfallWhy It HappensFix
Overwrite without promptDefault behaviorUse -i or -n
Directory loopMove into subdirectoryUse a different destination
Slow cross-filesystem moveCopy + removeExpected, use cp then rm
Hidden files missedGlob does not matchUse .* or find
Alias prompts in the scriptThe alias is activeUse -f or \mv
Permission deniedNo write accessCheck the permissions
Source still in useThe process holds the fileCheck 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

ItemValue
Basicmv source dest
Renamemv old new
Move to directorymv file /dir/
Directory movemv dir /target/
Overwrite defaultYes
Prompt-i
No overwrite-n
Force-f
Verbose-v
Backup-b
Destination first-t
Incremental-u
Recursive flagNot needed
Atomic within FSYes
Atomic across FSNo

Key takeaways:

  • The mv moves or renames in one operation. On a Unix system, the move and the rename are the same: the directory entry changes. The mv old new renames, and the mv file /dir/ moves into the directory.
  • The directory moves with its contents by default. The cp requires the -r for the directory, but the mv does not. The directory move is the rename of the directory entry, and the contents are unaffected.
  • The mv overwrites the destination by default. The -i prompts, the -n skips, and the -b backs up. The alias mv='mv -i' is common on the interactive systems, and the -f overrides 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 mv does not move a directory into its own subdirectory. The error prevents the loop. Use a different destination or the cp and the rm.
  • The hidden files are not matched by the * glob. The .* or the find is the tool for the hidden files.
  • The -t option specifies the destination first. The -t form 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!