In this tutorial, we’re going to show you how to move a file in Linux. Step-by-step, beginner-friendly instructions for the Linux terminal.
If you’re using a GUI, you just need to drag and drop the file with your mouse. This tutorial will be for the command-line interface (CLI), or Terminal.
These instructions also work for other Unix-based systems, like macOS and BSD.
All of these examples are practical and useful for everyday tasks, with real-world use cases.
What command do you use to move a file in Linux?
We’re going to use the mv
command. Although you can use other commands, this is one of the most commonly used and easiest to use.
MV command syntax and options
The syntax for the mv command is:
mv [options] source destination
The most common options for the mv command are:
- -f (–force) – forces a move/overwrite, without verifying or prompting you
- -i (–interactive) – verifies and prompts before moving or overwriting
- -n (–no-clobber) – doesn’t move the file if a file already exists in the destination directory
- -b (–backup) – takes a backup of the file you’re moving
- -u (–update) – moves a file only when the source file is newer than the destination file or when the destination file is missing
There are plenty more options for the mv command, some of which will be explained below. To get a list of all the options and more info for the command, run the command with the help option:
mv --help
How to move a file from one directory to another in Linux?
Knowing the basic syntax of the command, we’ll show you the most basic example and most common usage of the mv command.
To move a file (song.mp3) from the current directory to the home directory, you’d run:
mv song.mp3 /home
To move a file from a specific directory to another specific directory, you’d run:
mv /home/music/song.mp3 /opt/music
Even though this command is pretty useful if you’re using the most basic example like this, we’ll show you more use cases and more useful examples below.
How to move a file to its parent directory in Linux?
If you’re currently in a directory and you want to move a file from your current directory to its parent directory, you can use the following syntax:
mv file ..
You need to use ..
as a replacement instead of typing out the full path to the parent directory.
A common and useful example is where you’d need to move a file (in this case index.html) from your current directory “/var/www/html/devwebsite” to its parent directory, “/var/www/html”. The command would be:
mv index.html ..
Assuming that you’re already in “/var/www/html/devwebsite” (you can run “pwd” to check), the file will be moved to the parent directory. You don’t have to currently be in a directory to move a file to its parent directory. If you’re currently in /root/ (cd ~), you can still move any file from anywhere to its parent directory.
For example, this command will the same thing the command above does, regardless of what directory you’re currently in:
mv /var/www/html/devwebsite/index.html ..
How to move multiple files in Linux?
There are more ways of moving multiple files in Linux with the mv command.
The most common example is using the * wildcard.
So, for example, if you’d like to move all the .mp3 files from the /home/username/Downloads folder to your /home/username/Music, folder, you’d run:
mv /home/linuxstans/Downloads/*.mp3 /home/linuxstans/Music
In this example, our username is “linuxstans”.
You can also combine this with another wildcard, ~ which will point to the /home/username directory.
The updated command would be:
mv ~/Downloads/*.mp3 ~/Music
If you’re currently in the Downloads directory, you’ll just need to run
mv *.mp3 ~/Music
The * wildcard can also be used in different ways, like all files that have the name “linuxstans”, with whatever file extension:
mv linuxstans.* /opt
This will move all the files named “linuxstans” from the current directory, like linuxstans.jpg, linuxstans.mp3, linuxstans.pdf, etc. to the /opt directory.
To move all files from the current directory to the parent directory, run:
mv *.* ..
To move all files and all directories from the current directory to the parent directory, run:
mv * ..
You can combine all these wildcards and examples into a command that would be the most useful for you.
If you want to move specific multiple files, you can just list them out. For example, if you’d want to move index.html and contact.html from the current directory to /var/www/html, you’d run:
mv index.html contact.html /var/www/html
How to move a file without overwriting an already existing file in another directory?
If you’re moving a file in a directory where a file with the same name already exists, by default (on most systems), the destination file will be overwritten with the source file.
For example, if there’s a file named notes.txt in ~/Documents and you’re trying to move a file named notes.txt from ~/Downloads to the ~/Documents directory, the file in ~/Documents will get overwritten by the file from ~/Downloads. So, this command:
mv ~/Downloads/notes.txt ~/Documents
Overwrites/replaces /Documents/notes.txt with /Downloads/notes.txt without prompting you. If you’d like to get a prompt, just use the “-i” option:
mv -i ~/Downloads/notes.txt ~/Documents
In which case, the terminal will prompt you with:
mv: overwrite '/home/linuxstans/Documents/notes.txt'?
To confirm, just enter “y”.
If by default you get a prompt when moving a file on your system, you can use the -f option to force the move without prompting you, which is the opposite of -i.
How to move a file and make a backup if it already exists?
The option I most often use with the mv command is the –backup (or -b flag). If you’re using -b, the mv command will make a backup of the file you’re moving and add a “~” at the end of it, if it already exists. So, if we’re using our previous example, instead of overwriting the notes.txt file, we can create a backup of it. Example:
mv -b ~/Downloads/notes.txt ~/Documents
This command will move the file notes.txt from /home/linuxstans/Downloads/ to /home/linuxstans/Documents/notes.txt~
How to move multiple files and not overwrite files if they already exist?
To combine multiple options again by using our previous example, here’s an example of using the -n option.
If a file ~/Documents/notes.txt already exists, the following command will do nothing:
mv -n ~/Downloads/notes.txt ~/Documents
A useful example is when you’re moving multiple files, in our example, songs. If the song already exists, the command will skip those files and only move the ones that don’t exist in the destination directory.
mv -n ~/Downloads/*.mp3 ~/Music
This command will essentially move all .mp3 files from the Downloads folder that don’t already exist in the Music folder.
How to move and rename a file at the same time?
The mv command can also be used to rename a file. To use it, you just need to enter the specific filename as the destination instead of just a directory.
For example, if you’d like to move a file named todays-notes.txt from ~/Desktop to ~/Documents and rename it to notes.txt, you can use the following command:
mv ~/Desktop/todays-notes.txt ~/Documents/notes.txt
A simpler example would be if you’re just renaming a file in the directory you’re currently in. So if you had a typo in a file, like indez.html, you can just use the following command to rename it:
mv indez.html index.html
This will rename the file from ‘indez.html’ to ‘index.html’
How to move a file only if it’s newer than the destination file?
If there’s a file named notes.txt in ~/Desktop which was created today, and a file named notes.txt in ~/Documents which was created yesterday, if you run:
mv -u ~/Desktop/notes.txt ~/Documents
The command will move and replace the older file. But, if the file in ~/Documents was newer than the file in ~/Desktop, the command would do nothing.
This command will also move the file if there’s no file named notes.txt in ~/Documents.
How do you move files on Linux?
Leave a comment below and let us know of any useful examples for the mv command.
One thought on “How to Move a File in Linux”
Thanks for such precise and clear instructions. I’ve been looking forward for information like this.