×

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

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

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.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

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.

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.

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.

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.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

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.

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.

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.

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.

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.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

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

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.

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.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.