×

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

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 minutes read.

apt-get Command in Linux with Examples

The apt-get command is a command-line tool that is used to handle packages in Linux. This command handles the processes like retrieving the packages and information from authentic sources for...

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

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

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

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

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

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.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

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.

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.

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.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.