The Unix and macOS terminal
gives you direct control over files and directories. One of the most useful commands for everyday file management is mv.
mv stands for move, but it performs two closely related jobs:
Moving files or directories to another location
Renaming files or directories
Its basic syntax is:
mv SOURCE DESTINATIONCheck Your Current Location
Before moving files, it is useful to know which directory you are working in.
pwdExample output:
/Users/walter/git-5/terminal-thingspwd means print working directory. It shows your current location in the file system.
You can then check what files are available with:
lsFor more detail:
ls -lThis is a good habit before making changes to files.
Creating Some Example Files
You can create empty files with touch:
touch file-1.txt
touch file-2.txt
touch file-33.txtNow the directory contains:
file-1.txt
file-2.txt
file-33.txtRenaming a File with mv
Suppose file-33.txt should actually be named file-3.txt.
Run:
mv file-33.txt file-3.txtAfterward:
file-1.txt
file-2.txt
file-3.txtBecause the source and destination are in the same directory, mv effectively renames the file.
The general pattern is:
mv old-name.txt new-name.txtWhy This Command Fails
Consider:
mv file file-33.txt file-3.txtWhen mv receives more than one source, the final argument must be a directory.
The command is interpreted as:
mv SOURCE1 SOURCE2 DIRECTORYSo mv expects file-3.txt to be a directory where both file and file-33.txt should be moved.
If it is not a directory, you may see an error such as:
mv: file-3.txt is not a directoryFor a simple rename, use only one source and one destination:
mv file-33.txt file-3.txtMoving a File into a Directory
Create a directory:
mkdir archiveThen move a file into it:
mv file-3.txt archive/The file is now located at:
archive/file-3.txtYou can also move and rename the file at the same time:
mv file-3.txt archive/important-file.txtNow its new path is:
archive/important-file.txtMoving Multiple Files
You can move several files at once:
mv file-1.txt file-2.txt archive/Here, archive/ must be a directory.
Both files will be moved into it.
Be Careful When the Destination Already Exists
Consider:
mv file-3.txt file-2.txtIf file-2.txt already exists as a regular file, mv may replace it.
After the operation, the path:
file-2.txtwill refer to the file that was previously named file-3.txt.
The original file-2.txt may be lost unless it was backed up elsewhere.
For safer use, add the -i option:
mv -i file-3.txt file-2.txtIf the destination already exists, mv asks before replacing it.
You can also use:
mv -n file-3.txt file-2.txt-n prevents an existing destination from being overwritten on systems that support the option, including common macOS and GNU implementations.
Useful mv Commands
Rename a file:
mv old.txt new.txtMove a file:
mv report.txt documents/Move several files:
mv file-1.txt file-2.txt archive/Move and rename:
mv draft.txt published/article.txtRename or move a directory:
mv old-folder new-folderAsk before overwriting:
mv -i source.txt destination.txtPrevent overwriting:
mv -n source.txt destination.txtVerify the Result
After using mv, check the directory:
lsOr inspect a particular file:
ls -l file-3.txtA simple and careful workflow is:
pwd
ls
mv -i file-33.txt file-3.txt
lsThis confirms where you are, shows the current files, performs the rename, and verifies the result.
Using mv in Real Work
mv becomes especially useful when combined with other shell commands.
For example:
mv *.txt text-files/This moves matching .txt files into the text-files directory.
Another example:
mv build/output.log logs/latest.logThis moves the generated log file and gives it a more useful name at the same time.
Because terminal commands can be placed inside shell scripts, mv is commonly used in development, deployment, backups, log management, and automated file-processing workflows.
One technical detail is worth knowing: when the source and destination are on the same file system, moving a file can often be completed by updating file-system metadata rather than copying all of the file's data. If the destination is on another file system, the operating system may need to copy the data and then remove the original.
Final Takeaway
The main rule is simple:
mv SOURCE DESTINATIONWith one source, the destination can be a new file name or another location:
mv old.txt new.txtmv report.txt documents/With multiple sources, the final argument must be a directory:
mv file-1.txt file-2.txt archive/And when there is a risk of replacing an existing file, use:
mv -i source.txt destination.txtmv is a small command, but understanding it properly gives you a fast and reliable way to rename, organize, and automate file management directly from the terminal.