cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view the contents of a file, to combine multiple files into a single file, and to create new files.

The basic syntax of the cat command is:

cat [options] [file(s)]

The "file(s)" is the file or files that you want to display or concatenate. If no file is specified, cat will read from the standard input.

The most basic use of the cat command is to display the contents of a single file. For example, to display the contents of a file called "fruits.txt", you would use the following command:

cat fruits.txt

This will display the contents of the "fruits.txt" file on the terminal.

The cat command also has the ability to concatenate multiple files into a single file. The following command will concatenate the contents of fruits1.txt, fruits2.txt and fruits3.txt into a single file called allfruits.txt:

cat fruits1.txt fruits2.txt fruits3.txt > allfruits.txt

The cat command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -n: display line numbers for each line of the input file.
  • -b: number non-blank lines, similarly to the -n option.
  • -s: squeeze multiple consecutive blank lines into one.
  • -A: equivalent to -vET
  • -E: display a "$" at the end of each line, which can be useful for identifying line endings.
  • -T: display TAB characters as "^I".
  • -v: display non-printing characters, such as control characters, using the "^" notation.

The cat command can also be used in combination with other commands to perform more complex tasks. For example, you can use the cat command in combination with the grep command to display only the lines of a file that match a certain pattern.

cat fruits.txt | grep "apple"

This command will display the contents of fruits.txt, but it will only show the lines that contain the word "apple".

The cat command can also be used to create new files. For example, the following command will create a new file called "newfruits.txt" and add the text "apple, banana, orange" to it:

echo "apple, banana, orange" > newfruits.txt

The cat command also has the ability to append text to an existing file using the ">>" operator. For example, the following command will add the text "grape" to the end of the fruits.txt file:

echo "grape" >> fruits.txt

Another useful feature of the cat command is the ability to view multiple files at once using the command "cat -n file1 file2 > combinedfile". This command will create a new file called combinedfile and will concatenate the contents of file1 and file2 into it, while also displaying the line numbers of each file.

The cat command can also be used to view the contents of a file in reverse order by using the "tac" command. The "tac" command is the reverse of the "cat" command, it will display the contents of a file in reverse order, starting with the last line.

The cat command can also be used to convert between different file formats. For example, the following command can be used to convert a file in Windows format to a file in Linux format:

cat file.txt | tr -d '\r' > newfile.txt

The cat command can also be used to display the contents of a file in hexadecimal format using the "od" command. The "od" command stands for "octal dump" and it can be used to display the contents of a file in various formats, including hexadecimal.

od -x file.txt

In addition to displaying the contents of a file, the cat command can also be used to modify the contents of a file by redirecting the output of a command to a file. For example, the following command can be used to change all occurrences of the word "apple" to "banana" in a file called fruits.txt:

sed 's/apple/banana/g' fruits.txt > newfruits.txt

In summary, the cat command in Linux is a simple and useful command that is widely used in Linux to view the contents of files, combine multiple files into a single file, and create new files. It has several options that can be used to modify its behavior, such as displaying line numbers, squeezing multiple blank lines into one, and displaying non-printing characters. The cat command can also be used in combination with other commands to perform more complex tasks, such as displaying only the lines of a file that match a certain pattern, converting between file formats, and modifying the contents of a file. It is an essential command for anyone who works with text data in Linux.