Reading Files in the Terminal
Learn essential terminal commands for reading files including cat, less, head, and tail. Covers practical usage scenarios and tips for viewing file contents efficiently in Linux and Unix systems.
When you're working in the terminal, you'll frequently need to examine the contents of files without opening a full text editor. Linux and Unix systems provide several powerful commands for reading files, each with its own strengths and use cases. Let's explore the most common and useful methods.
The cat Command: Your Basic File Reader
The cat command is probably the most straightforward way to display file contents. It reads the entire file and prints it to your terminal.
cat filename.txtThis works great for small files, but be careful with large files – cat will dump everything to your screen at once, which can be overwhelming. You can also concatenate multiple files:
cat file1.txt file2.txt file3.txtThe name "cat" actually comes from "concatenate," which explains this behavior.
Reading Large Files with less and more
For larger files, you'll want to use pagers that let you scroll through content page by page. The less command is the modern standard:
less large_file.logOnce inside less, you can navigate using these keys:
- Space bar: Move forward one page
- B: Move backward one page
- Arrow keys: Move line by line
- /search_term: Search for text
- Q: Quit and return to terminal
The older more command works similarly but with fewer features. Most systems have both, but less is generally preferred because it's more flexible and allows backward navigation.
Viewing File Beginnings and Endings
Sometimes you only need to see the start or end of a file. The head command shows the first 10 lines by default:
head access.logYou can specify a different number of lines:
head -n 20 access.logSimilarly, tail shows the last 10 lines:
tail error.logThe tail command has a particularly useful flag for monitoring files that are actively being written to:
tail -f live_log.txtThis "follows" the file, showing new lines as they're added – perfect for watching log files in real time.
Practical Tips for Everyday Use
Here are some common scenarios and the best commands to use:
- Quick config file check:
cat /etc/hosts - Examining log files:
less /var/log/syslog - Checking recent log entries:
tail -n 50 application.log - Monitoring active logs:
tail -f /var/log/apache2/access.log
Remember that these commands work with any text file. If you try to read binary files (like images or executables), you'll see gibberish characters that might mess up your terminal display.
Combining Commands for Power
You can combine file reading commands with other tools using pipes. For example, to search for specific content:
cat config.txt | grep "database"Or count lines in a file:
cat large_file.txt | wc -lThese combinations make the terminal incredibly powerful for file analysis and system administration tasks.
What's Next
Now that you can read files in the terminal, the next logical step is learning how to create and edit them. In our next post, we'll explore text editors available in the terminal, starting with nano for beginners and touching on the more powerful vim editor.