Moving and Renaming with mv

Move a file

mv works like cp, but the original is gone afterwards: the file is moved, not copied.

mv report.txt Documents/
ls
ls Documents

In the terminal

student@vulntech:~$ mv report.txt Documents/
student@vulntech:~$ ls
Documents  backup  notes.txt
student@vulntech:~$ ls Documents
book.txt  report.txt

Rename a file

Linux has no separate rename command. To rename, you move a file to a new name in the same directory:

mv notes.txt meeting-notes.txt
ls

In the terminal

student@vulntech:~$ mv notes.txt meeting-notes.txt
student@vulntech:~$ ls
Documents  backup  meeting-notes.txt  report.txt

Move and rename in one go

mv notes.txt backup/old-notes.txt
ls backup

In the terminal

student@vulntech:~$ mv notes.txt backup/old-notes.txt
student@vulntech:~$ ls backup
old-notes.txt

Directories

Unlike cp, mv moves directories without any extra option:

mv Documents Papers
ls

In the terminal

student@vulntech:~$ mv Documents Papers
student@vulntech:~$ ls
Papers  backup  notes.txt  report.txt

Moving several files

With more than two arguments, the last one must be a directory, and everything else is moved into it:

mv notes.txt report.txt Documents/
ls Documents

In the terminal

student@vulntech:~$ mv notes.txt report.txt Documents/
student@vulntech:~$ ls Documents
book.txt  notes.txt  report.txt

Common mistake: Like cp, mv silently replaces a file that already has the destination name. mv -i asks first.

Think of it like this: cp is the photocopier: you end up with two. mv is picking the paper up and putting it somewhere else (or writing a new name on it): you still have one.

Remember: mv old new renames. mv file dir/ moves. No -r needed.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…