Skip to main content

Command Palette

Search for a command to run...

How to Display Contents of a File in Linux

Published
6 min readView as Markdown

When you start working with Linux, one of the first things you’ll want to know is how to view the contents of a file. Whether you’re checking configuration files, reading logs, or just exploring text files, knowing the right commands can save you a lot of time. In this article, I’ll walk you through the most common and useful ways to display file contents in Linux.

You don’t need to be a Linux expert to follow along. I’ll explain everything in simple terms and show you examples you can try right away. By the end, you’ll feel confident opening and reading any file on your Linux system.

Basic Commands to Display File Contents

Linux offers several commands to display file contents directly in the terminal. These commands are simple but powerful, and each has its own strengths.

The cat Command

The cat command is the most straightforward way to display the entire content of a file.

  • Usage: cat filename
  • It prints the whole file content to the terminal.
  • Works best for small files because it dumps everything at once.

Example:

cat example.txt

This will show the full content of example.txt. If the file is large, the output might scroll past quickly, making it hard to read.

The less Command

less is a pager program that lets you view files one page at a time.

  • Usage: less filename
  • Allows scrolling up and down with arrow keys.
  • Supports searching inside the file.
  • Does not load the entire file into memory, so it works well with large files.

Example:

less example.txt

Press q to quit less when you’re done.

The more Command

more is similar to less but with fewer features.

  • Usage: more filename
  • Displays the file page by page.
  • You can press space to go to the next page or q to quit.

Example:

more example.txt

While more is simpler, less is generally preferred for its flexibility.

The head and tail Commands

Sometimes, you only want to see the beginning or end of a file.

  • head filename shows the first 10 lines by default.
  • tail filename shows the last 10 lines by default.

You can specify the number of lines with the -n option.

Examples:

head -n 20 example.txt   # Shows first 20 lines
tail -n 15 example.txt   # Shows last 15 lines

tail is especially useful for monitoring log files as they grow.

Viewing Files with Syntax Highlighting

If you’re working with code or configuration files, syntax highlighting can make reading easier.

Using bat

bat is a modern alternative to cat that adds syntax highlighting and line numbers.

  • Install it via your package manager (e.g., sudo apt install bat).
  • Usage: bat filename

Example:

bat example.py

This will display the Python file with colors and line numbers, improving readability.

Using highlight

highlight is another tool that converts source code to colored output.

  • Install with your package manager.
  • Usage: highlight filename

It’s useful if you want to export highlighted code to HTML or other formats.

Displaying Binary Files

Not all files are text. Sometimes you need to peek inside binary files.

Using xxd

xxd creates a hex dump of a file, showing both hexadecimal and ASCII representations.

  • Usage: xxd filename

Example:

xxd image.png | head -n 20

This shows the first 20 lines of the hex dump for image.png.

Using strings

strings extracts readable text from binary files.

  • Usage: strings filename

Example:

strings executable.bin

This can help identify embedded text or messages inside binaries.

Tips for Efficient File Viewing

Here are some practical tips to make viewing files easier:

  • Combine commands with pipes. For example, cat file | less lets you scroll through a file using cat.
  • Use tail -f filename to watch a file update in real time, great for logs.
  • Use head and tail together to view specific parts, like head -n 50 file | tail -n 20 to see lines 31-50.
  • Use wc -l filename to count lines before viewing large files.

Handling Large Files

Large files can be tricky to open because they might freeze your terminal or consume too much memory.

  • Use less instead of cat to avoid loading the entire file.
  • Use head or tail to view just a portion.
  • Use split to break large files into smaller chunks.

Example of splitting a large file into 1000-line parts:

split -l 1000 largefile.txt part_

This creates files named part_aa, part_ab, etc.

Viewing Files with GUI Tools

If you prefer graphical interfaces, Linux offers many text editors and viewers:

  • Gedit: Simple text editor for GNOME.
  • Kate: Advanced editor for KDE.
  • Mousepad: Lightweight editor for XFCE.

You can open files with these editors by typing their name followed by the filename:

gedit example.txt

These editors support syntax highlighting and easy navigation.

Summary Table of Commands

CommandPurposeBest For
catDisplay entire fileSmall files
lessScrollable file viewerLarge files
moreSimple pagerBasic paging
headShow first linesPreview file start
tailShow last linesPreview file end, logs
batSyntax-highlighted displayCode files
xxdHex dumpBinary files
stringsExtract readable textBinary files

Conclusion

Now that you know how to display the contents of a file in Linux, you can explore your system’s files with confidence. Whether you want to quickly peek at a small text file or scroll through a large log, there’s a command that fits your needs. Tools like cat, less, and tail are essential for everyday Linux use.

Remember, combining these commands with options and other tools can make your workflow even smoother. Try them out on your own files and see how easy it is to read and analyze data in Linux. With these skills, you’re well on your way to mastering the Linux command line.

FAQs

How do I view a file without opening an editor in Linux?

You can use commands like cat, less, or more to display file contents directly in the terminal without opening an editor.

What command shows the last lines of a file?

The tail command shows the last 10 lines by default. Use tail -n number filename to specify how many lines.

Can I view large files without crashing my terminal?

Yes, use less or tail -f to view large files efficiently without loading everything at once.

How do I display a file with line numbers?

Use cat -n filename or tools like bat which show line numbers by default.

Is there a way to view binary files as text?

Yes, use strings to extract readable text or xxd to see a hex dump of the binary file.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts