How To Display Contents Of A File In Linux : View File Contents In Terminal

To display the contents of a file in Linux, use the cat command followed by the filename. This is the simplest way to view file content directly in your terminal, and it works for most text files without any extra options.

Linux offers several commands to show file contents, each with unique strengths. Whether you need to read a log file, check a configuration, or view source code, knowing these commands helps you work faster and smarter.

In this guide, you will learn multiple methods to display file contents, from basic to advanced. You will also see practical examples, common use cases, and tips to avoid mistakes.

How To Display Contents Of A File In Linux

The cat command is the most common tool for this task. It prints the entire file to the screen. But there are many other commands like less, more, head, tail, and nl that give you more control.

Using The Cat Command

cat stands for concatenate. It reads files and outputs their content. To use it, type:

cat filename.txt

This shows the whole file at once. If the file is long, the output scrolls past quickly. You can combine multiple files:

cat file1.txt file2.txt

This prints both files one after another. You can also number lines with -n:

cat -n filename.txt

This adds line numbers to the output. It is helpful for code files or logs.

Using The Less Command

less lets you scroll through files page by page. It is better for large files. Open a file with:

less filename.txt

Use arrow keys to move up and down. Press Space to go forward a page, b to go back. Press q to quit. less does not load the whole file into memory, so it works fast even with huge logs.

You can search within less by typing / followed by your search term. Press n for next match, N for previous.

Using The More Command

more is an older pager. It shows one screen at a time. Use it like this:

more filename.txt

Press Space to see the next page. Press Enter to scroll one line. Press q to quit. more has fewer features than less, but it is still useful on minimal systems.

Using The Head Command

head shows the first few lines of a file. By default, it shows 10 lines:

head filename.txt

To see a different number of lines, use -n:

head -n 20 filename.txt

This shows the first 20 lines. head is great for checking the beginning of a file quickly.

Using The Tail Command

tail shows the last lines of a file. Default is 10 lines:

tail filename.txt

To see more lines, use -n:

tail -n 30 filename.txt

tail has a special option -f for following a file as it grows. This is useful for watching log files in real time:

tail -f /var/log/syslog

Press Ctrl+C to stop following.

Using The Nl Command

nl numbers lines while displaying a file. It is similar to cat -n but with more formatting options:

nl filename.txt

You can choose numbering style, like numbering only non-empty lines:

nl -b a filename.txt

This numbers all lines including blank ones. nl is useful for code listings.

Using The Od Command

od shows file content in octal or other formats. It is good for binary files. To see a file in hexadecimal:

od -x filename.txt

This displays bytes in hex. You can also see ASCII representation with -c:

od -c filename.txt

This shows characters and escape sequences.

Using The Xxd Command

xxd creates a hex dump of a file. It is part of the vim package. Use it like this:

xxd filename.txt

This shows hex values on the left and ASCII on the right. You can revert a hex dump back to binary with -r.

Using The Strings Command

strings extracts readable text from binary files. It is useful for finding text in executables or data files:

strings filename.bin

This prints sequences of printable characters. You can set a minimum length with -n:

strings -n 5 filename.bin

This shows only strings of 5 characters or more.

Using The Rev Command

rev reverses each line of a file. It is not common but can be fun or useful for specific tasks:

rev filename.txt

This prints each line backward. It works character by character.

Using The Tac Command

tac is cat spelled backward. It prints lines in reverse order, last line first:

tac filename.txt

This is helpful for viewing logs from newest to oldest.

Using The Sort Command

sort sorts lines alphabetically or numerically. It does not display the file directly but shows sorted output:

sort filename.txt

You can reverse order with -r:

sort -r filename.txt

This is useful for organizing data before reading.

Using The Uniq Command

uniq removes duplicate adjacent lines. It works best on sorted input:

sort filename.txt | uniq

This shows unique lines. Use -c to count occurrences:

sort filename.txt | uniq -c

Using The Grep Command

grep searches for patterns in files. It displays matching lines. To find lines containing “error”:

