How To Display The Contents Of A File In Linux : Read File Contents Using Cat Command

The contents of a file in Linux appear on your screen when you run the cat command with the filename. If you are new to Linux, learning how to display the contents of a file in Linux is one of the first skills you need. This guide covers all the common commands and tricks to view file content quickly.

Linux gives you many ways to read files. You can show the whole file at once, scroll through it page by page, or just peek at the beginning or end. Each method has its own best use case. Let’s start with the simplest one.

How To Display The Contents Of A File In Linux

The most direct way to show a file’s content is using the cat command. It prints the entire file to your terminal window. This works great for short files like configuration files or scripts.

Open your terminal and type:

cat filename.txt

Press Enter. The file content appears immediately. If the file is long, it scrolls past quickly. You might only see the last few lines. That is fine for small files but not for big ones.

You can also display multiple files at once:

cat file1.txt file2.txt

This concatenates the files and shows them one after another. The name “cat” actually comes from “concatenate.”

Using Cat With Line Numbers

Sometimes you need to see line numbers. Use the -n flag:

cat -n filename.txt

This adds a number at the start of each line. It helps when you are debugging code or referencing specific lines.

Another useful flag is -b. It numbers only non-blank lines:

cat -b filename.txt

This keeps the output cleaner if your file has many empty lines.

Using Less To Scroll Through Files

For long files, cat is not ideal. You need a pager. The less command lets you scroll up and down. It is the standard tool for reading large files.

Type:

less filename.txt

Now you see the first screen of the file. Use these keys to navigate:

  • Space bar: move forward one page
  • B: move back one page
  • Arrow keys: scroll line by line
  • G: go to the end of the file
  • g: go to the beginning
  • /text: search for “text” in the file
  • n: jump to the next search result
  • q: quit and return to the terminal

Less is very efficient. It does not load the whole file into memory. This makes it perfect for huge log files or data dumps.

Using More As A Simpler Alternative

The more command is an older pager. It works similarly but with fewer features. You can only scroll forward, not backward.

more filename.txt

Press Space to go forward. Press Enter to go down one line. Type q to quit. Most people prefer less because it is more flexible.

Viewing The Start Of A File With Head

Sometimes you only need the first few lines. The head command shows the top part of a file. By default, it displays the first 10 lines.

head filename.txt

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

head -n 20 filename.txt

This shows the first 20 lines. You can also use a negative number to show all lines except the last few:

head -n -5 filename.txt

This displays everything except the last 5 lines. It is handy when you want to skip the footer of a file.

Viewing The End Of A File With Tail

The tail command is the opposite of head. It shows the last 10 lines by default.

tail filename.txt

Use -n to change the number:

tail -n 50 filename.txt

This shows the last 50 lines. Tail is extremely useful for monitoring log files that grow over time.

Following A File In Real Time

You can watch a file as it updates. Use the -f flag:

tail -f /var/log/syslog

This keeps the terminal open and displays new lines as they are added. Press Ctrl+C to stop. This is a lifesaver for debugging server issues.

Combine -f with -n to see the last 100 lines first, then follow:

tail -n 100 -f filename.log

Using Od To Display Binary Files

Not all files are text. Binary files need a different approach. The od command dumps the file in octal, hexadecimal, or other formats.

od filename.bin

By default, it shows octal bytes. To see hexadecimal, use:

od -x filename.bin

This is useful for inspecting the raw bytes of a file. You can also see ASCII representation with -c:

od -c filename.bin

This shows printable characters and escape sequences for non-printable ones.

Using Xxd For Hex Dumps

Another tool for binary files is xxd. It creates a hex dump and can also convert back.

xxd filename.bin

This shows the offset, hex bytes, and ASCII representation side by side. It is great for low-level file analysis.

To see only the hex part without ASCII:

xxd -p filename.bin

This outputs a continuous hex string. You can also limit the number of bytes shown:

xxd -l 100 filename.bin

This shows only the first 100 bytes.

Using Strings To Extract Text From Binary Files

Binary files often contain readable text strings. The strings command extracts them.

strings filename.bin

This prints all sequences of printable characters that are at least 4 characters long. You can change the minimum length:

strings -n 6 filename.bin

This only shows strings of 6 or more characters. It is helpful for finding error messages or embedded text in executables.

Using Grep To Search Inside Files

Sometimes you do not want to see the whole file. You only want lines that contain a specific word. The grep command does this.

grep "error" filename.log

This shows every line that contains “error.” It is case-sensitive by default. Use -i for case-insensitive search:

grep -i "error" filename.log

You can also show line numbers with -n:

grep -n "error" filename.log

Combine grep with other commands using pipes. For example, to see the last 100 lines of a log and search for “warning”:

tail -n 100 filename.log | grep "warning"

This is a powerful way to filter output.

Using Awk For Advanced File Processing

