GO BACK TO BLOG'S MAIN PAGE

Cat Command in Linux  

Posted on

Article illustration

The cat command is a commonly used tool in Linux, and it is included in most Linux distribution systems. It is primarily used to display the contents of existing files.

If you have experience with Linux, you have likely encountered a code snippet that utilizes the cat command. Concatenate is the full term for cat. This command allows you to view the contents of one or more files without opening them for editing.

In addition, the cat command is a versatile tool that allows users to perform various operations such as combining multiple files, creating new files, adding content to existing files, viewing file contents, and redirecting output in the terminal or files.

One can utilize the cat command to format the output of the file by employing various options, like including numbers before each line of the file’s content.

In addition, it can work alongside other commands to carry out different tasks, such as enabling page navigation and converting file formats to binary or hexadecimal.

Discover the ins and outs of actively using the cat command in Linux.

Requirements

  • An operating system running Linux
  • How to access a terminal window or command line Syntax for using the cat command

cat Command Syntax

To make use of the cat command, adhere to the specified format:

cat [options] filename(s)

[options] – These instructions allow for additional functionality with the cat command. As an example, you can use the -n option to display the contents of a file with each line numbered.

cat –n filename

filename(s) – Specify the name of the file (or files) that you want to display. If multiple filenames are used, each file will be displayed.

Examples of Using the Linux Cat Command

This article provides 21 cat commands along with examples demonstrating their usage. To experiment with the commands, generate a few example files, and evaluate the cat commands provided below.

1. Start by creating a new file.

Creating new files and adding content to them is possible with the cat command.

Generate test1.txt and test2.txt as sample files for testing the other commands.

1. Begin by opening a terminal window and creating the initial file:

cat >test1.txt

2. The cursor will move to a new line where you can input the desired text. Compose a straightforward sentence, for example:

This is test file #1.

3. To exit the prompt and save the changes to the file, use the Ctrl key and press d.

Use the cat command to create a new file.

4. Follow the same steps to generate test2.txt. Execute:

cat >test2.txt

5. Enter the following:

This is test file #2.

6. Use the keyboard shortcut Ctrl+d.

2. Show the Contents of a Single File

To view the contents of test1.txt, use the cat command:

cat test1.txt

The output presents the content in the image provided.

3. Show the Contents of Multiple Files

To view the contents of both files, execute the following command:

cat test1.txt test2.txt

4. Redirecting the contents of a single file

Instead of displaying the contents of a file on the screen, the cat command has the ability to store them in a file.

cat test1.txt > test3.txt

If the destination filename does not exist, it will be created. If you execute the cat command on test3.txt, the contents of test1.txt should be displayed.

cat test3.txt

Here is the output:

Use the cat command to redirect file contents.

When a file is exported and it already exists, its contents will be overwritten.

cat test2.txt > test3.txt

cat test3.txt

The content of the test3.txt file has been updated:

5. Managing the Contents of Multiple Files

You have the ability to consolidate the contents of multiple files into a single file.

cat test1.txt test2.txt > test3.txt

Show the contents of test3.txt using:

cat test3.txt

The output displays the contents of both files, as shown in the image below.

6. Show the Contents in Reverse Order

The cat command has the ability to display the content of a file in reverse order, specifically by lines. For this task, utilize tac (which is essentially cat in reverse):

tac test3.txt

7. Add the contents of one file to another file.

The cat command allows for the addition of file contents to the end of another file. Instead of using a single greater-than sign, use a double greater-than sign:

cat test1.txt >> test3.txt

Please run the command to open the test3 file:

cat test3.txt

The display should show the content of test3 followed by test1.

8. Add Text to an Existing File

You can utilize a comparable command to add text to an already existing file:

cat >> test1.txt

Please include an additional line in the file:

This is the second line in test file #1.

Remember to press Ctrl and then press d.

Please review the contents of the test1.txt file:

cat test1.txt

9. Consolidate Operations

You can combine the functions of the cat command. As an illustration, to merge the output of two files and save the outcome in a fresh file:

cat test1.txt test2.txt > test4.txt

cat test4.txt

Facilitating cat operations with a single command

Alternatively, you have the option to add multiple files to the end of an existing file.

cat test2.txt test1.txt >> test4.txt

cat test4.txt

Combine several files into one by using the cat command.

Please note that the specified order is crucial as it determines the sequence in which the files are added to the destination file.

10. Additional and Reduced Choices (Handling Large Files)

If you work with a large file, using cat will result in a lengthy string of data that can be difficult to interpret. You can divide it into pages using the “| more” command:

cat test4.txt | more

This shows a single page of the file. When a key is pressed, it will automatically scroll to the next page.

If you want to have the option to navigate through the display in both directions, you can utilize the command | less.

cat test4.txt | less

11. Display Line Numbers

Having line numbers in the output can be quite helpful, especially when dealing with large files. To enable line numbering, you can include the -n option when using the cat command:

cat –n test1.txt

The output should be displayed in the image provided:

12. Display the End of Line

You can use the cat command to add a visual indicator at the end of each line and between lines with the symbol $.

To accomplish this, opt for the command:

cat -e test1.txt

Given that the sample file test1.txt contains just a single line, the resulting output will display a solitary $ character at its conclusion.

13. Display Lines Separated by TAB

The cat command can display the file content along with the tab space within the text.

To display tab-separated lines for a sample run:

cat -t test4.txt

The tab space within the text is denoted by ^I.

14. Remove any blank lines.

