Smart Systems for Home

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

View Contents of File in Linux: 6 Commands

· 5 min read · Home Tech

In this article
  1. Why Linux Uses Commands for File Viewing
  2. The Basic Commands: cat, less, and more
  3. Viewing Parts of a File: head and tail
  4. Searching Within a File: grep
  5. How to Choose the Right Command
  6. Common Problems and Solutions
  7. Alternatives for Special Cases
  8. Frequently Asked Questions

To view the contents of a file in Linux, you use commands like cat, less, more, head, tail, and grep. The right choice depends on file size, whether you need to scroll, and whether you're searching for specific text.

Why Linux Uses Commands for File Viewing

Linux is a command-line-centric operating system. Unlike Windows or macOS, where double-clicking a file opens it in a graphical application, Linux servers and many desktop environments rely on terminal commands to read file contents. This design is efficient for remote administration, scripting, and automation. Commands like cat and less have existed since the early days of Unix and remain the standard tools because they are fast, lightweight, and work over any terminal connection.

The Basic Commands: cat, less, and more

cat (concatenate) is the simplest way to display a file's contents. It prints the entire file to the terminal, then exits. For short files, this is perfect. For long files, the output scrolls past, and you only see the last screenful. Example:

cat /etc/hostname

less is a pager—it shows one screenful at a time and lets you scroll up and down with the arrow keys, Page Up/Down, or the spacebar. Press q to quit. Less is ideal for large files, log files, or any file longer than a few screens. Example:

less /var/log/syslog

more is an older pager that only allows forward scrolling (spacebar) and no backward movement. It's rarely used today because less is more capable. However, more is still installed by default on most systems, and some scripts use it for simplicity.

Viewing Parts of a File: head and tail

Often you don't need the whole file—just the beginning or end. head shows the first 10 lines by default, and tail shows the last 10 lines. You can specify a different number with the -n option:

head -n 20 file.txt   # first 20 lines
tail -n 50 file.log   # last 50 lines

The tail -f option is especially useful for monitoring log files in real time. It prints the last lines and then continues to display new lines as they are appended:

tail -f /var/log/nginx/access.log

Searching Within a File: grep

grep doesn't just display a file—it searches for lines that match a pattern. This is invaluable when you're looking for a specific error, IP address, or keyword in a large file. Basic usage:

grep "error" /var/log/syslog

This prints every line containing "error". You can combine grep with other commands using pipes to filter output further:

cat access.log | grep "404" | head

This shows the first 10 lines containing "404" from the access log. Grep is case-sensitive by default; use -i for case-insensitive search.

How to Choose the Right Command

Choosing the right tool depends on what you need:

  • Short file, just want to see it: cat
  • Long file, need to scroll: less
  • First or last N lines: head or tail
  • Search for a pattern: grep
  • Monitor a growing file: tail -f
  • Binary file or mixed content: strings to extract readable text

For most everyday tasks, less is the safest default because it handles both short and long files gracefully.

Common Problems and Solutions

One common issue is trying to cat a binary file—the terminal will spew garbage and may even mess up your terminal settings. Use less or strings instead. Another issue is viewing a file that is continuously being written, like a log. cat will show a snapshot, but tail -f will follow updates. Also, if a file has very long lines, less wraps them by default, but you can toggle line wrapping with -S to see lines without wrapping. Finally, if you get a "Permission denied" error, you may need to use sudo to view files owned by root or others.

Alternatives for Special Cases

Beyond the basics, there are specialized tools:

  • nl numbers the lines while displaying the file: nl file.txt
  • od shows the file in octal/hex format for binary analysis
  • sed can print specific line ranges: sed -n '10,20p' file.txt
  • awk can print specific columns: awk '{print $1}' file.txt
  • vi/vim or nano are full editors, useful when you also need to modify the file

For viewing compressed files, use zcat (for .gz), bzcat (for .bz2), or xzcat (for .xz) to view contents without decompressing to disk.

Frequently Asked Questions

How do I view a file with line numbers?

Use nl file.txt to display the file with line numbers. Alternatively, cat -n file.txt does the same thing.

What's the difference between less and more?

less allows both forward and backward scrolling, while more only allows forward. less also loads files faster for large files because it doesn't read the entire file into memory.

How do I view a file that's too large for cat?

Use less or tail. less lets you scroll through the entire file, while tail shows only the end. If you need to search, use grep.

Can I view a file while it's being written?

Yes, tail -f will display new lines as they are appended. This is commonly used to watch log files in real time.

How do I view hidden files?

Hidden files (starting with a dot) can be viewed with the same commands; just specify the filename, e.g., cat .bashrc. To list hidden files, use ls -a.

What if I get "Permission denied" when viewing a file?

This means your user doesn't have read permission. You can try sudo cat file.txt (if you have sudo privileges) or ask the file owner to grant read permission with chmod.