🚚 Command: mv (Move / Rename)
The mv command serves a dual purpose in Linux: it moves files from one location to another, and it is also the standard way to rename files and directories.
1. The "Why"
In a graphical interface, you might drag and drop a file to move it, or right-click to rename it. In the terminal, mv handles both. It is essential for:
- Organizing your project files (moving
.javafiles into asrcfolder). - Correcting typos in filenames.
- Moving completed drafts of your e-book into a "Finished" directory.
- Deploying configuration files to their proper system paths.
2. Basic Syntax
mv [OPTIONS] SOURCE DESTINATION
- To Rename: Keep the location the same but change the name.
- To Move: Change the location (and optionally the name).
3. Most Useful Flags
| Flag | Name | What it does |
|---|---|---|
-i |
Interactive | Prompts you before overwriting an existing file at the destination. |
-n |
No-Clobber | Prevents overwriting any existing file (it just won't move it). |
-v |
Verbose | Shows a message for every file moved (e.g., 'a' -> 'b'). |
-u |
Update | Only moves the file if the source is newer than the destination. |
4. Practical Examples
A. Renaming a File
If you want to rename draft.tex to final_version.tex:
mv draft.tex final_version.tex
B. Moving a File to a Different Folder
To move a photo into your Pictures folder:
mv screenshot.png ~/Pictures/
C. Moving and Renaming at the Same Time
You can move a file and give it a new name at its destination:
mv temp_log.txt ~/logs/system_oct_25.txt
D. Moving an Entire Directory
Unlike cp or rm, you do not need a -r flag to move a folder. mv handles directories by default:
mv old_project_folder/ archive/
5. Pro-Tips
- Safety First: Just like
cp,mvwill silently overwrite files at the destination. Usemv -iif you want Bash to ask you "Overwrite?" before it acts. - Bulk Moving: You can move multiple files into a folder at once:
mv file1.java file2.java file3.java src/ - Hidden Files: To rename a file so it becomes hidden, just add a dot to the start of the name:
mv config_file .config_file - Arch Linux Context: You'll use this often when moving downloaded custom scripts to your
/usr/local/binor moving configuration files into your.configdirectory.
6. Summary Reference
| Goal | Command |
|---|---|
| Rename a file | mv old_name new_name |
| Move a file to a folder | mv file.txt /path/to/folder/ |
| Move and rename | mv file.txt /path/to/folder/new_name.txt |
| Move multiple files | mv *.txt folder/ |
| Safe move (asks first) | mv -i source destination |