Cat Command in Linux

superior_hosting_service

Cat Command in Linux with Example

how-to-use-the-cat-command-in-linux.png

The cat command is one of the most widely used commands in Linux. The name of the cat command comes from its functionality to concatenate files. It can read and concatenate files, writing their contents to the standard output. If no file is specified or if the input file name is specified as a single hyphen (-) it reads from the standard input.

Cat is most commonly used to display the contents of one or multiple text files, combine files by appending the contents of one file to the end of another file, and create new files.

In this tutorial, we will show you how to use the cat command through practical examples.

Cat Command Syntax

Before going into how to use the cat command, let’s start by reviewing the basic syntax.

The cat utility expressions take the following form:

cat [OPTIONS] [FILE_NAMES]
  • OPTIONS – cat options . Use cat --help to view all available options.
  • FILE_NAMES – Zero or more file names.

Displaying File Contents

The most basic and common usage of the cat command is to read the contents of files.

For example, the following command will display the contents of the /etc/issue file in the terminal:

$ cat /etc/issue
cat_command.png

Redirect Contents of File

Instead of displaying the output to stdout (on the screen) you can redirect it to a file.

The following command will copy the contents of file1.txt to file2.txt using the (>) operator :

$ cat file1.txt > file2.txt

Normally you would use the cp command to copy a file.

If the file2.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file.

Use the (>>) operator to append the contents of file1.txt to file2.txt :

$ cat file1.txt >> file2.txt

Same as before, if the file is not present, it will be created.

To display contents of a file with line numbers, use the -n option:

$ cat -n /etc/lsb-release
OUTPUT
1	DISTRIB_ID=LinuxMint
2	DISTRIB_RELEASE=18
3	DISTRIB_CODENAME=sarah
4	DISTRIB_DESCRIPTION="Linux Mint 18 Sarah"

Suppress Repeated Empty Lines

Use the -s option to omit the repeated empty output lines:

$ cat -s file.txt

Display TAB characters

The -T option allows you to visually distinguish between tabs and spaces.

$ cat -T /etc/hosts
OUTPUT
127.0.0.1^Ilocalhost
127.0.1.1^Iroot

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

The TAB characters will be displayed as ^I.

Display End of Lines

To display the invisible line ending character use the -e argument:

$ cat -e /etc/lsb-release
OUTPUT
DISTRIB_ID=LinuxMint$
DISTRIB_RELEASE=18$
DISTRIB_CODENAME=sarah$
DISTRIB_DESCRIPTION="Linux Mint 18 Sarah"$

The Line endings will be displayed as $.

Concatenating Files

When passing two or more file names as arguments to the cat command the contents of the files will be concatenated. cat reads the files in the sequence given in its arguments and displays the file’s contents in the same sequence.

For example, the following command will read the contents of file1.txt and file2.txt and display the result in the terminal:

$ cat file1.txt file2.txt

You can concatenate two or more text files and write them to a file.

The following command will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the (>) operator :

$ cat file1.txt file2.txt > combinedfile.txt

If the combinedfile.txt file doesn’t exist, the command will create it. Otherwise, it will overwrite the file.

To concatenate the contents of file1.txt and file2.txt and append the result to file3.txt to use the (>>) operator:

$ cat file1.txt file2.txt >> file3.txt

If the file is not present, it will be created.

When concatenating files with cat, you can use the same arguments as shown in the previous section.

Creating Files

Creating small files with cat it often easier than opening a text editor such as nano , Vim, Sublime Text or Visual Studio Code .

To create a new file, use the cat command followed by the redirection operator (>) and the name of the file you want to create. Press Enter, type the text and once you are done, press the CRTL+D to save the file.

In the following example, we are creating a new file named file1.txt:

$ cat > file1.txt

If a file named file1.txt is present, it will be overwritten. Use the ‘>>’ operator to append the output to an existing file.

cat >> file1.txt

Conclusion

The cat command can display, combine and create new file.

If you have any questions or feedback, feel free to leave a comment.