The Basic Command to Move a File
To move a file in Linux, you use the mv command followed by the file name and where you want it to go. The simplest form looks like this: mv filename destination. Linux executes the command when ready — there is no confirmation dialog, and the file disappears from its original location and appears in the new one.
For example, if you have a file called report.txt in your home directory and you want to move it to a folder called Documents, you would type mv report.txt Documents/. The file is now in Documents and no longer in your home directory. This is different from copying, which leaves the original file behind.
The mv command also renames files. If you type mv report.txt summary.txt, the file keeps its location but its name changes. You are moving it from one name to another in the same place.
Key Takeaways
- The mv command moves a file to a new location and removes it from the old one, unlike copy which leaves the original behind.
- You can move a file to a folder by typing the folder name after the file name, with a forward slash at the end of the folder name.
- Moving a file to a new name in the same location is how you rename files in Linux.
- Linux executes the move when ready without asking for confirmation, so double-check the command before you press Enter.
- You can move multiple files at once by listing them all before the destination folder.
Moving a File to a Different Folder
When you move a file to a folder, you need to tell Linux both where the file is now and where you want it to go. If the file is in your current working directory, you only need the file name. If it is somewhere else, you need the path to it.
Suppose you have budget.xlsx in your home directory and you want to move it into a subfolder called Finance. Type mv budget.xlsx Finance/. The forward slash after Finance tells Linux that Finance is a folder, not a file name. If you forget the slash, Linux might try to rename the file to Finance instead of moving it into that folder.
If the file is not in your current directory, include the path. For example, mv Downloads/photo.jpg Pictures/ moves photo.jpg from your Downloads folder to your Pictures folder. You can use absolute paths too: mv /home/username/Downloads/photo.jpg /home/username/Pictures/ does the same thing but spells out the full path from the root of the system.
Moving Multiple Files at Once
You can move more than one file to the same destination in a single command. List each file name, then the destination folder at the end. For example, mv file1.txt file2.txt file3.txt Documents/ moves all three files into Documents.
You can also use a wildcard to move files that match a pattern. The asterisk * stands for any characters. The command mv *.jpg Pictures/ moves every file ending in .jpg from your current directory into Pictures. The command mv report* Archive/ moves every file whose name starts with report — so report.txt, report_2024.pdf, and report_final.docx would all move.
Be careful with wildcards. If you type mv * by mistake, Linux will try to move every file in the current directory, which is usually not what you intended. Always check your command before pressing Enter.
Renaming a File While Moving It
You can move a file and rename it in the same command. Instead of just typing the destination folder, type the folder path followed by the new file name. For example, mv old_report.txt Documents/new_report.txt moves the file into Documents and renames it at the same time.
This is useful when you are organizing files and want to give them clearer names as you sort them. You might move and rename a dozen files in a few commands rather than doing each step separately. The file ends up in the right place with the right name in one operation.
What Happens If the Destination Folder Does Not Exist
If you try to move a file to a folder that does not exist, Linux will return an error message saying something like "No such file or directory". The file stays where it is. You must create the destination folder first using the mkdir command before you can move files into it.
For example, if you type mv photo.jpg NewFolder/ and NewFolder does not exist, the command fails. First create the folder with mkdir NewFolder, then run the move command. After that, the file moves successfully.
Moving a File to a Location Where a File With That Name Already Exists
If you move a file to a destination where another file has the same name, Linux overwrites the existing file without asking. The old file is deleted and replaced by the one you are moving. This happens silently — you get no warning.
Some versions of Linux have a flag called -i that makes mv ask before overwriting. You would type mv -i filename destination. Linux then prompts you to confirm. If you are not sure whether a file with that name already exists in the destination, using -i is safer. Type y to proceed or n to cancel.
Moving Files You Do Not Have Permission to Access
Linux controls who can read, write, and move files through a permission system. If you try to move a file that belongs to another user or that has restricted permissions, you may see an error like "Permission denied". The file does not move.
In most cases on a personal computer, you own the files in your home directory and can move them freely. If you are working on a shared system or trying to move a system file, you may need higher permissions. The sudo command runs a command with administrator privileges: sudo mv filename destination. Linux will ask for your password. Use sudo only when you are certain you need it, because it bypasses normal safety checks.
Frequently Asked Questions
Can I undo a move if I made a mistake?
Linux does not have an undo function for the mv command. Once a file is moved, it is moved. If you moved a file to the wrong location, you must move it again to the correct location. This is why it is important to double-check your command before pressing Enter, especially when using wildcards or moving multiple files.
What is the difference between mv and cp?
The mv command moves a file — it removes it from the original location. The cp command copies a file — it leaves the original behind and creates a duplicate in the new location. Use mv when you want to reorganize files, and cp when you want to keep a backup or duplicate.
Can I move a folder instead of a file?
Yes. The mv command works on folders the same way it works on files. mv OldFolderName NewLocation/ moves the entire folder and everything inside it. You can also rename a folder: mv OldFolderName NewFolderName renames it in place.
What does the dash in front of a letter mean, like in mv -i?
Dashes introduce flags or options that change how a command behaves. The -i flag makes mv ask before overwriting. Other common flags include -v (verbose, which shows you what the command is doing) and -f (force, which skips confirmation). You can combine them: mv -iv filename destination asks before overwriting and tells you what happened.
Can I move a file to a location using just a tilde, like ~/Documents?
Yes. The tilde ~ is a shortcut for your home directory. mv filename ~/Documents/ moves the file into your Documents folder no matter where you are in the file system. This is faster than typing the full path and works the same way on every Linux system.