Terminal basics: files, folders, and commands · 第 5 / 6 节

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 rm and rmdir.

Prerequisites: Lessons 01-04 | Previous << 04 | Next 06 >>

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, and rmdir deletes empty folders 1.

Security note: The terminal does not send deleted files to a trash bin. When rm removes a file, it is gone immediately. Use rm slowly, especially with patterns that match many files.

Worked example (follow along)

You are in ~/terminal-practice. Run these commands one at a time.

text
$ mkdir backup$ lsbackup  greeting.txt  notes.txt$ cp greeting.txt backup/greeting-copy.txt$ ls backupgreeting-copy.txt$ mv greeting.txt hello.txt$ lsbackup  hello.txt  notes.txt$ rm hello.txt$ lsbackup  notes.txt$ rmdir backuprmdir: backup: Directory not empty$ rm backup/greeting-copy.txt$ rmdir backup$ lsnotes.txt

What happened:

  1. mkdir backup created a folder.
  2. cp greeting.txt backup/greeting-copy.txt copied the file into the backup folder.
  3. mv greeting.txt hello.txt renamed the file.
  4. rm hello.txt deleted the renamed file.
  5. rmdir backup failed because the folder was not empty.
  6. After deleting the file inside the folder, rmdir backup succeeded 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:

text
$ ____ archive$ ____ notes.txt archive/todo.txt

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

  1. DigitalOcean: Linux Navigation and File Management — https://www.digitalocean.com/community/tutorials/basic-linux-navigation-and-file-management 2 3 4

练习

01

In ~/terminal-practice, create a folder named drafts, copy notes.txt into it, and confirm the copy exists.

Level 1 (warm-up)
完成标准 · 本地勾选
02

Create a folder named temp, move a file into it, then delete the folder and its contents. Be careful: this permanently deletes the file.

Level 2 (advanced)
完成标准 · 本地勾选