The basic command to move a file

To move a file in terminal, use the mv command followed by the file name and where you want it to go. The simplest form is mv filename destination. For example, if you have a file called report.pdf in your current folder and want to move it to a folder called Documents, you would type mv report.pdf Documents/ and press Enter.

The mv command works the same way on Mac, Linux, and most Unix systems. Windows PowerShell uses a different command called Move-Item, but the concept is identical — you name the file, then name the destination.

One important thing: mv does not copy the file. It removes it from the original location and puts it in the new one. If you want to keep a copy in both places, use cp (copy) instead, then delete the original if you want.

Key Takeaways

  • The mv command moves a file from one location to another in a single action, removing it from where it started.
  • You must know the exact path to the destination folder, or use a relative path like ../folder/ if the folder is nearby.
  • You can rename a file at the same time you move it by typing a new name as the destination.
  • If a file with the same name already exists in the destination, mv will overwrite it without warning on most systems.
  • Use mv -i to make the terminal ask for confirmation before overwriting an existing file.

Moving a file to a specific folder path

If the destination folder is not in your current directory, you need to give the full path or a relative path. A full path starts from the root of your system — on Mac or Linux, that looks like /Users/yourname/Documents/. A relative path describes where the folder is compared to where you are now.

For example, if you are in your home directory and want to move a file to a folder one level down called Projects, use mv report.pdf Projects/. If you need to go up one level first, use ../ — so mv report.pdf ../archive/ moves the file to an archive folder that is one level above where you are.

You can check where you are right now by typing pwd (print working directory) and pressing Enter. This shows your current path. You can also type ls to see what files and folders are in your current location, which helps you figure out the right path to type.

Renaming a file while you move it

The mv command can rename a file and move it in one step. Instead of just typing the destination folder, type the destination folder plus the new file name. For example, mv report.pdf Documents/report_final.pdf moves the file to the Documents folder and renames it to report_final.pdf at the same time.

This is faster than moving first and renaming second, and it is useful when you want to organize files by moving them and giving them clearer names in one action. If you only want to rename without moving, you can use mv oldname.pdf newname.pdf in the same directory.

Avoiding accidental overwrites

If a file with the same name already exists in the destination folder, mv will replace it without asking. This happens silently on most systems, and you will lose the old file. To prevent this, use the -i flag, which stands for "interactive." Type mv -i report.pdf Documents/ and the terminal will ask you to confirm before overwriting.

Some systems have -i turned on by default, but not all. If you want to be extra safe with important files, always use -i. You can also use -n instead, which means "do not overwrite" — the command will simply fail if the file already exists, and your original file stays put.

Moving multiple files at once

You can move more than one file to the same destination in a single command. List all the files you want to move, then the destination folder at the end. For example, mv file1.pdf file2.pdf file3.pdf Documents/ moves all three files to the Documents folder.

You can also use a wildcard to move files that match a pattern. The asterisk * means "any characters." So mv *.pdf Documents/ moves every file ending in .pdf from your current folder to Documents. Be careful with wildcards — make sure you are moving only the files you intend to move, or use ls *.pdf first to see what files match the pattern.

What to do if the move fails

The most common reason mv fails is that the destination folder does not exist. If you type mv report.pdf Documents/ but the Documents folder is not there, you will get an error message saying something like "No such file or directory." Check that the folder exists by typing ls to list what is in your current location, or use pwd to confirm where you are.

Another reason mv might fail is permission issues. If you do not have permission to write to the destination folder, the terminal will tell you "Permission denied." This usually means the folder is owned by another user or is protected by your system. You may need to use sudo (which runs the command with administrator rights), but be cautious with sudo — it can change system files if you are not careful.

If you move a file by accident, you can sometimes undo it by moving it back, but only if you remember where it came from. There is no built-in undo in terminal, so think before you press Enter.

Frequently Asked Questions

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

No, mv will not create a new folder for you. The destination folder must already exist. If you need to create a folder first, use mkdir foldername to make it, then move your file there.

What is the difference between mv and cp?

mv moves a file — it removes it from the original location. cp copies it, leaving the original in place. Use cp when you want the file in two locations, and mv when you want to relocate it.

How do I move a file to my home directory from anywhere?

Use mv filename ~/. The tilde ~ is a shortcut for your home directory, so you do not have to type the full path. This works from any location in the terminal.

What happens if I move a file to a folder with the same file name inside?

By default, mv will overwrite the existing file without warning. Use mv -i filename destination/ to make the terminal ask for confirmation first, or use mv -n to cancel the move if the file already exists.

Can I undo a move command?

Terminal does not have an undo feature. If you moved a file by accident, you can move it back if you remember where it came from. Otherwise, check your system's trash or recycle bin — some systems move deleted files there instead of erasing them permanently.