In Linux, “cat” stands for concatenate and is a command-line utility that is used to display the content of files or concatenate them. This command displays the contents of one or more files without having to open the file for editing.
Here are some basic examples of using the “cat” command:
Displaying the content of a file in linux:
# cat filename
Replace “filename” with the name of the file you want to display.
Concatenating multiple files:
# cat file1 file2
This will display the content of “file1” followed by the content of “file2” on the terminal.
Redirecting output to a new file:
# cat file1 > newfile
This will create a new file named “newfile” and copy the content of “file1” into it.
Appending content to an existing file:
# cat file2 >> file1
This will append the content of “file2” to the end of “file1”.
Displaying line numbers:
# cat -n filename
This will display the content of the file with line numbers.
Displaying non-printing characters:
# cat -v filename
This will display the content of the file with non-printing characters visible.
Displaying multiple files with line breaks:
# cat -s file1 file2
This will display the content of “file1” followed by a line break and then the content of “file2”.
Displaying content with a highlighted tab character:
# cat -T filename
This will display the content of the file with tab characters (^I) highlighted.
These are some of the basic uses of the “cat” command in Linux. The command is versatile and can be combined with other commands or used in scripts for various tasks.
Displaying Tab Characters:
# cat -T filename
Use the -T
option to display tab characters with a “^I” symbol.
Squeezing Consecutive Blank Lines:
# cat -s filename
The -s
option can be used to squeeze multiple consecutive blank lines into a single line.
Redirecting Output:
# cat file1 > newfile # Redirect and create a new file
# cat file2 >> existingfile # Redirect and append to an existing file
You can redirect the output of cat
to create or modify files.
The cat (short for “concatenate“) command is one of the most frequently used commands in Linux that comes pre-installed in most Linux distribution systems and is primarily used to display the content of existing files.
Moreover, the cat command can be utilized by the user to concatenate multiple files, create new files, append content to existing files, view the content of a file, and redirect output in the terminal or files.
The cat command can also be used to format the output of the file with the help of different options, such as adding numbers before each line of the file’s content.
Additionally, it can execute in combination with other commands to perform various tasks including providing page navigation and conversion of file format to binary or hexadecimal.
It is a standard Unix program used to concatenate and display files. The cat command display file contents to a screen. Also, you can use cat command for quickly creating a file. The cat command can read and write data from standard input and output devices.
Use Cases:
- Quick File Checks: Use
cat
to quickly view the content of a file without opening a text editor. - Combining Files: Concatenate multiple files together to create a new file.
- Appending Content: Append content to an existing file.
- Text Manipulation: Use options like
-n
,-v
, and-s
for additional features like line numbering, displaying non-printing characters, and squeezing blank lines. - Piping Output: Combine
cat
with other commands using pipes (|
) for more complex operations.
While cat
is simple and useful, for more advanced file viewing or editing tasks, other commands like less
, more
, or text editors like nano
and vim
might be preferred.