How to Move and Rename Files with the Linux mv Command
The mv command moves files from one place to another, and renaming is just moving a file to a new name. mv old.txt new.txt renames it, and mv file.txt backup/ moves it into a folder. This guide covers both uses and the safety flags.
Rename a file
Rename is a move where the destination is a new file name in the same folder.
Move a file into a folder
When the destination is an existing directory, mv places the file inside it, keeping the name.
Move and rename in one step
Move into a folder and give the file a new name at the same time by writing both in the destination.
Avoid overwrites with -i
mv silently replaces a file with the same name. Add -i to be asked before overwriting, or -n to skip existing files entirely.
Move a whole directory
mv works on folders with no extra flags. mv project/ /var/www/ moves the entire tree. Add -v to see each move as it happens. See mv for the full reference.
Keep learning
Related guides: How to Copy Files and Directories with the Linux cp Command · The Linux ls Command: List Files Like a Pro · How to Find Files and Directories with the Linux find Command. Read all tutorials on the Learn Linux blog.