To remove empty lines from the output of cat using the -s option:

cat -s test4.txt

This command will reduce multiple adjacent blank lines to a single blank line in the output. If you want to completely eliminate all blank lines, you can use grep:

grep -v ‘^$’ test4.txt

This uses the grep command with the -v option to exclude lines that are completely empty, effectively removing all blank lines from the output.

15. Here is a list of all the CAT commands:

If you’re having difficulty recalling the options, you can employ the –help command:

cat ––help

16. Reversing the Order of File Contents for Display

If you want to view the contents of a file in reverse order, you can use the tac command. This command, often referred to as ‘cat’ backward, will display the last line first, followed by the second last line, and so on.

$ tac Weekdays.txt

The output shows the content of the “Weekdays.txt” in reverse order.

17. Display File Content in Binary Format

By applying the cat command in conjunction with the “xxd” utility and the “-b” option, one can convert the file’s contents into binary format.

$ cat Weekdays.txt | xxd -b

Now, let’s analyze the command provided:

  • The output of the cat command can be obtained by using the pipe symbol (|) with the command xxd -b.
  • xxd is a handy tool that can convert content into a hexadecimal representation.
  • The -b option is used with xxd to specify the binary output format instead of the default hexadecimal format.

Display File Content in Binary Format

The output displays the file’s content in binary format alongside the original format.

18. Displaying the contents of a file in hexadecimal format.

If you want to convert the content of a file into the hexadecimal format, you can use the “hexdump” utility. Here’s an example:

$ cat Weekdays.txt | hexdump -C

In this command, the pipe symbol is used to combine both commands, with the “hexdump” command converting the content into hexadecimal format. In addition, the “-C” option will display the ASCII representation next to the hexadecimal values.

The output displays the converted content of the file in hexadecimal format without any issues.

19. How to Display Specific Lines of a File in Linux

By combining the cat and sed commands, you can effectively display a specific range of lines from a file that matches a defined pattern.

$ cat Weekdays.txt | sed -n ‘3,6p’

In the above command, the sed command will receive the output of the cat command as input using the pipe symbol. Using the sed command with the option “-n” and pattern “3,6p” will allow you to print lines 3 to 6 from the given input.

20. Sort the contents of the file alphabetically.

Users can use the cat command in conjunction with the sort command to alphabetically sort the lines of content, as demonstrated.

$ cat -v veg.txt | sort

The output presents the content of the file in alphabetical order.

21. Show End of File Indicator in File

With the “here document” feature, the cat command allows users to conveniently input content into a file and specify the page end marker.

Using the “here document” feature, you can conveniently input multiple lines of text directly from the terminal and save them into a file. This is done by using the “<<” symbol followed by a delimiter “EOF”.

The input process is completed by entering the designated delimiter on a fresh line.

$ cat > month.txt << EOF

The program stored the user’s input in the file “month.txt” and stopped accepting input when the user entered “EOF”.

In summary

By now, you should have a solid grasp on utilizing the cat command in Linux.

Becoming proficient in the cat command in Linux is crucial for individuals operating in a Unix environment, as it provides a robust tool for managing files. 

With its wide range of capabilities, cat is known for its versatility. It can handle tasks as simple as viewing a file on screen or creating a new file, as well as more complex operations that involve input files, file arguments, and output devices. 

The command’s functionality is enhanced by its ability to use redirection operators, such as the > and >>, which enable the concatenation of file contents and more. 

This article has provided a comprehensive overview of the cat command, including its syntax, various options, and 21 examples of its usage. By practicing these examples, users can gain a solid understanding of how to effectively utilize the cat command in their daily Linux tasks. As a result, users will be better equipped to handle various file management scenarios and enrich their workflow in a Unix environment.

FAQs

The cat command in Linux is primarily used to display the contents of existing files without opening them for editing.

Yes, you can create new files using the cat command by redirecting the output to a new file using the ‘>’ symbol.

You can combine multiple files by specifying their names in the cat command, separated by a space, and then redirecting the output to a new file using the ‘>’ symbol.

Yes, you can add content to an existing file using the cat command by appending the output to the file using the ‘>>’ symbol.

You can display line numbers by using the ‘-n’ option with the cat command.

Yes, you can view the contents of a file in reverse order by using the ‘tac’ command, which is essentially the cat command in reverse.

You can convert the contents of a file to binary or hexadecimal format by piping the output of the cat command to the ‘xxd’ utility with the ‘-b’ or ‘-p’ option, respectively.

Yes, you can sort the contents of a file by piping the output of the cat command to the ‘sort’ command.

You can display specific lines of a file by combining the cat command with the ‘sed’ or ‘awk’ command and specifying the desired line numbers or patterns.

Yes, you can view the contents of compressed files by piping the output of the appropriate decompression command to the cat command. For example, to view the contents of a gzip-compressed file, you can use the ‘zcat’ command.

Author photo

Bohdan Pavlyuk

Pavlyuk Bohdan specializes in Kubernetes and Cloud technologies. Thus, he progressed to becoming one of our top field specialists within a matter of a few years and impressively shifting from a trainee potion to a full-trained professional DevOps. He deals with a broad range of deep-knowledge requiring areas, such as working on Linux-based infrastructure; configuration and managing databases; CI/CD platforms, Kubernetes; Helm, Docker; Python, Ansible; TCP/IP, DNS, HTTP/HTTPS, SSH. Despite his relatively young yet highly prospective age, Bohdan demonstrates impressive expertness.

What else to read on the topic