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

Lesson 4: Look inside files and folders

Lesson objectives:

  • Use cat to print the whole contents of a small file.
  • Use less to scroll through a longer file and exit safely.
  • Use file to learn what kind of file something is.
  • Read a long-form ls listing with ls -l.

Prerequisites: Lessons 01-03 | Previous << 03 | Next 05 >>

Peeking without opening an app

Graphical file managers let you double-click a file to open it. The terminal has the same power, but with text commands. For short files, cat prints the whole thing at once. For long files, less opens a reader that lets you scroll and then quit 1.

Two more commands help you inspect things:

  • file tells you what kind of file something is (text, image, directory, and so on).
  • ls -l gives a detailed list including file size and last-changed date 23.

Worked example (follow along)

First, make sure you are in a folder with a text file. If you are not sure, create one by following these commands exactly.

text
$ cd ~$ mkdir -p terminal-practice$ cd terminal-practice$ echo "Hello, terminal." > greeting.txt$ cat greeting.txtHello, terminal.$ ls -l greeting.txt-rw-r--r--  1 alex  staff  18 Jul 17 09:15 greeting.txt$ file greeting.txtgreeting.txt: ASCII text

What happened:

  1. mkdir -p terminal-practice created a folder called terminal-practice in your home directory.
  2. echo "Hello, terminal." > greeting.txt wrote a short line into a new file.
  3. cat greeting.txt printed the whole file.
  4. ls -l showed details: -rw-r--r-- means it is a regular file, 18 is the byte size, and the date shows when it was last changed.
  5. file greeting.txt confirmed it is plain text.

To practice less, you can look at a system file that is longer than one screen, such as /usr/share/dict/words on macOS or /usr/share/doc files on Linux. Run:

text
$ less /usr/share/dict/words

Scroll with the arrow keys or space bar. Press q to quit and return to the prompt 1.

Your turn (faded example)

You want to read a five-line poem stored in poem.txt. Fill in the command:

text
$ ____ poem.txt

Answer: cat poem.txt (or less poem.txt if you want to scroll).

Summary + what's next

You can now read small files with cat, long files with less, learn a file's type with file, and read details with ls -l. In Lesson 5, you will learn to create, move, copy, and delete files and folders from the terminal.

Footnotes

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

  2. Apple Command Line Primer — https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/CommandLInePrimer/CommandLine.html

  3. MDN: Command line crash course — https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Environment_setup/Command_line

练习

01

Create a small text file called notes.txt in your terminal-practice folder, put two lines in it, and use cat to print it.

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

Use ls -l in your terminal-practice folder. Then run file on each file you see. Write down what kind of file each one is.

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