How to Move a File in Bash: Simple mv Command Guide
To move a file in bash, use the
mv command followed by the source file and the destination path. For example, mv file.txt /path/to/destination/ moves file.txt to the specified folder.Syntax
The basic syntax of the mv command is:
mv [options] source destination
source is the file or directory you want to move.
destination is the new location or new name for the file.
Options can modify behavior, like -i to ask before overwriting.
bash
mv source_file destination_path
Example
This example moves a file named notes.txt from the current directory to a folder called backup.
bash
mkdir -p backup mv notes.txt backup/
Common Pitfalls
One common mistake is forgetting that mv will overwrite files without warning. Use mv -i to be prompted before overwriting.
Another issue is specifying the wrong destination path, which can rename the file instead of moving it to a folder.
bash
mv notes.txt backup # This renames notes.txt to 'backup' if 'backup' is not a directory mv -i notes.txt backup/ # This moves notes.txt into the backup folder with confirmation if overwriting
Quick Reference
| Command | Description |
|---|---|
| mv source destination | Move or rename a file or directory |
| mv -i source destination | Move with prompt before overwrite |
| mv -v source destination | Show details of the move operation |
| mv -n source destination | Do not overwrite existing files |
Key Takeaways
Use
mv source destination to move or rename files in bash.Add
-i option to avoid accidental overwrites by prompting before replacing files.Ensure the destination path is correct to avoid renaming files unintentionally.
Use
mv -v to see what the command is doing during the move.Directories can also be moved with
mv, not just files.