grep "error" filename.txt

You can use regular expressions for complex searches. grep is powerful for filtering file content.

Using The Awk Command

awk is a text processing tool. It can display specific columns. To show the first column of a file:

awk '{print $1}' filename.txt

This prints the first field of each line. You can change the field separator with -F.

Using The Sed Command

sed is a stream editor. It can display parts of a file. To show lines 5 to 10:

sed -n '5,10p' filename.txt

This prints only those lines. sed is good for extracting ranges.

Using The Cut Command

cut removes sections from lines. To show characters 1 to 10 of each line:

cut -c1-10 filename.txt

You can also cut by fields with -f.

Using The Paste Command

paste merges lines from multiple files. It displays them side by side:

paste file1.txt file2.txt

This shows corresponding lines together. Use -d to change the delimiter.

Using The Column Command

column formats output into columns. It is useful for aligning data:

column -t filename.txt

This creates a table-like display. It works well with tabular data.

Using The Pr Command

pr formats files for printing. It adds headers and page breaks:

pr filename.txt

This shows the file with page numbers and dates. It is rarely used now but still available.

Using The Fold Command

fold wraps long lines to a specified width. To wrap at 80 columns:

fold -w 80 filename.txt

This makes long lines readable without scrolling horizontally.

Using The Expand And Unexpand Commands

expand converts tabs to spaces. unexpand does the opposite. To see a file with tabs as spaces:

expand filename.txt

This is helpful for viewing files with inconsistent tab stops.

Using The Hexdump Command

hexdump is another hex viewer. It is similar to xxd but with different options:

hexdump -C filename.txt

This shows a canonical hex+ASCII display.

Using The Bvi Or Bview Commands

bvi and bview are hex editors. They let you view and edit binary files. They are not installed by default but can be added.

Practical Examples

Here are some real-world scenarios:

  • Check the first 5 lines of a log: head -n 5 /var/log/syslog
  • Monitor a log in real time: tail -f /var/log/apache2/access.log
  • View a large file page by page: less /etc/nginx/nginx.conf
  • Number lines of a script: cat -n script.sh
  • Find error messages: grep "ERROR" application.log
  • Show the last 20 lines of a file: tail -n 20 data.txt
  • Reverse a file: tac log.txt
  • Extract readable strings from a binary: strings /usr/bin/ls

Common Mistakes To Avoid

Beginners often make these errors:

  • Using cat on binary files – it can mess up your terminal. Use xxd or od instead.
  • Forgetting to quit less – press q to exit.
  • Typing the filename wrong – use tab completion to avoid errors.
  • Using more when less is better – less has more features.
  • Not using quotes around search terms in grep – this can cause issues with special characters.

Tips For Efficiency

These tips will speed up your work:

  • Use less for any file over 50 lines.
  • Combine commands with pipes, like grep "error" log.txt | less.
  • Use tail -f to watch logs during debugging.
  • Alias common commands in your .bashrc file.
  • Learn regular expressions for grep and sed.

Frequently Asked Questions

What Is The Easiest Way To Display A File In Linux?

The easiest way is using the cat command. Just type cat filename and press Enter. It shows the entire file content on the screen.

How Do I View A Large File Without Scrolling?

Use the less command. It lets you scroll page by page. Press Space to go forward, b to go back, and q to quit.

Can I Display Only The First 10 Lines Of A File?

Yes, use the head command. Type head filename to see the first 10 lines. Use head -n 20 to see 20 lines.

How Do I Watch A File As It Grows?

Use tail -f filename. This shows new lines as they are added. Press Ctrl+C to stop.

What Command Shows Line Numbers In A File?

Use cat -n filename or nl filename. Both display line numbers on the left side of the output.

Now you know multiple ways to display file contents in Linux. Start with cat for small files, switch to less for large ones, and use tail -f for live logs. Practice these commands daily, and they will become second nature. If you make a typo, dont worry – just try again. The terminal is forgiving, and you can always press Ctrl+C to cancel a command. Happy file viewing!