0
0
Linux-cliHow-ToBeginner ยท 3 min read

How to Move a File in Linux: Simple mv Command Guide

To move a file in Linux, use the mv command followed by the source file and the destination path. For example, mv file.txt /home/user/Documents/ moves file.txt to the Documents folder.
๐Ÿ“

Syntax

The basic syntax of the mv command is:

  • mv [options] source destination

Here, source is the file or directory you want to move, and destination is where you want to move it to. The destination can be a directory or a new filename.

bash
mv source_file destination_path
๐Ÿ’ป

Example

This example moves a file named notes.txt from the current directory to the Documents folder.

bash
mv notes.txt Documents/
โš ๏ธ

Common Pitfalls

One common mistake is forgetting that mv will overwrite files in the destination without warning. To avoid accidental overwrites, use the -i option which asks for confirmation before overwriting.

Another mistake is specifying the wrong destination path, which can cause the file to be moved to an unexpected location or renamed unintentionally.

bash
mv notes.txt Documents/
# Wrong: overwrites without warning

mv -i notes.txt Documents/
# Right: asks before overwriting
๐Ÿ“Š

Quick Reference

OptionDescription
-iPrompt before overwrite
-vShow files being moved
-nDo not overwrite existing files
--backupMake backup of each existing destination file
โœ…

Key Takeaways

Use the mv command with source and destination to move files in Linux.
Add the -i option to prevent accidental overwriting by prompting before overwrite.
The destination can be a directory or a new filename to rename the file.
Check the destination path carefully to avoid moving files to wrong locations.