Cantech Knowledge Base

Your Go-To Hosting Resource

How to Move Files in Linux? (mv Command)

Introduction

Linux has a very useful command known as mv. It helps you move files and directories from one place to another. You can also use it to rename files and folders easily. Thus, mv is a must-know command for organizing files or making quick changes in Linux.

In this article, let’s understand how to use the mv command effectively for moving and renaming files in Linux.

Understanding the mv Command

The mv command has a simple structure –

mv [options] source destination

Here:

  • source means the file/directory to move.
  • destination is the new location or new name of the file.

Now, let us see how you can use the mv command in different ways.

Prerequisites

Before using the mv command, ensure you meet these prerequisites:

  • You must be working in a Linux system (desktop or server) or connected via SSH to one.

    Example: ssh username@your_server_ip
  • You must have write permission on the source directory (to remove or rename the file) and on the target directory (to add the moved item). Check permissions using:
ls -l
  • If you don’t have permission, you may need to use sudo: 
sudo mv file.txt /usr/local/bin/
  • You should know how to use the terminal and navigate directories:
cd /path/to/directory
ls
  • The source file or directory must exist, or mv will give an error.

Check if it exists with:

  • If the source and destination reside on different file systems, mv acts by copying and then deleting the original. 
  • You should check the available disk space on the destination location when moving large files across file systems. 
  • Use a shell or terminal that understands quoting or escaping to handle file names with spaces or special characters.
  • If you’re unsure about where a file is before you move it, give the locate command a try to find it quickly. See our guide on using the locate command in Linux for more details.

Common Options and Flags in the Linux mv Command

The mv command comes with useful options for efficient file handling. Let’s see some of them below –

-i (Interactive Mode) prompts before overwriting existing files.
-f (Force Mode) moves files (does not ask for confirmation).
-n (No Overwrite) ensures existing files are not overwritten.
-u (Update Mode) moves a file only if it is newer than the existing one or if the destination file is missing.
-v (Verbose Mode) shows each move operation as it is happening.

How to Move Files in Linux using the mv Command (Examples)

Below are the examples of how you can use the command with different options –

Move a file to another directory (location) from your current directory –

mv file1.txt /home/user/documents/

This command moves file1.txt to the /home/user/documents/ folder.

Rename or change a file’s name –

mv oldname.txt newname.txt

Now, oldname.txt is renamed as newname.txt.

You can move multiple files together like this –

mv file1.txt file2.txt /home/user/documents/

Both file1.txt and file2.txt are moved to /home/user/documents/.

Move a directory/entire folder to a new location with –

mv /home/user/oldfolder /home/user/newfolder

This command moves oldfolder into newfolder.

Prevent Overwrites with the Linux mv Command

Ask before overwriting with -i Option

The below command will enable a confirmation prompt if a file with the same name exists in the destination. This way you won’t accidentally replace files.

mv -i file1.txt /home/user/documents/

Force move without any prompt

You won’t get confirmation messages. The file will move without them and replace any existing file without asking.

mv -f file1.txt /home/user/documents/

Prevent overwriting existing files

The below only moves the files in the absence of any conflicts. Thus, the existing files in the destination remain unchanged –

mv -n file1.txt file2.txt /home/user/documents/

Create a backup

Keep a backup of the file before renaming or moving. The below saves file2.txt with a backup copy before replacing it.

mv -b file1.txt file2.txt

See progress while moving files

You get the details i.e. each step of the moving process with the below –

mv -v file1.txt /home/user/documents/

Special Cases and Advanced Usage

Your filename may have spaces.

So, enclose it in quotes. This way, the command correctly recognizes the entire filename.

mv "file name with spaces.txt" /home/user/documents/
  • Moving hidden files

Well, you can also move hidden files (starting with .) using –

mv .hiddenfile /home/user/documents/

Automatically rename files with today’s date and create a script:

  • Open a new file in a text editor –
nano mv_script.sh
  • Add this script inside:
#!/bin/bash
for file in *.txt; do
    mv "$file" "${file%.txt}_$(date +%F).txt"
done
  • Save and close the file
  • Make the script executable with –
chmod +x mv_script.sh
  • Run it –
./mv_script.sh

Now, all .txt files will be renamed with the current date.

Moving files at once using Wildcards

mv *.txt /home/user/textfiles/

This moves every text file to /home/user/textfiles/.

Ensure you don’t overwrite important files and enable confirmation when moving multiple files –

mv -i *.txt /home/user/documents/

With this, each move operation will ask for confirmation.

Move files to another location based on how old they are say 30 days

find /source_directory -type f -mtime +30 -exec mv {} /destination_directory/ \;

This uses the ‘find’ command to search for the files that the more than 30 days old.

Note: If you’re looking for more details on how to use the find command,  refer our detailed guide on How to Use the Find Command in Linux.

Conclusion

The mv command helps you manage files and directories in Linux. This article provides various ways to move, rename, and handle files safely using different options. However, you can get more details, with man mv command. If you often work with files or directories on Linux Servers, a Linux VPS provides full root access for better control and flexibility.

FAQs

How to Use the mv command in Linux?

You use it in the form mv [options] source destination. The source is the file or directory you want to move or rename. The destination is either a new name or a directory path.

How to Move Files from One Directory to another in Terminal?

In Terminal, run mv filename /path/to/target_directory/. This moves the filename into that directory. If you specify a new full path (with a name), you rename as well. 

Can I Move Multiple Files at Once Using mv?

Yes. You list multiple source files followed by a target directory.
For example:

mv file1 file2 /targetdir/

 The command places both files into /targetdir/.

How Can I Prevent Overwriting Files When Using mv?

You can use the -n or --no-clobber option. That stops mv from replacing any existing file in the destination. You may also use -i (interactive mode) so that mv prompts you before overwriting. 

October 27, 2025