Introduction
The cat command is one of the most useful and versatile commands in Linux. It is used to view, create, concatenate, and print files. It can also be used to combine multiple files into one, display the contents of a file, and even convert text from one format to another. In this article, we will discuss the basics of the cat command and provide 22 useful examples of how to use it.
How to Use the Cat Command in Linux [22 Useful Examples]
1. List the Contents of a Directory: The most basic use of the cat command is to display the contents of a file. To do this, simply type “cat” followed by the name of the file you want to view. For example, to view the contents of a file named “example.txt”, type “cat example.txt”.
2. Create a New File: You can also use the cat command to create a new file. To do this, type “cat >” followed by the name of the file you want to create. For example, to create a file named “example.txt”, type “cat > example.txt”.
3. Append Text to a File: You can also use the cat command to append text to an existing file. To do this, type “cat >>” followed by the name of the file you want to append to. For example, to append text to a file named “example.txt”, type “cat >> example.txt”.
4. Display the Line Numbers of a File: You can use the cat command to display the line numbers of a file. To do this, type “cat -n” followed by the name of the file you want to view. For example, to view the line numbers of a file named “example.txt”, type “cat -n example.txt”.
5. Display the Number of Lines in a File: You can also use the cat command to display the number of lines in a file. To do this, type “cat -l” followed by the name of the file you want to view. For example, to view the number of lines in a file named “example.txt”, type “cat -l example.txt”.
6. Display the Number of Characters in a File: You can also use the cat command to display the number of characters in a file. To do this, type “cat -c” followed by the name of the file you want to view. For example, to view the number of characters in a file named “example.txt”, type “cat -c example.txt”.
7. Display the Number of Words in a File: You can also use the cat command to display the number of words in a file. To do this, type “cat -w” followed by the name of the file you want to view. For example, to view the number of words in a file named “example.txt”, type “cat -w example.txt”.
8. Display the Number of Bytes in a File: You can also use the cat command to display the number of bytes in a file. To do this, type “cat -b” followed by the name of the file you want to view. For example, to view the number of bytes in a file named “example.txt”, type “cat -b example.txt”.
9. Display the Number of Tabs in a File: You can also use the cat command to display the number of tabs in a file. To do this, type “cat -T” followed by the name of the file you want to view. For example, to view the number of tabs in a file named “example.txt”, type “cat -T example.txt”.
10. Display the Number of Non-Printable Characters in a File: You can also use the cat command to display the number of non-printable characters in a file. To do this, type “cat -v” followed by the name of the file you want to view. For example, to view the number of non-printable characters in a file named “example.txt”, type “cat -v example.txt”.
11. Display the Number of Lines in a File Without Trailing Newlines: You can also use the cat command to display the number of lines in a file without trailing newlines. To do this, type “cat -s” followed by the name of the file you want to view. For example, to view the number of lines in a file named “example.txt” without trailing newlines, type “cat -s example.txt”.
12. Display the Number of Blank Lines in a File: You can also use the cat command to display the number of blank lines in a file. To do this, type “cat -b” followed by the name of the file you want to view. For example, to view the number of blank lines in a file named “example.txt”, type “cat -b example.txt”.
13. Display the Number of Words in a File Without Trailing Spaces: You can also use the cat command to display the number of words in a file without trailing spaces. To do this, type “cat -w” followed by the name of the file you want to view. For example, to view the number of words in a file named “example.txt” without trailing spaces, type “cat -w example.txt”.
14. Display the Number of Bytes in a File Without Trailing Spaces: You can also use the cat command to display the number of bytes in a file without trailing spaces. To do this, type “cat -c” followed by the name of the file you want to view. For example, to view the number of bytes in a file named “example.txt” without trailing spaces, type “cat -c example.txt”.
15. Display the Number of Tabs in a File Without Trailing Spaces: You can also use the cat command to display the number of tabs in a file without trailing spaces. To do this, type “cat -T” followed by the name of the file you want to view. For example, to view the number of tabs in a file named “example.txt” without trailing spaces, type “cat -T example.txt”.
16. Display the Number of Non-Printable Characters in a File Without Trailing Spaces: You can also use the cat command to display the number of non-printable characters in a file without trailing spaces. To do this, type “cat -v” followed by the name of the file you want to view. For example, to view the number of non-printable characters in a file named “example.txt” without trailing spaces, type “cat -v example.txt”.
17. Display the Contents of Multiple Files: You can also use the cat command to display the contents of multiple files. To do this, type “cat” followed by the names of the files you want to view. For example, to view the contents of two files named “example1.txt” and “example2.txt”, type “cat example1.txt example2.txt”.
18. Concatenate Multiple Files: You can also use the cat command to concatenate multiple files. To do this, type “cat” followed by the names of the files you want to concatenate. For example, to concatenate two files named “example1.txt” and “example2.txt”, type “cat example1.txt example2.txt > example3.txt”.
19. Display the Contents of a File in Reverse Order: You can also use the cat command to display the contents of a file in reverse order. To do this, type “cat -r” followed by the name of the file you want to view. For example, to view the contents of a file named “example.txt” in reverse order, type “cat -r example.txt”.
20. Display the Contents of a File in Hexadecimal Format: You can also use the cat command to display the contents of a file in hexadecimal format. To do this, type “cat -x” followed by the name of the file you want to view. For example, to view the contents of a file named “example.txt” in hexadecimal format, type “cat -x example.txt”.
21. Display the Contents of a File in Octal Format: You can also use the cat command to display the contents of a file in octal format. To do this, type “cat -o” followed by the name of the file you want to view. For example, to view the contents of a file named “example.txt” in octal format, type “cat -o example.txt”.
22. Display the Contents of a File in Binary Format: You can also use the cat command to display the contents of a file in binary format. To do this, type “cat -b” followed by the name of the file you want to view. For example, to view the contents of a file named “example.txt” in binary format, type “cat -b example.txt”.
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.
In this article, we are going to find out the handy use of cat commands with their examples in Linux.
Cat Command Syntax
The cat command can accept multiple options and file name arguments as shown:
$ cat [OPTION]... [FILE]...
Let’s understand the above syntax:
- [OPTION] – Users can provide multiple options to alter the behavior of the command. The options start with a hyphen
("-")
, such as"-E"
is used to display line ends and"-n"
to display numbers before lines. - [FILE] – The file argument specifies the file which will be manipulated by the command. However, users can provide names of multiple files separated by space.
Note: To read in detail about all the available options of the cat command, execute the "cat --help"
command in your Linux terminal:
$ cat --help
Let’s explore different examples to harness the power of cat command.
1. Display Contents of a File in Linux
The basic functionality of the cat command is to display the content of an existing file in Linux. For that purpose, provide the name of the file with no option as shown.
$ cat Documents/tecmint1.txt
Here in the command, the content of the file “tecmint1.txt” which is located in the “Documents” directory will display.
2. Display Contents of Multiple Files in Linux
The cat command can also be utilized to show the contents of more than one file by providing the file names separated by space as shown:
$ cat tecmint1.txt tecmint2.txt
In the above output, we can see the contents of both files in the terminal. The first two lines are of file “tecmint1.txt”, whereas the last line of the output is the content of the “tecmint2.txt” file.
3. Create a File with Cat Command
The user can create a new file and save content in it with the ">"
symbol (known as the “output redirection operator”) will redirect the output of the command to the file specified by the filename “Tecmint_tutorial.txt” as shown.
$ cat > Tecmint_tutorial.txt
Note: If you want to create a new file, be careful that a file with the same name does not exist already. Otherwise, the command will overwrite the content of the existing file.
After executing the command, an indicator will blink in the new line. Write the content for the file and press the “CTRL + D”
keys to save and exit the file:
You can verify the file’s creation by using the ls command and use the cat command to view the content of the newly created file:
$ ls $ cat Tecmint_tutorial.txt
4. Append Text to a File in Linux
One of the benefits of the cat command is that it can append the content to an existing file using the ">>"
symbol (known as “append redirection operator”) will append/combine the additional content to an existing file “Tecmint_tutorial.txt“.
$ cat >> Tecmint_tutorial.txt
Type or paste the content that you want to append to this file and press the "CTRL + D"
keys:
Now let’s verify if the content has been appended to the existing contents stored in the file:
$ cat Tecmint_tutorial.txt
5. Copy File Content to Another File in Linux
Sometimes, the user wants to create a copy of the contents stored in a file into a new file for different purposes, such as backup. Here, ">"
operator will read the content of the “Tecmint_tutorial.txt” file sequentially and will place it into a new file named “New_file.txt“.
$ cat Tecmint_tutorial.txt > New_file.txt
The next step is that you verify if the new file has been created successfully by running:
$ ls $ cat New_file.txt
6. Append Contents of Multiple Files Into One File on Linux
As mentioned earlier, the cat command can be utilized for concatenation purposes. Let’s run the command to concatenate/merge contents of the “tecmint1.txt” and “tecmint2.txt” files and store the result in a new file named “cat_tecmint.txt”:
$ cat tecmint1.txt tecmint2.txt > cat_tecmint.txt
The above command will read the content of the “tecmint1.txt” and “tecmint2.txt” files and will write them in a new file “cat_tecmint.txt”.
Moving forward, we need to verify whether the new file stores the concatenated content of both files or not:
$ ls $ cat cat_tecmint.txt
7. View File Content with Line Endings
The user can also use the "-E"
option to view the EOL (End of Line) character in the contents of the file. The EOL characters are known as non-printing characters and they are represented by the dollar ("$")
symbol.
$ cat -E cat_tecmint.txt
The expected output will show the "$"
symbol at the end of each line of the content.
8. List Contents of All Specified File Types
The cat command can use the "*"
wildcard character to list the content of all files available in the current directory. Additionally, you can also specify any particular file type such as ".txt"
followed by a wildcard character to show the content of all “txt” files available in the directory.
$ cat *.txt
The output portrays the content of all “txt” files one after one.
9. Display Line Numbers in File
If you want to show line numbers prior to each line of the file’s content, use the "-n"
option to display the line number in the output without any changes to the original content of the file.
$ cat -n Fruits.txt
The expected output will show line numbers before each line.
10. Print Line Numbers of Multiple Files
The cat command with the "-n"
option can also work on multiple files by concatenating the contents of multiple files and adding numbers prior to each line of combined output.
$ cat -n Fruits.txt veg.txt
Here in the command, the content “Fruits.txt” and “veg.txt” files will combine, and then the "-n"
option will add line numbers at the beginning of each line of the output.
11. Show File Contents with Tab Characters
The "-T"
option can display the tab spaces characters "^I"
in the output which are known as non-printing characters.
$ cat -T tabfile
The output contains the tab characters in place of tab spaces.
12. View File Contents with More Command
Some files contain a lot of content that does not entirely fit in the output screen of the terminal. If we use the simple cat command to display the content of such files, the output does not indicate that more content is available and the user needs to scroll down to see it.
$ cat tutorial.txt
Here in the output, we can see only some of the actual content.
To resolve this issue, you can use the pipe "|"
symbol that helps in using the output of one command as the input to another command, in this case, it is the “more” command that offers page navigation at the end of the file.
$ cat tutorial.txt | more
13. View File Contents with Less Command
You can also use less commands to view the contents of a file in a scrollable and searchable manner using the keys.
$ cat tutorial.txt | less
14. Suppress Repeated Empty Lines in Output
In some cases, the user leaves repeated empty lines by mistake instead of a single empty line. However, the cat command can be utilized to suppress repeated empty lines from the content of a file with the help of the "-s"
option.
$ cat -s tutorial.txt
The output has only single empty lines, all repeated lines are suppressed successfully.
15. Append File Content to End of Another File
The cat command can append the content of a file at the end of another file by using the ">>"
symbol (known as “append redirection operator”).
$ cat Fruits.txt >> veg.txt $ cat veg.txt
The output shows that the contents of both files are appended in the “veg.txt” file.
16. Display File Contents in Reversed Order
To display the file’s content in reverse order, use the tac command, which is also known as ‘cat’ backward that displays the last line first, then the second last, and so on.
$ tac Weekdays.txt
The output displays the content of the “Weekdays.txt” in reverse order.
17. Show File Content in Binary Format
The cat command can be used in combination with the “xxd” utility along with the "-b"
option to convert the contents of the file into the binary format.
$ cat Weekdays.txt | xxd -b
Let’s break down the above command:
|
– The pipe symbol(|)
will give the output of the cat command to the command (xxd -b).- xxd – It is a utility that will convert content into a hexadecimal representation.
-b
– This option is used with xxd to specify the binary output format instead of the default hexadecimal format.
The output portrays the file’s content in binary format and the original format side by side.
18. Show File Content in Hexadecimal Format
To convert the content of a file into the hexadecimal format, the user can utilize the “hexdump” utility as shown below:
$ cat Weekdays.txt | hexdump -C
Here in the command, the pipe symbol is joining both commands whereas the “hexdump” command will convert the content into hexadecimal format. Additionally, the "-C"
option will show the ASCII representation alongside the hexadecimal values.
The output shows the converted content of the file in hexadecimal format successfully.
19. Display Specific Lines of File in Linux
The cat command can be combined with the sed command to display a specific range of lines from a file that matches the defined pattern from the file.
$ cat Weekdays.txt | sed -n '3,6p'
Here in the above command, the sed command will get the output of the cat command as input with the help of the pipe symbol. Then the sed command with the option "-n"
and pattern “3,6p” will print lines 3 to 6 from that input.
20. Sort the Contents of File Alphabetically
The user can utilize the cat command with the sort command to alphabetically sort the lines of content as shown.
$ cat -v veg.txt | sort
The output displays the alphabetically sorted content of the file.
21. Display End of File Marker in File
The cat command can be utilized with the “here document” which aids the user to input the content into the file and set the page end marker.
The “here document” is denoted by the "<<"
symbol followed by a delimiter "EOF"
, which allows the user to input multiple lines of text directly from the terminal and save them into a file.
The input process is terminated by entering the specified delimiter on a new line.
$ cat > month.txt << EOF
The output saved the input in the file “month.txt” and terminated the input process when “EOF” was entered.
22. View CPU Info in Linux
The cat command can also display the content of a virtual file named “cpuinfo”, which contains information about the CPU processor, model name, cache size, number of cores, and other details of the CPU.
$ cat /proc/cpuinfo
That is it for this blog, I hope you understood the usage of the cat command in Linux. You may refer man page of the cat command if you want to know more options.
$ man cat
Conclusion
This article displayed 20 examples of the cat command in Linux to concatenate, display, and create files. Additionally, the cat command can be utilized with other Linux commands to perform more advanced operations like sorting content or converting the content into other file formats.
How to Use the Cat Command in Linux [22 Useful Examples]
The cat command is one of the most frequently used commands in Linux. It is a simple yet powerful command that allows you to view, create, and concatenate files. In this article, we will discuss the basics of the cat command and explain some of its most useful examples.
What is the Cat Command?
The cat command is a command-line utility for viewing, creating, and concatenating files. It stands for “concatenate” and is used to display the contents of a file or multiple files. It is one of the most basic and frequently used commands in Linux.
Syntax of the Cat Command
The syntax of the cat command is as follows:
cat [options] [file_name]
The options are used to specify the behavior of the command. The file_name is the name of the file that you want to view or create.
Examples of the Cat Command
Now that we have discussed the basics of the cat command, let’s look at some examples of how to use it.
1. View the Contents of a File
To view the contents of a file, use the following command:
cat file_name
2. Create a New File
To create a new file, use the following command:
cat > file_name
This will create a new file with the specified name. You can then enter the contents of the file and press Ctrl+D to save it.
3. Concatenate Two Files
To concatenate two files, use the following command:
cat file1 file2 > file3
This will create a new file (file3) that contains the contents of both file1 and file2.
4. Display Line Numbers
To display line numbers when viewing a file, use the following command:
cat -n file_name
5. Display Tabs as Characters
To display tabs as characters when viewing a file, use the following command:
cat -T file_name
6. Display Non-Printing Characters
To display non-printing characters when viewing a file, use the following command:
cat -v file_name
7. Display End of Line Characters
To display end of line characters when viewing a file, use the following command:
cat -e file_name
8. Display the Number of Newlines
To display the number of newlines in a file, use the following command:
cat -b file_name
9. Display the Number of Bytes
To display the number of bytes in a file, use the following command:
cat -c file_name
10. Display the Number of Words
To display the number of words in a file, use the following command:
cat -w file_name
11. Display the Number of Characters
To display the number of characters in a file, use the following command:
cat -m file_name
12. Display the Number of Lines
To display the number of lines in a file, use the following command:
cat -l file_name
13. Display the Number of Fields
To display the number of fields in a file, use the following command:
cat -f file_name
14. Display the Number of Columns
To display the number of columns in a file, use the following command:
cat -C file_name
15. Display the Number of Pages
To display the number of pages in a file, use the following command:
cat -P file_name
16. Display the Number of Lines in a File
To display the number of lines in a file, use the following command:
cat -n file_name
17. Display the Number of Words in a File
To display the number of words in a file, use the following command:
cat -w file_name
18. Display the Number of Characters in a File
To display the number of characters in a file, use the following command:
cat -m file_name
19. Display the Number of Bytes in a File
To display the number of bytes in a file, use the following command:
cat -c file_name
20. Display the Number of Fields in a File
To display the number of fields in a file, use the following command:
cat -f file_name
21. Display the Number of Columns in a File
To display the number of columns in a file, use the following command:
cat -C file_name
22. Display the Number of Pages in a File
To display the number of pages in a file, use the following command:
cat -P file_name
Conclusion
In this article, we discussed the basics of the cat command and explained some of its most useful examples. We hope that you now have a better understanding of how to use the cat command in Linux.