Cat Command in Linux
Introduction
In Linux, the cat
command is one of the most commonly used commands. The cat command gets its name from its ability to concatenate files. It can read, write, and concatenate file contents to standard output. It reads from standard input if no file is supplied or if the input file name is specified as a single hyphen (-
).
The most typical uses of cat
is to show the contents of one or more text files, combine files by adding the contents of one file to the end of another, and create new files.
In this tutorial, we'll show you how to utilize the cat
command with real-world examples. We will also address a few FAQs on cat command in Linux.
cat
Command Syntax
Let's go through the basic syntax of the cat
command before we get into how to utilize it.
The utility expressions for cats are as follows:
cat [OPTIONS] [FILE_NAMES]
OPTIONS
-cat options
. Usecat --help
to view all the available options.FILE_NAMES
- A list of one or more files.
Displaying File Contents
The cat
command's most basic and widespread use is to read the contents of files.
The contents of the /etc/issue
file, for example, can be viewed on the terminal with the following command:
cat /etc/issue
Redirect Contents of File
You can redirect the output to a file instead of printing it to stdout
(on the screen).
Using the (>
) operator, the following command will duplicate the contents of file1.txt
to file2.txt
:
cat file1.txt > file2.txt
cp
command.The command will create the file2.txt
file if it does not already exist. Otherwise, the file will be overwritten.
To append the contents of file1.txt
to file2.txt
, use the (>>
) operator:
cat file1.txt >> file2.txt
If the file is not present, it will be generated, the same as before.
Print Line Numbers
Invoke cat
with the -n
option to see the contents of a file with line numbers:
cat -n /etc/lsb-release
Output
1 DISTRIB_ID=Ubuntu
2 DISTRIB_RELEASE=18.04
3 DISTRIB_CODENAME=bionic
4 DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"
Suppress Repeated Empty Lines
To remove the repeated empty output lines, use the -s
option:
cat -s file.txt
Display TAB
Characters
You may visually distinguish between tabs and spaces with the -T
option.
cat -T /etc/hosts
Output
127.0.0.1^Ilocalhost
127.0.1.1^Iubuntu1804.localdomain
The TAB characters will be represented by the ^I
.
Display End of Lines
Use the -e
parameter to see the invisible line ending character:
cat -e /etc/lsb-release
Output
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=18.04$
DISTRIB_CODENAME=bionic$
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"$
The $
symbol will appear at the end of each line.
Concatenating Files
When using the cat
command with two or more file names as inputs, the contents of the files will be concatenated. cat
reads the files in the order specified in its parameters and shows the contents of those files in the same order.
The following command, for example, will read the contents of file1.txt
and file2.txt
and display the result in the terminal:
cat file1.txt file2.txt
You can write a file by concatenating two or more text files.
Using the (>
) operator, the contents of file1.txt
and file2.txt
will be concatenated and written to a new file combinedfile.txt
:
cat file1.txt file2.txt > combinedfile.txt
The command will generate the combinedfile.txt
file if it does not already exist. Otherwise, the file will be overwritten.
Use the (>>
) operator to concatenate the contents of file1.txt
and file2.txt
and append the result to file3.txt
:
cat file1.txt file2.txt >> file3.txt
The file will be generated if it does not exist.
You can use the same arguments as in the previous section when concatenating files with cat
.
Creating Files
It's often easier to create small files with cat
than it is to open a text editor like nano, Vim, Sublime Text, or Visual Studio Code.
Use the cat
command followed by the redirection operator (>
) and the name of the file you want to create a new file. Press Enter
, type the text, and then save the file by pressing CRTL+D
.
We'll make a new file called file1.txt
in the example below:
cat > file1.txt
If there is a file named file1.txt
, it will be overwritten. To append the output to an existing file, use the ‘>>
’ operator.
cat >> file1.txt
FAQs on cat command in Linux
How do I use the "cat" command to display the contents of a file?
To display the contents of a file, simply type "cat" followed by the filename as the argument. For example, "cat file.txt" will display the contents of "file.txt" on the terminal.
Can "cat" display multiple files at once?
Yes, you can provide multiple filenames as arguments to the "cat" command, and it will display the contents of each file sequentially. For example, "cat file1.txt file2.txt" will display the contents of both files.
Is it possible to number the lines while displaying the file using "cat"?
Yes, you can use the "-n" option with "cat" to number the lines while displaying the file's contents. For example, "cat -n file.txt" will display the file with line numbers.
How can I create a new file using "cat"?
To create a new file with "cat," you can use the output redirection feature. For example, "cat > newfile.txt" allows you to type the content of the new file directly in the terminal.
Can "cat" append text to an existing file?
Yes, you can use the ">>" operator with "cat" to append text to an existing file. For example, "cat >> existingfile.txt" allows you to input additional text that will be appended to the end of the file.
Can "cat" display non-printable characters and special symbols?
Yes, you can use the "-v" or "--show-nonprinting" option with "cat" to display non-printable characters and special control symbols using their caret (^) notation.
Is "cat" suitable for reading large files?
"cat" can read and display the contents of large files, but it is not recommended for extremely large files as it loads the entire file into memory at once, which can impact performance.
Conclusion
The cat
command allows you to see, combine, and create new files.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.