Command Line Utilities for Viewing File Content in Linux
Linux is a powerful operating system with a plethora of command-line utilities that make it an excellent choice for running servers, coding, and programming. One of the essential tasks while working with files is to view their contents. There are various command-line utilities in Linux which are used for viewing file contents. In this article, we will discuss some of the popular command-line utilities for viewing file content in Linux.
1. Cat –
Cat (short for concatenate) is the simplest and one of the most commonly used command-line utilities for viewing file content in Linux. It is used to display the contents of a text file on the terminal screen. The syntax for using the Cat command is:
$ cat filename
To view more than one file, we separate the filenames using space. For example, to display the contents of two files, use the following command:
$ cat file1.txt file2.txt
2. Less –
Less is a powerful and versatile command-line utility that is used to view the contents of a file one screen at a time. It is helpful in viewing the large files, and it allows the user to scroll through the content. The syntax for using the Less command is:
$ less filename
To move to the next page, press the “Spacebar,” and to move to the previous page, press “b.”
3. Head –
The head command is used to display the beginning of a file. By default, it displays the first ten lines of a file. The syntax for using the head command is:
$ head filename
We can also display a specific number of lines using the following syntax:
$ head -n 5 filename
This command will display the first five lines of the file.
4. Tail –
The tail command is used to display the end of a file. By default, it displays the last ten lines of a file. The syntax for using the tail command is:
$ tail filename
We can also display a specific number of lines using the following syntax:
$ tail -n 5 filename
This command will display the last five lines of the file.
5. Grep –
Grep is a powerful command-line utility that is used to search for a particular string or pattern in a text file. It is helpful in finding specific information in large files. The syntax for using the Grep command is:
$ grep “pattern” filename
For example, to search for the word “Linux” in a file named “file.txt,” use the following command:
$ grep “Linux” file.txt
The above command will search for the word “Linux” in the file.txt file and display all the lines where it finds the word.
Conclusion –
These are some of the most popular command-line utilities used for viewing file content in Linux. These utilities are powerful and versatile, and they help in performing various tasks while working with files. By using these utilities, you can easily view, search and edit file content on the Linux terminal.