Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

View Text File Contents from Linux Command Line

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open a terminal. On most Linux systems, you can press Ctrl+Alt+T or search for "Terminal" in your applications menu.
  2. Navigate to the directory containing your file. Use the cd command: cd /path/to/directory. To see where you are, type pwd (print working directory).
  3. List files to confirm the file exists. Type ls and look for your file. If you see it, you're ready.
  4. View the entire file with cat. For short files, type cat filename.txt. This prints the whole content to the screen and returns you to the prompt. For example: cat test.txt.
  5. View a file page by page with less. For long files, less filename.txt is 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.
  6. Use more as a simpler alternative. more filename.txt also paginates, but with fewer navigation options (spacebar for next page, q to quit). It's older than less, but still works.
  7. View just the beginning of a file with head. Type head filename.txt to see the first 10 lines. To see a specific number of lines, use head -n 20 filename.txt for the first 20 lines.
  8. View the end of a file with tail. Type tail filename.txt to see the last 10 lines. This is useful for checking log files where the newest entries are appended at the end. Use tail -n 50 filename.txt for the last 50 lines.
  9. 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.log will show new lines as they appear. Press Ctrl+C to stop following.
  10. 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 cat on a huge file. If you run cat on a 500MB log file, it will flood your terminal and may freeze it. Use less instead.
  • Forgetting to navigate to the correct directory. If you get "No such file or directory," you're probably not in the right folder. Use pwd to check, and cd to move.
  • Typing the filename incorrectly. Linux filenames are case-sensitive. Test.txt and test.txt are 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 less for almost everything. It's more powerful than more and allows you to search within the file by pressing / and typing a term.
  • Use head and tail for 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.txt shows line numbers, which is helpful when referencing specific lines in discussions or error reports.
  • Use grep to filter content. Instead of viewing the whole file, search for a pattern: grep "error" filename.txt will show only lines containing "error".
  • Use file to check the file type. If you're not sure a file is text, run file filename to 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).