LearnLinux FoundationsManipulation

mv

mv - a hands-on Linux lab on a real virtual machine.

Meet mv, the one command that both moves and renames, because to Linux they are the same act. A new name renames the file; a folder moves it. mv is silent on success, so you confirm with ls. Learn -v to make it speak, -i to be asked before an overwrite, -n to never overwrite, and why mv needs no -r for folders.

You have a file called draft.txt and you want it named final.txt. Different task, you might think, from taking that same file and dropping it into a reports folder. One is a rename, the other is a move.

Linux disagrees. To Linux, they are the same act. Renaming a file is just moving it to a new name in the same folder. Moving it to another folder is the same thing with a new address. One command does both, and it is called mv, short for move. This lesson shows you how a single tool covers what feels like two separate jobs.

The black boxes below are a practice terminal: a safe sandbox that checks the one command each step teaches, so you cannot break anything. mv says nothing when it works. That silence is normal and it is the whole challenge of this lesson, so after every mv you will run a second command, like ls, to SEE what changed on the disk.

mv takes something from one name and gives it another name. You write it as mv <source> <destination>: the thing you have, then where you want it to end up. Source is what exists now. Destination is what you want.

Because a rename and a move are the same act to Linux, the destination decides which one happens. If the destination is a new name, you renamed the file. If the destination is a folder, you moved the file into it. Same command, two outcomes, chosen entirely by what you type on the right.

One warning up front that the rest of the lesson keeps proving: mv prints nothing when it succeeds. No cheerful confirmation, no summary. A blank line and a fresh prompt IS success. To trust it, you look at the result yourself.

mv has been in Unix since the very first releases in the early 1970s, alongside cp and rm. The three-letter name is Unix tradition: short commands you type a hundred times a day should be quick to type. mv, cp, ls, rm. Two letters wherever they could manage it.

Start with the rename, because it is the form you will reach for most. You have a file named old.txt, and you want it named new.txt. The source is old.txt, the destination is the new name new.txt.

Before you run it, decide what you expect: after the rename, old.txt should be gone and new.txt should exist in its place. There should be one file, not two. Run the rename, then list the folder to check:

mv old.txt new.txt

prompt: student@linuxcamp:~$ answer: mv old.txt new.txt output: hint: Type mv, a space, the old name, a space, then the new name: mv old.txt new.txt

Nothing printed. That blank response is mv succeeding. It did not ask for approval and it did not report back, because on Unix silence means it worked. The change is real, it is just on the disk where you cannot see it yet. That is why the next move is always to look.

Now list the folder and read the proof:

ls

prompt: student@linuxcamp:~$ answer: ls output: new.txt hint: Just the two letters ls and Enter to list the folder: ls

There it is. old.txt is gone and new.txt stands in its place. Notice there is only ONE file, not two. mv did not make a copy and leave the original behind; it renamed the single file in place. That is the difference between mv and cp from the last lesson: cp duplicates, mv relocates. The contents never changed, only the name on the door.

Now the other half of the same command. Instead of a new name, you give mv a folder as the destination. When the destination is a folder, mv drops the file INSIDE it and keeps the original filename.

You have a file report.txt and a folder archive. You want the file tucked inside the folder. The trailing slash on archive/ is a habit worth building: it says out loud that the destination is a folder, not a new filename. Run the move, then look inside the folder:

mv report.txt archive/

prompt: student@linuxcamp:~$ answer: mv report.txt archive/ output: hint: Source first, then the folder name with a slash: mv report.txt archive/

Silent again, as expected. mv saw that the destination archive/ was a folder, so instead of renaming the file it moved the file into that folder under its own name. Prove it by looking inside.

List the contents of the folder to see where the file went:

ls archive

prompt: student@linuxcamp:~$ answer: ls archive output: report.txt hint: List the folder by name to see what is inside it: ls archive

report.txt now lives inside archive, and it is no longer in the folder you started in. Same command as the rename, opposite result, and the only thing that changed was the destination: a folder instead of a new name. That is the whole trick of mv. Read the right-hand side and you know which job it did.

The silence protects you from clutter, but sometimes you WANT mv to speak, especially when you are moving many files and need to see each one land. The -v flag turns silence into a running report. -v stands for verbose, which just means talkative.

This is the one form of mv that prints on its own, so you do not need a follow-up ls to see the result. Rename a file with -v and watch it narrate:

mv -v a.txt b.txt

prompt: student@linuxcamp:~$ answer: mv -v a.txt b.txt output: renamed 'a.txt' -> 'b.txt' hint: Add the -v flag right after mv: mv -v a.txt b.txt

