View Text File Contents from Linux Command Line
In this article
This guide shows you how to read the contents of a text file directly from the Linux command line using built-in tools. You'll learn the essential commands—cat, less, more, head, and tail—and when to use each. No special software is required; these commands are available on every Linux distribution by default.
What You'll Need
- A Linux system with a terminal (command line) access.
- Basic familiarity with opening a terminal (e.g., Ctrl+Alt+T on most desktop environments).
- A text file to view. If you don't have one, you can create a test file with
echo "Hello, world!" > test.txt.
Step-by-Step
- Open a terminal. On most Linux systems, you can press
Ctrl+Alt+Tor search for "Terminal" in your applications menu. - Navigate to the directory containing your file. Use the
cdcommand:cd /path/to/directory. To see where you are, typepwd(print working directory). - List files to confirm the file exists. Type
lsand look for your file. If you see it, you're ready. - View the entire file with
cat. For short files, typecat filename.txt. This prints the whole content to the screen and returns you to the prompt. For example:cat test.txt. - View a file page by page with
less. For long files,less filename.txtis better. It shows one screenful at a time. Use the spacebar to go forward, b to go back, and q to quit. This is the recommended tool for reading logs or configuration files. - Use
moreas a simpler alternative.more filename.txtalso paginates, but with fewer navigation options (spacebar for next page, q to quit). It's older thanless, but still works. - View just the beginning of a file with
head. Typehead filename.txtto see the first 10 lines. To see a specific number of lines, usehead -n 20 filename.txtfor the first 20 lines. - View the end of a file with
tail. Typetail filename.txtto see the last 10 lines. This is useful for checking log files where the newest entries are appended at the end. Usetail -n 50 filename.txtfor the last 50 lines. - Follow a file that is being updated with
tail -f. If you're monitoring a log file that's actively being written to,tail -f filename.logwill show new lines as they appear. PressCtrl+Cto stop following. - Combine commands with pipes for more power. For example, to view the first 5 lines of a file and also search for a keyword, you can do:
head -5 filename.txt | grep "error". The pipe (|) sends the output of the first command to the second.
Common Mistakes
- Using
caton a huge file. If you runcaton a 500MB log file, it will flood your terminal and may freeze it. Uselessinstead. - Forgetting to navigate to the correct directory. If you get "No such file or directory," you're probably not in the right folder. Use
pwdto check, andcdto move. - Typing the filename incorrectly. Linux filenames are case-sensitive.
Test.txtandtest.txtare different files. - Not quoting filenames with spaces. If your file is named
my file.txt, you must quote it:cat "my file.txt"or escape the space:cat my\ file.txt.
Tips
- Use
lessfor almost everything. It's more powerful thanmoreand allows you to search within the file by pressing/and typing a term. - Use
headandtailfor quick checks. If you just need a quick peek at the start or end of a file, these are faster than opening a full pager. - Add line numbers with
cat -n.cat -n filename.txtshows line numbers, which is helpful when referencing specific lines in discussions or error reports. - Use
grepto filter content. Instead of viewing the whole file, search for a pattern:grep "error" filename.txtwill show only lines containing "error". - Use
fileto check the file type. If you're not sure a file is text, runfile filenameto see if it's ASCII text, UTF-8, or binary data.
Troubleshooting
Problem: "No such file or directory"
This means the file doesn't exist in your current directory. Check your spelling and location. Use ls to see what files are present. If you're sure the file exists elsewhere, provide the full path, e.g., cat /home/user/documents/file.txt.
Problem: The file appears to be binary or garbled text.
Some files have non-text characters (like a Word document or a compressed file). Running cat on them will output gibberish. Use file filename to check the type. If it's not plain text, you may need a different tool, like strings to extract readable text from binary files.
Problem: The terminal freezes or behaves strangely.
This often happens when you accidentally open a binary file with cat. Press Ctrl+C to break out. If that doesn't work, press Ctrl+Z to suspend the process, then type kill %1 to kill it. Always use less for unknown files.
Problem: less shows a colon prompt but doesn't respond.
You might be in command mode. Press q to quit, or h for help. If you're stuck, Ctrl+C usually exits.
Problem: tail -f doesn't show new lines.
Make sure the file is actually being written to. If it's a log file, the application may buffer output. Also, check that you have read permission on the file.
Frequently Asked Questions
Q: What is the difference between cat and less?
A: cat prints the entire file to the terminal at once. less displays it page by page, allowing you to scroll and search. For any file longer than a screen, use less.
Q: How do I view a file with line numbers?
A: Use cat -n filename for line numbers, or less -N filename when using less.
Q: Can I edit a file from the command line?
A: Yes, but the commands in this guide are for viewing only. To edit, use nano, vim, or gedit (if a GUI is available).
Q: How do I view a file that is continuously updated?
A: Use tail -f filename. This will show new lines as they are appended. Press Ctrl+C to stop.
Q: What if the file is too large and less still loads slowly?
A: For extremely large files (gigabytes), consider using less anyway, as it loads parts of the file on demand. Alternatively, use grep to search for specific content instead of viewing the whole file.
Q: Is there a way to view multiple files at once?
A: Yes, you can pass multiple files to cat: cat file1.txt file2.txt. With less, you can open multiple files and navigate with :n (next) and :p (previous).