Agentic AI with Java: Live Cohort
LinuxLinux Fundamentals

Viewing File Contents

Creating a file is only the beginning. When working with Linux, you'll frequently need to read, inspect, and update the content of files directly from the terminal.

In this lecture, we'll learn how to use cat, tac, rev, head, and tail to work with file contents.

Writing Content with cat

The cat command is primarily used to display file contents, but it can also be used with shell redirection to write content into a file.

Writing or Overwriting a File

Use the > operator with cat:

cat > telusko.txt

You can now type content directly into the terminal:

This is Linux journey.

After this we will learn Bash shell scripting.

Press Ctrl + D when you're finished.

The > operator redirects the typed content into the file.

  • If the file doesn't exist, it is created.
  • If the file already exists, its previous content is overwritten.

Be careful with >: using it on an existing file replaces its current content.

Appending Content to a File

To add content without removing what is already there, use >>:

cat >> telusko.txt

Enter the additional content:

After which we shall focus on cloud.

Press Ctrl + D to finish.

The >> operator adds the new content at the end of the file.

Difference_Between_Operators


cat - View File Content

The simplest way to display the contents of a file is:

cat telusko.txt

It prints the complete file from the first line to the last.

For a small file, cat is convenient because you can see everything at once.

Display Content with Line Numbers

Use the -n option:

cat -n telusko.txt

Example:

     1  This is Linux journey.
     2  After this we will learn Bash shell scripting.
     3  After which we shall focus on cloud.

Line numbers are useful when discussing a particular line or checking the structure of a file.


tac - View Lines in Reverse

The tac command displays the lines of a file from bottom to top.

tac telusko.txt

If the original file contains:

Line 1
Line 2
Line 3

cat displays:

Line 1
Line 2
Line 3

while tac displays:

Line 3
Line 2
Line 1

A simple way to remember it:

tac is cat written backwards, and it reverses the order of lines.


rev - Reverse Characters

Don't confuse rev with tac.

The rev command reverses the characters within each line.

rev telusko.txt

For example:

Linux
Cloud

becomes:

xuniL
duolC

The order of the lines remains unchanged.

tac vs rev

CommandWhat it reverses
tacOrder of lines
revCharacters within each line

head - View the Beginning of a File

For a large file, displaying everything with cat may not be practical.

The head command displays the first 10 lines by default.

head telusko.txt

You can specify exactly how many lines you want:

head -n 4 telusko.txt

This displays the first four lines.

The general syntax is:

head -n <number> <file>

For example:

head -n 5 application.log

This is particularly useful when quickly checking the beginning of a log or configuration file.


tail - View the End of a File

The tail command works in the opposite direction. It displays the last 10 lines by default.

tail telusko.txt

To display a specific number of lines:

tail -n 2 telusko.txt

This shows the last two lines.

The general syntax is:

tail -n <number> <file>

For example:

tail -n 5 application.log

tail is especially useful when checking the latest entries in log files.


Quick Reference

CommandPurpose
cat fileDisplay the complete file
cat -n fileDisplay file with line numbers
cat > fileWrite/overwrite file content
cat >> fileAppend content to a file
tac fileDisplay lines in reverse order
rev fileReverse characters in each line
head fileDisplay first 10 lines
head -n N fileDisplay first N lines
tail fileDisplay last 10 lines
tail -n N fileDisplay last N lines

Written By: Muskan Garg

How is this guide?

Last updated on