rimubhai_
PortfolioBlogsComponentsAbout

© 2026 rimubhai_ All rights reserved.

AboutGalleryContactPrivacy
BlogTerminal
Terminal

Mastering mv: Move and Rename Files from the Terminal

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…

rimubhai
Oct 2, 20264 min read12 sections

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 DESTINATION

Check Your Current Location

Before moving files, it is useful to know which directory you are working in.

pwd

Example output:

/Users/walter/git-5/terminal-things

pwd means print working directory. It shows your current location in the file system.

You can then check what files are available with:

ls

For more detail:

ls -l

This 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.txt

Now the directory contains:

file-1.txt
file-2.txt
file-33.txt

Renaming a File with mv

Suppose file-33.txt should actually be named file-3.txt.

Run:

mv file-33.txt file-3.txt

Afterward:

file-1.txt
file-2.txt
file-3.txt

Because the source and destination are in the same directory, mv effectively renames the file.

The general pattern is:

mv old-name.txt new-name.txt

Why This Command Fails

Consider:

mv file file-33.txt file-3.txt

When mv receives more than one source, the final argument must be a directory.

The command is interpreted as:

mv SOURCE1 SOURCE2 DIRECTORY

So 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 directory

For a simple rename, use only one source and one destination:

mv file-33.txt file-3.txt

Moving a File into a Directory

Create a directory:

mkdir archive

Then move a file into it:

mv file-3.txt archive/

The file is now located at:

archive/file-3.txt

You can also move and rename the file at the same time:

mv file-3.txt archive/important-file.txt

Now its new path is:

archive/important-file.txt

Moving 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.txt

If file-2.txt already exists as a regular file, mv may replace it.

After the operation, the path:

file-2.txt

will 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.txt

If 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.txt

Move 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.txt

Rename or move a directory:

mv old-folder new-folder

Ask before overwriting:

mv -i source.txt destination.txt

Prevent overwriting:

mv -n source.txt destination.txt

Verify the Result

After using mv, check the directory:

ls

Or inspect a particular file:

ls -l file-3.txt

A simple and careful workflow is:

pwd
ls
mv -i file-33.txt file-3.txt
ls

This 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.log

This 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 DESTINATION

With one source, the destination can be a new file name or another location:

mv old.txt new.txt
mv 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.txt

mv 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.

On this page

The Unix and macOS terminalCheck Your Current LocationCreating Some Example FilesRenaming a File with mvWhy This Command FailsMoving a File into a DirectoryMoving Multiple FilesBe Careful When the Destination Already ExistsUseful mv CommandsVerify the ResultUsing mv in Real WorkFinal Takeaway
0% read
All articlesFiled under Terminal

More in Terminal

Mastering cp: Copy Files and Folders from the Terminal

5 min read