File Contents in Linux

In this section, we will discuss on all about the file contents in Linux. In Linux, file contents refer to the data stored within a file. The contents of a file can vary greatly, from plain text to binary data, and they can be manipulated using various commands and utilities provided by the Linux operating system. If you want to learn about Working with Directory please click here.

Top commands in File Contents in Linux

  1. head
  2. tail
  3. cat
  4. tac
  5. more
  6. less

To understand this concept better, we are creating two files on my Linux machine named as testfile1 and testfile2. In testfile1, we have added 20 lines which are Test file line1, Test file line2 up to Test file line20. In the second file also, we have added 20 lines which are Test file line50, Test file line52 and up to Test file line70.

The head command

The head command in Linux is used to display the beginning lines of a file or the output of another command. It is particularly useful when you want to quickly examine the beginning portion of a file or review the output of a command without having to display the entire contents. By default, it displays the first 10 lines of the specified file or input. If you want to learn more on head command follow this.

Here’s the basic syntax of the head command: head [options] [file(s)] Here, options represents as flags.

In the below example of head command is showing first 10 lines of the file while we have added 20 lines in this.

File Contents in Linux

Now, lets suppose we want to see first 4 lines of multiple files, then we need to add flag here.

Syntax: head -n 4 file1 file2 file3

The tail command

The tail command in Linux is used to display the last lines of a file or the output of another command. By default, it displays the last 10 lines of the specified file or input. It is also used to view log files, monitor file changes, or quickly check the recent output of a command. It provides an efficient way to focus on the most recent or relevant content in a file or command output.

Here’s the basic syntax of the tail command: tail [options] [file(s)] Here, options represents as flag

In the below example, we can see only last 10 lines of files.

To see, last four lines of multiple files.

Syntax: tail -n 4 file1 file2 file3

The cat command

The cat command stands for concatenate. It is used to display the contents of one or more file. It is versatile command that can be used for various purposes, including viewing file contents, creating new files, appending to existing files and combining multiple files into one.

Here’s the basic syntax of the cat command: cat [options] [file(s)] Here, options represents as flag. In the below example, we will be able to see all the contents of the file.

We can also create a file and add some contents to it over the Terminal using cat command. Once we create a file, it will move to next step where it will asking to add some contents to it, after adding it press Ctrl+D key to save and exit.

Append the contents of one file to another which means copy contents of one file another.

Combining multiple files into single file. In the below example, we tried copying data from testfile7 & testfile8 to testfile9 and can see we were able to do so.

Contents in testfile9 after combining the file

The tac command

The tac command in Linux is the reverse counterpart of the cat command. It is used to concatenate and display the contents of one or more files in reverse order, with the last line appearing first and the first line appearing last. It is also useful when you want to examine the contents of a file or multiple files in reverse order, especially for log files or any other scenarios where the last entries or lines are of primary interest. It provides an easy way to quickly view the end of files without having to manually scroll or navigate through them.The name tac stands for reverse cat.

Here’s the basic syntax of the tac command: tac [options] [file(s)]

The more command

The more command in Linux is used to view the contents of a file one page at a time in the terminal. It allows you to scroll through the file and navigate forward or backward. The more command is often used for viewing large files or text documents that don’t fit within a single screen.

Here’s the basic syntax of the more command: more [options] [file] Here, options represents as flag.

The less command

The less command in Linux is similar to the more command, but it provides more advanced features and capabilities for viewing and navigating through file contents in the terminal. It allows you to scroll through a file interactively, search for specific patterns, and perform various operations on the displayed content.

Here’s the basic syntax of the less command: less [options] [file] Here, options represents as flag.

Conclusion on File Contents in Linux

So, in this section we learnt about File Contents in Linux. The understanding of file contents in Linux is crucial for effectively working with the operating system and performing various tasks. Whether it’s viewing, searching, editing, or manipulating files, having a good grasp of the tools and techniques available can greatly enhance your productivity.

Text file contents can be easily examined and modified using commands like cat, tac, less, and more, allowing you to extract specific information, filter results, and work with plain text or configuration files. On the other hand, binary file contents require specialized tools such as file, and strings for analysis, providing insights into file formats, extracting useful data, and ensuring proper handling of executable, libraries, or compressed archives.