Awk is a full programming language for text processing. It can display specific columns from a file.

awk '{print $1}' filename.txt

This prints the first column of each line. To print the second and third columns:

awk '{print $2, $3}' filename.txt

You can also use conditions. For example, show lines where the first column is greater than 100:

awk '$1 > 100' filename.txt

Awk is very powerful but has a steep learning curve. For simple column extraction, cut might be easier.

Using Cut To Extract Columns

The cut command extracts sections from each line. It works with delimiters like tabs or commas.

cut -d',' -f1,3 filename.csv

This splits lines by comma and shows the first and third fields. The -d flag sets the delimiter. The -f flag selects fields.

For tab-separated files, you can omit -d because tab is the default:

cut -f2 filename.tsv

This shows the second column of a tab-separated file.

Using Sed To Display Specific Lines

Sed is a stream editor. It can print specific lines from a file.

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

This prints lines 10 through 20. The -n flag suppresses automatic printing. The p command prints the specified lines.

To print only line 5:

sed -n '5p' filename.txt

You can also print every third line:

sed -n '1~3p' filename.txt

This prints lines 1, 4, 7, and so on.

Using Vim To View And Edit Files

Vim is a text editor, but you can use it just to view a file. Open the file in read-only mode:

vim -R filename.txt

Or use the view command:

view filename.txt

Inside Vim, you can scroll with arrow keys, search with /, and quit with :q. Vim is overkill for simple viewing, but it is good if you already know the editor.

Using Nano To View Files

Nano is a simpler editor. It is more beginner-friendly than Vim.

nano filename.txt

You see the file content immediately. Use arrow keys to scroll. Press Ctrl+X to exit. Nano is fine for quick peeks, but it loads the whole file into memory.

Using Redirection To Display File Content

You can also use shell redirection to show file content. The < operator feeds a file into a command.

cat < filename.txt

This does the same thing as cat filename.txt. It is not very useful alone, but you can combine it with other commands.

For example, to count the number of lines:

wc -l < filename.txt

This is slightly more efficient than wc -l filename.txt because it avoids printing the filename.

Using Process Substitution

Process substitution lets you compare two files side by side. The diff command shows differences:

diff file1.txt file2.txt

For a side-by-side view, use sdiff:

sdiff file1.txt file2.txt

This shows both files in two columns. Lines that differ are marked with a pipe symbol.

You can also use vimdiff for a graphical comparison:

vimdiff file1.txt file2.txt

This opens Vim with both files side by side. Differences are highlighted.

Using Head And Tail Together

You can combine head and tail to view a specific range of lines. For example, to see lines 50 to 60:

head -n 60 filename.txt | tail -n 11

This takes the first 60 lines, then shows the last 11 of those (lines 50 to 60). It is a common trick for extracting a middle section.

Alternatively, use sed as shown earlier. It is cleaner for this task.

Using Less With Line Numbers

You can enable line numbers in less. Press -N while inside less to toggle line numbers. Or start less with the flag:

less -N filename.txt

This shows line numbers on the left. It helps when you need to reference specific lines.

Using Cat With Tabs And Non-Printing Characters

Sometimes you need to see hidden characters like tabs. Use the -T flag to show tabs as ^I:

cat -T filename.txt

Use -E to show a dollar sign at the end of each line:

cat -E filename.txt

Combine them with -A to show all non-printing characters:

cat -A filename.txt

This is useful for debugging whitespace issues in configuration files.

Using Pr To Format File Output

The pr command formats files for printing. It adds headers, page numbers, and columns.

pr filename.txt

This shows the file with a header containing the date and filename. You can specify the number of columns:

pr -2 filename.txt

This displays the file in two columns. It is useful for viewing wide data.

Using Fold To Wrap Long Lines

Long lines can be hard to read. The fold command wraps lines at a specified width.

fold -w 80 filename.txt

This breaks lines after 80 characters. Use -s to break at spaces instead of mid-word:

fold -w 80 -s filename.txt

This is more readable for natural language text.

Using Column To Format Tabular Data

The column command formats data into aligned columns. It works well with tab-separated files.

column -t filename.tsv

This aligns columns by inserting spaces. Use -s to specify a different delimiter:

column -t -s',' filename.csv

This makes CSV files much easier to read.

Using Rev To Reverse Lines

The rev command reverses each line character by character.

rev filename.txt

This is a niche tool. It can be fun for puzzles or for reversing DNA sequences.

Using Tac To Reverse File Order

The tac command is the reverse of cat. It prints the file from the last line to the first.

tac filename.txt

This is useful for reading log files in reverse chronological order.

Using Sort To Display Sorted Content

You can sort the content of a file before displaying it. The sort command sorts lines alphabetically.

sort filename.txt

Use -n for numeric sort:

sort -n numbers.txt

Use -r for reverse order:

sort -r filename.txt