Cat Command in Linux

Cat command (short for concatenate) is one of the most widely used command-line utility in Linux OS. It is a standard Linux utility that is used for viewing the contents of a file without the need of opening it in any text editor. Another major usage of cat command is concatenation of files that is combining multiple files into a single file. There are other several uses of cat command in Linux about which we will talk in this article to give you an understanding of how it works in different scenarios.

Following are some basic functions of cat command:

  • Displaying contents of the file
  • Creating new files
  • Combining files
  • Copying file contents

Note that we have used Debian 10 OS for explaining the commands and procedures mentioned in this article.

Basic Syntax

The basic syntax for using the Cat command is:

$ cat [option] [filename]...

Use the above command if you are in the same directory that contains your file. Otherwise, if you are in some other directory, mention the path to that file as follows:

$ cat [option] [path/to/file]...

Display Contents of File

The most common and basic usage of cat command is displaying the file contents. In order to display the file contents to a Terminal, simply type cat and the filename as follows:

$ cat [filename]

An example of this would be to display the contents of /etc/hosts file. In that case, the command would be:

$ cat /etc/hosts

Display content of All Files

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

$ cat *

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

$ cat *.txt

Display Multiple Files at Once

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:

$ cat [file1] [file2] [file3]

An example of this would be to view the output of all the three files /etc/hostname, /etc/resolv.conf and /etc/hosts in the Terminal as shown in the following screenshot:

Copy the output of one file to another file

It can also be utilized to copy the output of one file to another file. If the destination file does not exist, it will first create it, otherwise overwrites the targeted file.

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

$ Cat [source_file] > [destination_file]

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

$ cat [testfile1] > [testfile_backup]

This command will first create the file testfile_backup and then copy the contents of testfile1 to it.

Append the output of a file to another file

Instead of overwriting the output of a targeted file in the above example, you can also make the cat command to append the output. The following syntax can be used for this purpose:

$ cat [source_file] >> [destination_file]

It will create the destination file if it does not already exist, otherwise will append the output.

Copy multiple files to another text file/ Concatenating the files

Another major use of cat command is that you can combine multiple files into a single file. Following syntax can be used to concatenate the file1, file2, and file3 and save them to another file named file4.txt.

$ cat [file1] [file2] [file3] > [file4]

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. The following command can be used for this purpose:

$ cat /etc/hostname /etc/resolv.conf /etc/hosts > network.txt

Display Line Numbers in File

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

$ cat -n [filename]

For instance, if you are viewing a file containing the list of items, then you can use the –n flag to display those items with a number. Remember that empty lines are also numbered as shown in the following screenshot:

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

$ cat –b file.txt

Create a File

You can also create a file using the cat command. The following syntax can be used for thi spurpose:

$ cat > [filename]

After entering the above command, enter the text you want to store in the file. Once done, use Ctrl+D to save and exit. After that, you can view the contents of your newly created file by executing the following command in Terminal:

$ cat [filename]

Sorting output

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

$ cat [filename] | sort

Similarly, in case of multiple files, you can concatenate the output into one file in an alphabetical order:

$ cat [file1] [file2] | sort > [file3]

Remove consecutive empty lines

Sometimes the file contains consecutive empty lines that you do not want to print. 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:

$ cat –s [filename]

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

By using the –s flag, you will see the difference.

Display tab characters

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

$ cat –t [filename]

Tabs will be shown in the output as ^I characters.

Printing output of a file

Another popular use of 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 will be used:

$ cat [filename] > /dev/lp

In this article, we have explained through various examples how you can use the cat command to manipulate the files in Linux. Cat command is popular among all users because of its simple syntax and the lot of 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.



from Linux Hint https://ift.tt/2RMp6EO

Post a Comment

0 Comments