Log In

How to Use the Linux Cat Command: Tutorial and Examples

How to Use the Linux Cat Command: Tutorial and Examples
22.02.2024
Reading time: 6 min
Hostman Team
Technical writer

The cat command is a vital tool for handling files and their contents in the context of Linux and Unix-like operating systems. Cat is a shortcut for "concatenate" and is used for a number of file generation, viewing, and modification tasks. 

The basic purpose of the cat command is to join files together and show their contents on the terminal. Its usefulness goes far beyond simple concatenation, though. With its many functions, it's a useful addition to any Linux user's command-line toolkit.

Basic syntax

Using SSH, log into the server and check the basic syntax.  

Basic syntax of cat command is: 

Image11

  • cat: command itself.

  • [OPTION]: optional arguments or flags that alter the cat command's behavior. A hyphen (-) usually comes before them.

  • [FILE]: the name of the file or files for processing. 

Usage examples

Here are some of the most common examples for using the cat command in Linux: 

1. Using cat command to create new file

Use the cat command with the input redirection (>) to create a new file in Linux.

cat > filename

After executing the command, type the content to be added to the file.

Image10

Press Ctrl + D to save the content and exit. 

Example: to create a file named example.txt with some sample text, follow the command:

cat > example.txt

When you press Ctrl + D the file example.txt will be created with the content that was specified.

Image24

2. Using cat command to view file content

To view a file's contents with the cat command, provide the filename as an argument. Use syntax cat filename.

Image6

Replace filename with the file that needs to be viewed. 

Example: To view a file named example.txt, use the command cat example.txt to display the content of example.txt in the terminal.

Image5

To view multiple files in order, specify the files as additional arguments:

cat file1 file2 file3

This will show the content of file1, followed by file2 and then file3.

Image14

3. Using cat command to concatenate Linux files

Concatenate files using cat command by listing the files that need to be concatenated as an argument:

cat file1 file2 > merged_file

Image2

Example: To concatenate the contents of file1and file2 while redirecting the output to a new file named merge.file, follow the command:

cat file1 file2 >> merge_file

Image22

Multiple files can be concatenated by listing them after cat. To concatenate multiple files while maintaining the original files intact, use the append (>>) redirection operator instead of (>)

4. Using cat command to copy file content from one to another

To copy the content of one file to another, use the cat command along with redirection:

cat file1 > destination_file

Image18

Put the names of the files need to be copied to and from in lieu of file1 and destination_file, respectively.

Example: To copy the content of file1 to file2, use the command:

cat file1 > file2

Image13

Remember that if the destination file already exists, this procedure will overwrite its contents. 

To append the content without overwriting the existing content, you can use the cat command with the add (>>) redirection operator: more on it in the next chapter.  

5. Using cat command to append content of one file to the end of another file

To append the content of one file to the end of another, use the append (>>) redirection operator along with the cat command:

cat source_file >> destination_file

Image1

Replace source_file with the name of the file which content needs to be appended, and destination_file with the name of the file to which the content needs to be appended to. 

Example: To append the content of file1 to the end of file2 without overwriting its existing content, use:

cat file1 >> file2

Image12

If file2 does not exist, it will be created.

6. Using cat command to suppress repeated empty lines in the output

The -s can be used to suppress repeated empty lines in the output using the cat command. This option compresses the multiple empty lines into a single empty line:

cat -s filename

Image20

Replace filename with the name of the file to show, and hide the lines that include repeated empty lines. 

Example: example.txt file has multiple empty lines.

Image4

Use cat -s on example.txt to suppress the repeated empty lines. 

Image3

7. Using cat command to display line numbers in the output

Use the cat command along with -n to display line numbers in the output: 

cat -n filename

Example: example.txt file with the content This is line 1, This is line 2, This is line 3, This is line 4 will be displayed with line numbers by using the command:

cat -n example.txt

Image7

8. Using the cat command to number non-empty lines

Use the cat command along with grep to number the non-empty lines in the output.

cat -n filename | grep -v '^*$'

Image23

  • cat -n filename: Numbers all the lines in the file

  • grep -v '^*$': removes lines that contain spaces only or that are empty. 

Example: example.txt file has multiple empty lines.

Image4

Use cat -n example.txt | grep -v '^ *$' to display the non-empty lines with numbers.

Image9

9. Using cat command to display non-printable characters

To display non-printable characters using the cat command, use the -v or --show-nonprinting option. 

cat -v filename

Image8

Or:

cat --show-nonprinting filename

Image21

Replace filename with the name of the file to display, including non-printable characters. 

10. Using the cat command to highlight the end of line 

There is no direct option to highlight the end of lines using the cat command. This can be accomplished by appending a special character to each line using the sed command, and using the cat to show the file.

cat -s/$/→/’ filename | cat -e

Image16

  • sed 's/$/→/' filename: This command uses sed to substitute (s) the end of each line ($) with , or any other preferred character.

  • cat -e: Shows the content of the file, with special character ($ by default) at the end of each line. 

Example: example.txt with content This is line 1, This is line 2 and This is line 3

Image15

Using the command sed 's/$/→/' example.txt | cat -e will show the content with the end of each line highlighted.

Image25

The output indicates the end of each line as →$, where $ is the end of the line marker from cat -e and is the character inserted by sed. The character can be altered per the user’s preference.

Conclusion

Being proficient with the cat command helps users to be much more productive and efficient when working in the command line environment. The cat command offers crucial capabilities for manipulating text files in Linux. For operations involving text processing, such as reading file contents or concatenating files, cat is still a crucial tool in the Linux toolbox.


Share