cat Command in Linux

Learn via video courses
Topics Covered

Overview

The 'cat command in Linux' is one of the most versatile and widely used commands in the Linux command-line interface. The name 'cat' is short for 'concatenate,' a term that essentially means 'to combine.' True to its name, the primary purpose of the cat command is to read, create, concatenate, and display the content of files. It's an essential tool for every Linux user to understand and master.

Syntax of cat Command in Linux

The basic syntax of the cat command in Linux is as follows:

Where:

  • cat: This is the command itself which tells the system to execute the cat command.
  • [options]: This part of the syntax can contain various options that modify the behavior of the cat command. This is optional.
  • [file_names]: This part of the syntax specifies the files that the cat command will operate on. You can specify multiple files, and the cat command will concatenate and display them in the order they are listed.

Options in cat Command in Linux

  1. -n: The '-n' option adds line numbers to the output of the cat command.

    For example -

    Output:

    In this example, the cat command outputs the contents of file.txt with each line numbered.

  2. -s: The '-s' option is used to squeeze blank lines in the output. In other words, it will convert multiple consecutive blank lines into a single blank line.

    For example -

    Output:

    In this example, if file.txt had multiple blank lines, they would be squeezed into a single blank line.

  3. -E: The '-E' option will display a '$' character at the end of each line, effectively visualizing the line ends.

    For example -

    Output:

    In this example, the cat command shows the end of each line in file.txt with a '$' symbol.

Example Usages

  • Display content of a file:

    Output:

    Explanation: This is the simplest use of the cat command in Linux. It reads and displays the content of file.txt.

  • Concatenate multiple files:

    Output:

    Explanation: In this example, the cat command concatenates the contents of file1.txt and file2.txt and displays them.

Tips

  • The cat command is often used in conjunction with redirection operators ('>', '>>') to write the output into files.

  • The cat command can also be used to create a new file by providing input from the terminal.

Advanced Use Cases of cat Command in Linux

  • Creating a new file:

    Explanation: This command creates a new file named 'newfile.txt'. The system waits for the user to input the content of the file from the terminal. After entering the text, press 'CTRL+D' to save and exit.

  • Appending content to a file:

    Explanation: This command opens the 'existingfile.txt' in append mode. Any input provided from the terminal will be appended to the existing content of the file.

  • Using cat with pipes:

    Output:

    Explanation: In this example, the cat command feeds the content of 'file.txt' into the grep command, which then filters lines containing 'World'.

Conclusion

  • The 'cat' command in Linux is a powerful and versatile command for manipulating text files.

  • It can be used to read, create, concatenate files and display their contents.

  • Various options like '-n', '-s', '-E' can modify the output of the cat command.