×

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.


Related Topics

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

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...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.