3 Commands to View the Content of a File in Linux Terminal

In this tutorial, you’ll explore 3 commands to view the content of a file in the Linux Terminal. Linux is a powerful operating system that offers various tools to manage files effectively. In this blog, we will explore how to use the head, tail, and cat commands to work with files in Linux.

Introduction to head, tail, and cat commands

The head, tail, and cat commands are commonly used in Linux to manipulate files. These commands can be used to display the contents of a file, extract specific lines from a file, or concatenate multiple files into a single file.

Head Command

The head command is used to display the first few lines of a file. By default, the head command displays the first 10 lines of a file. However, you can specify the number of lines you want to display using the -n option.

Syntax:

head [options] filename

Example:

head -n 5 file.txt

In this example, the head command will display the first 5 lines of the file.txt.

Tail Command

The tail command is used to display the last few lines of a file. By default, the tail command displays the last 10 lines of a file. However, you can specify the number of lines you want to display using the -n option.

Syntax:

tail [options] filename

Example:

tail -n 5 file.txt

In this example, the tail command will display the last 5 lines of the file.txt.

Cat Command

The cat command is used to concatenate multiple files into a single file. You can also use the cat command to display the contents of a file on the terminal.

Syntax:

cat [options] filename

Example:

cat file1.txt file2.txt > combined.txt

In this example, the cat command will concatenate the contents of file1.txt and file2.txt and save the result in combined.txt. The > operator is used to redirect the output of the command to a file.

Conclusion

These are the 3 commands to view the content of a file in Linux Terminal. The head, tail, and cat commands are powerful tools for managing files in Linux. With these commands, you can display the contents of a file, extract specific lines from a file, or concatenate multiple files into a single file. By mastering these commands, you can become more efficient and productive when working with files in Linux, see the man page for the head, tail, and cat commands in Linux.

I hope this article has been helpful in understanding the use of head, tail, and cat commands in Linux. Thank you for reading!

If you have any questions please leave them in the comments below

Leave a Reply

Your email address will not be published. Required fields are marked *