Cantech Knowledge Base

Your Go-To Hosting Resource

How to Move Files in Linux Using the 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.

Options You Can Use with mv

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.

Practical Uses of the mv Command

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.

Handling Overwrites and Safety Checks

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

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.

May 16, 2025