The mv command moves or renames files in one step

To move a file in Linux, you use the mv command followed by the file name and the destination. The basic structure is mv filename destination. This single command both relocates the file and removes it from the original location — it does not copy and leave behind a duplicate.

The mv command works the same way whether you are moving a file to a different folder in your home directory or to a completely different part of the file system. You can also use mv to rename a file by "moving" it to the same directory with a new name.

Unlike graphical file managers where you drag and drop, the command line requires you to know or type the exact path where the file currently sits and where you want it to go. This precision is what makes mv reliable — there is no ambiguity about which file moved where.

Key Takeaways

  • The mv command moves a file to a new location and removes it from the original folder in a single action.
  • The basic syntax is mv filename destination, where destination can be a folder path or a new file name in the same directory.
  • You can move multiple files at once by listing them before the destination: mv file1 file2 file3 /path/to/folder.
  • Use mv -i to prompt before overwriting a file that already exists at the destination with the same name.
  • Moving a file requires write permission in both the source directory and the destination directory.

Moving a single file to a different folder

Open your terminal and navigate to the directory where your file is located, or use the full path to the file. Type mv filename /path/to/destination/ and press Enter. For example, if you have a file called report.txt in your home directory and want to move it to a folder called Documents, you would type mv report.txt Documents/.

The trailing slash after the folder name tells Linux that Documents is a directory, not a file name. If you omit the slash, Linux will still move the file there, but it is clearer to include it. After you press Enter, the file disappears from your current directory and appears in the destination folder. The terminal does not print a success message — no output usually means the command worked.

If the destination folder does not exist, mv will fail and show an error. Create the folder first using mkdir foldername if you need to. If you want to move the file to a folder that is several levels deep, type the full path: mv report.txt /home/username/Documents/Work/Reports/.

Renaming a file with the mv command

You can rename a file by moving it to the same directory with a new name. Type mv oldname.txt newname.txt and press Enter. This works because you are technically moving the file to a destination with a different name in the same location.

This method is faster than opening a file manager and right-clicking to rename. It also works for renaming files that have spaces or special characters in their names, as long as you handle the spaces correctly. If the file name has spaces, wrap it in quotes: mv "old file name.txt" "new file name.txt".

Moving multiple files at once

To move several files to the same destination folder, list all the file names before the destination path. Type mv file1.txt file2.txt file3.txt /path/to/destination/ and press Enter. All three files move to that folder in a single command.

You can also use a wildcard to move all files of a certain type. For example, mv *.txt Documents/ moves every file ending in .txt from your current directory to the Documents folder. The asterisk matches any characters before the extension. Be careful with wildcards — if you type mv * by accident, you could move everything in your current directory.

Preventing accidental overwrites

If a file with the same name already exists in the destination folder, mv will overwrite it without warning. To prevent this, use the -i flag, which stands for interactive. Type mv -i filename destination/ and press Enter. If a file with that name exists at the destination, Linux will ask you to confirm before overwriting.

You will see a prompt like overwrite 'destination/filename'? Type y to proceed or n to cancel. This is especially useful when you are moving files with common names or when you are not certain whether the destination already contains a file you want to keep.

Understanding file permissions and mv

To move a file, you need write permission in both the folder where the file currently sits and the folder where you are moving it. If you do not have write permission in the source directory, you cannot move the file out. If you do not have write permission in the destination directory, you cannot move the file in.

If you try to move a file and see a "Permission denied" error, you may need to use sudo to run the command with administrator privileges: sudo mv filename destination/. Be cautious with sudo — it bypasses permission checks, so use it only when you are certain the move is safe. If you are moving files in your own home directory, you should not need sudo.

Moving files across different storage devices

The mv command works the same way whether you are moving a file within your home directory or to an external drive or network location. If your external drive is mounted at /media/username/drivename/, you can move a file there just as you would move it to any other folder: mv filename /media/username/drivename/.

Moving across devices is faster than copying and deleting because mv relocates the file rather than creating a duplicate. However, if the destination is on a different file system (for example, moving from an ext4 drive to a FAT32 USB drive), some file attributes may not transfer. The file itself will move successfully, but extended attributes like permissions may be reset to defaults on the new device.

Frequently Asked Questions

What happens if I move a file to a location and then realize I made a mistake?

The file is gone from the original location and now sits in the destination. You can move it back by typing mv destination/filename original/location/. Linux does not have an undo function for the command line, so if you are uncertain about a move, use mv -i to get a confirmation prompt before the action completes.

Can I move a folder and everything inside it?

Yes. The mv command works on directories the same way it works on files. Type mv foldername /path/to/destination/ and the entire folder and all its contents move together. You do not need any special flags.

What does it mean if the terminal shows no output after I type mv?

No output is the normal behavior when mv succeeds. The command ran, the file moved, and there is nothing to report. If something went wrong, you would see an error message like "No such file or directory" or "Permission denied".

Can I move a file to a location that does not exist yet?

No. The destination folder must exist before you run mv. If it does not, create it first with mkdir /path/to/new/folder, then move the file there.

How do I move a file if its name starts with a dash or hyphen?

If a file name begins with a dash, Linux may interpret it as a command flag instead of a file name. To move such a file, use mv -- -filename destination/. The two dashes tell mv to stop looking for flags and treat everything after as a file name.