The basic command to move a file in Linux
To move a file from one folder to another in Linux, you use the mv command. Type mv, then the file name, then the destination folder. For example, mv document.txt /home/username/Documents moves document.txt into your Documents folder.
The mv command works the same way whether you are moving a single file or an entire folder. If you want to move a folder called Projects and everything inside it, the command is mv Projects /home/username/Backup. Linux treats folders and files the same way for this operation.
One important difference from dragging files in a graphical interface: mv does not show you a progress bar or ask for confirmation. The file moves when ready and silently. If a file with the same name already exists in the destination, mv will overwrite it without warning.
Key Takeaways
- The mv command moves files and folders by typing mv filename destination in the terminal.
- You can move multiple files at once by listing them all before the destination: mv file1.txt file2.txt /home/username/Documents.
- Use the -i flag to ask for confirmation before overwriting: mv -i oldname.txt newname.txt.
- Moving a file to the same folder with a different name effectively renames it, so mv old.txt new.txt renames the file.
- Absolute paths (starting with /) work from anywhere, but relative paths (like ./folder) only work from your current location.
Moving multiple files at once
If you need to move several files into one folder, list all the file names first, then the destination at the end. For example, mv photo1.jpg photo2.jpg photo3.jpg /home/username/Pictures moves all three photos into the Pictures folder in one command.
You can also use a wildcard to match multiple files by pattern. The asterisk * matches any characters. The command mv *.txt /home/username/Documents moves every file ending in .txt from your current folder into Documents. The command mv report*.pdf /home/username/Archive moves every file that starts with "report" and ends with ".pdf".
When you use a wildcard, Linux expands it to match all files that fit the pattern before running the command. If no files match, you get an error message instead of moving nothing silently.
Understanding absolute and relative paths
A path is the address of a folder or file on your computer. An absolute path starts with a forward slash and describes the full location from the root of the system. For example, /home/username/Documents/report.txt is an absolute path — it works the same no matter where you are in the terminal.
A relative path describes a location relative to where you are right now. If you are already in /home/username, you can type mv report.txt Documents instead of the full path. The dot-dot notation .. means "the folder above this one," so mv file.txt ../ moves the file up one level.
When you first open a terminal, you start in your home folder, usually /home/username. You can see where you are by typing pwd (print working directory). If you get lost or a path does not work, use the absolute path instead — it always works.
Renaming files by moving them
In Linux, renaming and moving are the same operation. When you move a file to the same folder but give it a new name, you have renamed it. The command mv oldname.txt newname.txt renames the file in place.
You can also rename while moving. The command mv /home/username/Downloads/report.pdf /home/username/Documents/final_report.pdf moves the file from Downloads to Documents and renames it at the same time.
This is useful when you want to organize files and fix their names in one step. For example, mv messy_name_2024.jpg /home/username/Pictures/vacation.jpg moves the file into Pictures and gives it a clearer name.
Protecting yourself from accidental overwrites
By default, mv overwrites any file with the same name in the destination without asking. If you move a file called document.txt into a folder that already contains a document.txt, the old file disappears and you cannot get it back.
To prevent this, use the -i flag, which stands for "interactive." The command mv -i document.txt /home/username/Documents asks you to confirm before overwriting. You type y for yes or n for no.
Another option is the -n flag, which means "no clobber" — it refuses to overwrite and just skips the file instead. The command mv -n document.txt /home/username/Documents will not move the file if one with the same name already exists there.
Moving files across different storage devices
The mv command works the same whether you are moving files within your hard drive or to an external drive, USB stick, or network location. As long as the destination is mounted (connected and accessible), you can move to it.
External drives usually appear in /media or /mnt. You can see what is mounted by typing mount in the terminal. For example, if a USB drive is mounted at /media/username/USB_Drive, you can move files there: mv document.txt /media/username/USB_Drive.
One caveat: if you move files across different file systems (for example, from an ext4 drive to a FAT32 USB stick), Linux may have to copy and delete instead of just moving the pointers, which takes longer. The command looks the same, but the operation is slower.
What happens when a move fails
If you try to move a file and something goes wrong, Linux tells you why. Common errors include "No such file or directory" (the source file does not exist or the path is wrong), "Permission denied" (you do not have permission to move that file), and "Is a directory" (you tried to move a folder but forgot it is a folder).
If you do not have permission to move a file, you may need to use sudo (super user do) to run the command with administrator rights: sudo mv /root/file.txt /home/username. Be careful with sudo — it bypasses safety checks and can cause problems if you make a mistake.
If the destination folder does not exist, mv will not create it for you. You have to create the folder first using mkdir (make directory). For example, mkdir /home/username/NewFolder creates the folder, then mv file.txt /home/username/NewFolder moves the file into it.
Frequently Asked Questions
Can I undo a move if I made a mistake?
No — once a file is moved, it is gone from the original location. Linux does not have a trash or recycle bin by default (though some graphical file managers add one). If you moved a file to the wrong place, you have to move it back manually using mv again.
What is the difference between mv and cp?
The cp command copies a file, leaving the original in place. The mv command moves a file, removing it from the original location. Use cp when you want a backup; use mv when you want to reorganize.
Can I move a file to a folder that does not exist yet?
No. You must create the destination folder first using mkdir foldername. Then you can move files into it. If you try to move to a folder that does not exist, you get a "No such file or directory" error.
How do I move a file if its name has spaces in it?
Put the file name in quotes: mv "my document.txt" /home/username/Documents. Without quotes, Linux treats each word as a separate argument and gets confused. Alternatively, you can escape the space with a backslash: mv my\ document.txt /home/username/Documents.
What does the -v flag do?
The -v flag stands for "verbose" and makes mv print out what it is doing. Instead of moving silently, mv -v file.txt /home/username/Documents tells you "file.txt -> /home/username/Documents/file.txt". This is useful when you are moving many files and want to see progress.