Cat how to linux

Cat Command in Linux

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, concatenate, and write file contents to the standard output. If no file is specified or 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 one file’s contents to the end of another file, and create new files.

In this article, 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:

  • 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 on the terminal:

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 :

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 :

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

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

1 DISTRIB_ID=Ubuntu 2 DISTRIB_RELEASE=18.04 3 DISTRIB_CODENAME=bionic 4 DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS" 

Suppress Repeated Empty Lines #

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

Читайте также:  Synaptic package manager linux ubuntu

Display TAB characters #

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

127.0.0.1^Ilocalhost 127.0.1.1^Iubuntu1804.localdomain 

The TAB characters will be displayed as ^I .

Display End of Lines #

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

DISTRIB_ID=Ubuntu$ DISTRIB_RELEASE=18.04$ DISTRIB_CODENAME=bionic$ DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"$ 

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:

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 :

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

Conclusion #

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

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

Источник

Linux Cat Command Examples

In Linux, the “cat” is the concatenation of files which combines multiple files into a single file. There are other several uses of the cat command in Linux which we will talk in this article to give you an understanding of how it works in different scenarios.

  • How Does the Cat Command Work?
  • Display the Content of All Files
  • Display Multiple Files at Once
  • Copy the Output of One File to Another File
  • Append the Output of a File to Another File
  • Display the Line Numbers in the File
  • Create New Files
  • Sort the Output
  • Remove the Consecutive Empty Lines
  • Display the Tab Characters
  • Print the Output of a File
Читайте также:  Yandex linux mirror ubuntu

Let’s start the article with the cat command.

How Does the Cat Command Work?

Using the “cat” command, you can create a file, view the file content, concatenate the files, and redirect the file output. The syntax of this command as follows:

Use the previous command if you are present in the same directory. Otherwise, mention the path to that file as follows:

Different options of the cat command are listed in the following:

Options Description
-n To display the line number of file content
-T To display the tab-separated characters in a line-e
-e To display the “$” at the end of lines
-s You can omit the empty lines from the output.
-a To display all the file content

To explore more options, use the following “help” utility:

Example 1: Display the Content of All Files

The common usage of the cat command is displaying the file contents. To display the file contents to a Terminal, simply type “cat” and the filename as follows:

To display all the files in a current directory, use the wildcard character with the cat command as follows:

To display only the contents of the text files in a directory, enter the following command:

Example 2: Display Multiple Files

You can also combine and display the contents of multiple files together in the Terminal using the cat command. To display multiple files simultaneously, use the following syntax:

Example 3: Copy the Output of One File to Another

It can also be utilized to copy the output of one file to another file. If not found, it first creates it. Otherwise, it overwrites the targeted file.

To copy the output of a source file to another file, use the following syntax:

An example of this is to copy the output of a testfile1 to another file named testfile_backup as follows:

This command first creates the testfile_backup file and then copies the contents of testfile1 to it.

Example 4: Append the Output of a File to Another File

Instead of overwriting the output of a targeted file in the pervious example, you can also make the cat command to append the output:

It creates the destination file if it does not already exist. Otherwise, it appends the output.

Copy Multiple Files to Another Text File/Concatenate the Files

The cat command combines several files into a single file. The following syntax can be used to concatenate file1, file2, and file3 and save them to another file named file4.txt:

Читайте также:  Linux chmod read all about it

For instance, we want to concatenate the output of /etc/hostname, /etc/resolv.conf, and the /etc/hosts file to another file named network.txt:

Example 5: Display the Line Numbers in File

To display the line numbers to the output of a file, simply use the -n flag as follows:

For instance, if you are viewing a file which contains the list of items, you can use the -n flag to display those items with a number. Remember that empty lines are also numbered as follows:

If you do not want to number the empty lines, use the -b flag as follows:

Example 6: Create a File

To create a file through the cat command, the following syntax can be used for the purpose:

After entering the previous command, enter the text that you want to store in the file. Once done, save and exit. After that, you can view the contents of your newly created file by executing the following command in the terminal:

Example 7: Sort the Output

You can also combine the sort with the cat command to sort the output alphabetically as follows:

Example 8: Remove the Consecutive Empty Lines

Sometimes, the file contains consecutive empty lines that you do not want to print. The cat command allows merging those consecutive empty lines and shows them as one empty line:

Use the following command syntax to remove the repeated empty lines:

For instance, we have the following file with consecutive empty lines:

Example 9: Display the Tab Characters

Sometimes, you have to remove the tabs from your files. Cat command can help you to find the tabs on your file using the -t flag as follows:

Example 10: Print the Output of a File

Another popular use of the cat command is in the printing contents of a document. For instance, to print the output of a file to a printing device named /dev/lp, the following syntax is used:

Conclusion

In this article, we explained through various examples on how you can use the cat command to manipulate the files in Linux. The cat command is popular among all users because of its simple syntax and the various options that it provides. Creating and viewing a file, merging, copying, and appending the file contents, printing, and a lot more can be handled with this single cat command.

About the author

Syed Minhal Abbas

I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.

Источник

Оцените статью
Adblock
detector