Shell Moving and Renaming
Moving a file means taking it to a new place. The same command renames a file when the new place is a new name in the same folder.
Move a file into a folder
mv takes to-move.txt into the archive folder.
cp notes.txt to-move.txt
mkdir archive
mv to-move.txt archive/to-move.txt
ls archiveRename a file
mv with a new name in the same folder renames the file.
cp notes.txt draft-name.txt
mv draft-name.txt final-name.txt
ls final-name.txtMove a folder
mv moves the whole docs-old folder to docs-new.
cp -r docs docs-old
mv docs-old docs-new
ls docs-newExample
cp shopping.txt shopping-old.txt
mv shopping-old.txt shopping-list.txt
mkdir store
mv shopping-list.txt store/shopping-list.txt
ls storeKey points
- mv: a command that moves a file or renames it.
- Use mv to move a file into another folder.
- Use mv with a new name in the same folder to rename a file.
- Use mv to move a whole folder, with all its files.
- After a move, the file is in one place only.
In this example, cp makes a file to work with. Then mv renames it to shopping-list.txt. Next mkdir makes a store folder, and mv moves the file into it. The last line lists the folder to show the file is there.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command renames notes.txt to list.txt?