Lesson 5: Move, copy, create, and delete
Lesson objectives:
- Create a folder with
mkdir.- Move or rename a file with
mv.- Copy a file or folder with
cp.- Delete files and empty folders with
rmandrmdir.
The terminal's file manager
Everything you do by dragging and dropping files in a graphical window can also be done with commands. In fact, commands are often faster once you know them. The four basic tools are:
mkdir(make directory) creates a new folder 1.mv(move) moves a file or renames it 1.cp(copy) copies a file or folder 1.rm(remove) deletes files, andrmdirdeletes empty folders 1.
Security note: The terminal does not send deleted files to a trash bin. When
rmremoves a file, it is gone immediately. Usermslowly, especially with patterns that match many files.
Worked example (follow along)
You are in ~/terminal-practice. Run these commands one at a time.
What happened:
mkdir backupcreated a folder.cp greeting.txt backup/greeting-copy.txtcopied the file into the backup folder.mv greeting.txt hello.txtrenamed the file.rm hello.txtdeleted the renamed file.rmdir backupfailed because the folder was not empty.- After deleting the file inside the folder,
rmdir backupsucceeded and removed the empty folder.
Your turn (faded example)
You want to rename notes.txt to todo.txt and move it into a new folder called archive. Fill in the blanks:
Answer: mkdir archive, then mv notes.txt archive/todo.txt.
Summary + what's next
You can now create folders, copy and move files, and delete them safely. In Lesson 6, you will learn how commands take options and flags, how to find help, and how to stop or recover from a command that goes wrong.
Footnotes
-
DigitalOcean: Linux Navigation and File Management — https://www.digitalocean.com/community/tutorials/basic-linux-navigation-and-file-management ↩ ↩2 ↩3 ↩4