AI Hints
Home›Linux Tutorial›Reading Files
Linux · Lesson 31 of 120

cat: Reading Files

Printing a file.

⏱ 2 min read▶ Video at 38:29🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

You've made files, copied them, and moved them. Now let's look inside them. The simplest way to read a file in the terminal is the cat command.

cat: Reading Files
From the video: cat: Reading Files
  • Time to look inside
  • Reading files
  • cat is the simplest

Cat, followed by a file name, prints the whole file to the screen. Cat notes dot TXT shows Sam's three notes. Cat with a path works too, so we can read the report inside Documents, without going there.

Terminal
cat notes.txt
Buy milk
Call Mum
Learn Linux
cat Documents/report.txt
Sales report
January: 120
February: 135
March: 150

Several files at once

Cat is short for concatenate, which means joining things together. Give it several files, and it prints them one after another, as if they were one file. Dash N adds line numbers, which is handy when you're talking about a particular line.

Terminal
cat notes.txt todo.txt
Buy milk
Call Mum
Learn Linux
1. Install Ubuntu
2. Learn the terminal
3. Write a script
cat -n todo.txt
     1  1. Install Ubuntu
     2  2. Learn the terminal
     3  3. Write a script

When not to use cat

Cat is perfect for short files. But cat dumps everything at once. On a file with thousands of lines, the text flies past, and you only see the end. For long files, there are better tools, which we'll meet next. And cat on a photo or a program prints garbage, because those aren't text.

keyvalue
Great forshort text files
Not forvery long files
Not forphotos or programs
Insteadhead, tail, less

Summary

Cat prints a file, cat with several names joins them, and dash N adds line numbers. Next, reading just the start or the end of a long file.

Key takeaways
  • cat <file>
  • cat a b joins
  • cat -n numbers lines
Next up: head and tail — the start and the end