mv (move or rename files or directories)
The mv command is a versatile utility used for both moving files and directories from one location to another and for renaming them. Unlike a copy operation, a move means the source file or directory is removed from its original location.
Example 1: Rename a file
# Rename the file 'temp.txt' to 'final.txt' in the current directory
mv temp.txt final.txt
Explanation: When the source and destination are in the same directory, mv simply renames the file. The file's inode (the underlying data structure) remains the same; only its name in the directory listing changes.
Example 2: Move a file to a different directory
# Move the file 'report.docx' from the current directory into the 'documents' directory
mv report.docx documents/
Explanation: This command moves report.docx to the documents directory. After the command, report.docx will no longer be in the current directory but will exist in documents.
Example 3: Move a file and rename it simultaneously
# Move 'data.csv' to the 'archive' directory and rename it to 'data_2025.csv'
mv data.csv archive/data_2025.csv
Explanation: By specifying a new name as part of the destination path, mv performs both a move and a rename in a single operation.
Example 4: Move an entire directory
# Move the 'project_alpha' directory into the 'archive' directory
mv project_alpha archive/
Explanation: The mv command can move entire directories and their contents. The project_alpha directory and everything inside it will now be located at archive/project_alpha.
Example 5: Interactive move to prevent overwrites
# Move 'settings.conf', but ask for confirmation before overwriting an existing file
mv -i settings.conf /etc/
Explanation: The -i (interactive) flag provides a safety check. If a file named settings.conf already exists in /etc/, the command will prompt you to confirm the overwrite before proceeding.
Example 6: Move with verbose output
# Move 'image1.jpg' and 'image2.png' to the 'gallery' directory and print the action
mv -v image1.jpg image2.png gallery/
Explanation: The -v (verbose) option causes mv to print a description of what is being done, explicitly stating the source and destination for each move.
Example 7: Do not overwrite an existing file
# Attempt to move 'config.xml', but do not overwrite the destination if it already exists
mv -n config.xml /deploy/
Explanation: The -n (no-clobber) option prevents mv from overwriting any existing file at the destination. If /deploy/config.xml already exists, the command will silently do nothing.