Lesson 4: Look inside files and folders
Lesson objectives:
- Use
catto print the whole contents of a small file.- Use
lessto scroll through a longer file and exit safely.- Use
fileto learn what kind of file something is.- Read a long-form
lslisting withls -l.
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:
filetells you what kind of file something is (text, image, directory, and so on).ls -lgives 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.
What happened:
mkdir -p terminal-practicecreated a folder calledterminal-practicein your home directory.echo "Hello, terminal." > greeting.txtwrote a short line into a new file.cat greeting.txtprinted the whole file.ls -lshowed details:-rw-r--r--means it is a regular file,18is the byte size, and the date shows when it was last changed.file greeting.txtconfirmed 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:
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:
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
-
DigitalOcean: Linux Navigation and File Management — https://www.digitalocean.com/community/tutorials/basic-linux-navigation-and-file-management ↩ ↩2
-
Apple Command Line Primer — https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/CommandLInePrimer/CommandLine.html ↩
-
MDN: Command line crash course — https://developer.mozilla.org/en-US/docs/Learn_web_development/Getting_started/Environment_setup/Command_line ↩