This time mv told you exactly what it did: renamed 'a.txt' -> 'b.txt'. The arrow reads left to right, from the old name to the new. That one line is proof without a separate ls. When you move a whole batch of files with -v, you get one such line per file. You can watch the entire operation and catch anything that landed in the wrong place. For a single quiet rename you will skip -v; for a big move you will be glad of it.

Here is the danger that makes mv worth respecting. If the destination already exists, mv overwrites it without a word. The old file is gone, replaced, no warning, no undo. That silent overwrite has ruined many a good file.

The -i flag is the seatbelt. -i stands for interactive, and it makes mv stop and ASK before it clobbers anything that is already there. You answer y for yes or n for no. Say no and the original is safe.

Suppose both notes.txt and keep.txt already exist, and you try to move notes.txt on top of keep.txt. With -i, mv pauses for permission:

mv -i notes.txt keep.txt

prompt: student@linuxcamp:~$ answer: mv -i notes.txt keep.txt output: mv: overwrite 'keep.txt'? hint: Put the -i flag right after mv to be asked first: mv -i notes.txt keep.txt

Instead of silently replacing keep.txt, mv stopped and asked mv: overwrite 'keep.txt'?. Now the choice is yours: type y and Enter to go ahead, or n and Enter to cancel and leave keep.txt untouched. Without -i, that question is never asked and keep.txt is simply overwritten. Many engineers make -i the default by adding an alias, a trick you will meet later. For now, reach for -i whenever the destination might already exist.

There is a stronger cousin: -n, for no-clobber. Where -i stops to ask, -n refuses outright. It never overwrites and it never prompts; if the destination exists, mv quietly skips the move and leaves both files as they were. Use -i when you want the choice, -n when you never want to overwrite, full stop.

Scaffolding off. No command is printed this time. You have every piece you need.

You have a file named temp.txt in the current folder, and you want it renamed to keep.txt. There is no folder involved, just a straight rename in place. Type the command that does it. Remember, it will say nothing back, and that silence means it worked.

prompt: student@linuxcamp:~$ answer: mv temp.txt keep.txt output: hint: The rename form is mv, then the current name, then the new name. Think mv, temp.txt, keep.txt.

Silent, and correct. You gave mv a source and a destination that was a new name, so it renamed the file in place: temp.txt is gone and keep.txt holds the same contents. You recalled the rename form from memory, which is the exact move the real machine will ask of you. Prove it to yourself the way you learned: run ls and confirm keep.txt is there and temp.txt is not.

mv fails loudly even though it succeeds quietly. When something is wrong, it tells you plainly. Here are the three messages you are most likely to see, each with what it means and what to check first.

The message mv: cannot stat 'old.txt': No such file or directory means the SOURCE does not exist. mv went looking for old.txt to move and found nothing there. Check the spelling of the source name and check that you are standing in the folder that actually holds it. Run ls first to confirm the file is really there.

The message mv: cannot move 'data' to 'backup/data': Directory not empty appears when you try to move a folder on top of another folder that already exists and has files inside it. mv will not merge two folders for you, so it stops. Check whether the destination folder already exists; either pick a different name or empty out the one that is blocking you.

The message mv: 'notes.txt' and 'notes.txt' are the same file means the source and the destination are the very same file, usually because you typed the same name twice. mv has nothing to do, so it refuses. Check that the two names on your command line are actually different.

You may have expected an -r flag here, the way cp and rm need -r to handle folders. mv does not. It moves a folder and everything inside it natively, no flag required, because moving is just relabelling the address, not walking every file. mv oldfolder newfolder renames a whole tree in one silent step.

You earned this cheat sheet. Every row is a form of mv you just ran or met:

Here is the one idea under all of it. mv moves a thing to a new name, and a rename is just a move that stays in the same folder. It is silent on success, so you confirm with ls. And it overwrites without asking unless you add -i or -n.

mv and cp are a matched pair. cp leaves the original where it was and makes a second copy; mv picks the original up and carries it somewhere new, leaving nothing behind. When you want two files, reach for cp. When you want the same file in a new place or under a new name, reach for mv.

The practice terminal has shown you the whole of mv. It renames when the destination is a new name and moves when the destination is a folder. It is silent on success, so ls is how you confirm. -v makes it speak, -i makes it ask before overwriting, and it handles folders with no -r. Every one of those you typed yourself.

The Manipulation module ends with one real Linux machine, the module capstone mission, and that is where you put these pieces to work for real. A VM boots just for you, with a live shell and real files to move. It hands you objectives that use exactly what you practiced across this module: creating, copying, moving, and linking files. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Manipulation lessons, then go move some real files for yourself.

Practice mv in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.