Viewing File Content (cat, less, head, tail)
Explore various commands to display and inspect the contents of text files, from full display to specific sections.
Viewing File Content (cat, less, head, tail) is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Inspecting Files in Linux
Ever wonder what's inside a file without opening a graphical editor? The Linux terminal provides powerful commands to quickly view file content.
This is super handy for checking log files, configuration settings, or just getting a quick peek at a document.
`cat`: Displaying Entire Files
The cat command (short for 'concatenate') is one of the simplest ways to display the entire content of a file directly to your terminal screen.
Let's create a small file and then view it:
echo "Line 1\nLine 2\nLine 3" > my_file.txt
Now, run cat my_file.txt:
cat my_file.txt
Output:Line 1
Line 2
Line 3
When `cat` Isn't Enough
While cat is great for small files, it can be problematic for larger ones. If a file has many lines, the content will scroll past your screen too quickly.
You'll only see the very last part of the file, making it hard to read from the beginning. This is where other tools come in handy!
`less`: Viewing Large Files
For larger files, the less command is your friend. It's a 'pager' that lets you view file content one screen at a time, scroll up and down, and even search.
Unlike cat, less doesn't load the entire file into memory, making it efficient for huge files.
Try it (you'll need to press 'q' to exit):
less my_file.txt
The terminal will display the file content, allowing you to navigate.
Navigating `less`
Once you're in less, you can use several keys to move around:
- Spacebar: Scroll down one page.
- b: Scroll up one page.
- Down arrow: Scroll down one line.
- Up arrow: Scroll up one line.
- q: Quit
lessand return to the terminal prompt.
These are essential for basic interactive file viewing!
`head`: Seeing the Beginning
Sometimes you only need to see the very beginning of a file. The head command displays the first few lines of a file.
By default, head shows the first 10 lines of a file.
Let's see the first lines of our my_file.txt (which only has 3 lines):
head my_file.txt
Output:Line 1
Line 2
Line 3
`head -n`: Customizing Line Count
You can specify exactly how many lines head should display using the -n option, followed by the number of lines.
For example, to see only the first 2 lines:
head -n 2 my_file.txt
Output:Line 1
Line 2
This is useful for quickly checking headers or initial configurations.
`tail`: Viewing the End
Just as head shows the beginning, the tail command shows you the end of a file. This is especially useful for log files that constantly have new entries added at the bottom.
By default, tail also shows the last 10 lines of a file.
tail my_file.txt
Output:Line 1
Line 2
Line 3
(Since our file is small, it shows all lines, similar to head).
`tail -n`: Tailoring the End View
Similar to head, you can use the -n option with tail to specify how many lines from the end of the file you want to see.
To view the last 2 lines:
tail -n 2 my_file.txt
Output:Line 2
Line 3
This is perfect for checking the latest entries in a log file.
Quick Check: File Viewing
You have a very large log file named server.log. You want to quickly see only the latest 5 entries without scrolling through the entire file. Which command should you use?
Recap: File Content Commands
Great job! You've learned essential commands for viewing file content:
cat: Displays the entire file, best for small files.less: An interactive pager for large files, allowing scrolling and searching.head: Shows the beginning of a file (default 10 lines), use-nfor custom count.tail: Shows the end of a file (default 10 lines), use-nfor custom count.
These commands are fundamental for efficient Linux navigation and troubleshooting!
Frequently asked questions
Is the “Viewing File Content (cat, less, head, tail)” lesson free?
Yes — the full text of “Viewing File Content (cat, less, head, tail)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Viewing File Content (cat, less, head, tail)”?
Explore various commands to display and inspect the contents of text files, from full display to specific sections. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Viewing File Content (cat, less, head, tail)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Viewing File Content (cat, less, head, tail)
- Understanding File Permissions (chmod, chown)
- Searching for Files (find, grep)
- Creating Links: Hard Links & Symlinks