0
0
Linux CLIscripting~5 mins

mv (move and rename) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to organize your files by moving them to different folders or changing their names. The mv command helps you do both quickly in the terminal.
When you want to move a photo from your Downloads folder to your Pictures folder.
When you need to rename a document to a clearer name without opening any program.
When organizing project files into different folders to keep things tidy.
When replacing an old version of a file with a new one by moving it over.
When cleaning up your desktop by moving files into categorized folders.
Commands
This command moves the file named report.txt from the current folder into the Documents folder. It helps organize files by placing them where they belong.
Terminal
mv report.txt Documents/
Expected OutputExpected
No output (command runs silently)
This command renames the file oldname.txt to newname.txt in the same folder. It changes the file's name without moving it.
Terminal
mv oldname.txt newname.txt
Expected OutputExpected
No output (command runs silently)
This moves photo.jpg into the Pictures folder but asks for confirmation if a file with the same name already exists there, preventing accidental overwrites.
Terminal
mv -i photo.jpg Pictures/
Expected OutputExpected
mv: overwrite 'Pictures/photo.jpg'?
-i - Prompt before overwriting files
Moves notes.txt into the Archive folder and shows a message about what it did, so you know the command worked.
Terminal
mv -v notes.txt Archive/
Expected OutputExpected
'notes.txt' -> 'Archive/notes.txt'
-v - Show what is being done
Key Concept

If you remember nothing else from mv, remember: it moves files or renames them by changing their location or name in one simple command.

Common Mistakes
Trying to move a file to a folder that does not exist.
The command fails because the destination folder must exist before moving files into it.
Create the destination folder first using mkdir, then run the mv command.
Using mv without the -i flag and accidentally overwriting important files.
mv will overwrite files without warning, causing data loss.
Use mv -i to get a prompt before overwriting files.
Trying to rename a file by moving it to a new folder without specifying the new filename.
If the destination is a folder, the file is moved there but keeps its original name.
Specify the full new path including the new filename to rename while moving.
Summary
Use mv to move files between folders or rename them by changing their path or filename.
Add -i to ask before overwriting files and -v to see what mv is doing.
Make sure the destination folder exists before moving files into it.