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 DocumentsIn 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
lsIn 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 backupIn 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
lsIn 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 DocumentsIn the terminal
student@vulntech:~$ mv notes.txt report.txt Documents/ student@vulntech:~$ ls Documents book.txt notes.txt report.txt
Common mistake: Like
cp,mvsilently replaces a file that already has the destination name.mv -iasks first.
Think of it like this:
cpis the photocopier: you end up with two.mvis picking the paper up and putting it somewhere else (or writing a new name on it): you still have one.
Remember:
mv old newrenames.mv file dir/moves. No-rneeded.