Basic Cat Command Examples in Linux (RPM) 13 essential usage of CAT commands in Linux

Source: Internet
Author: User

The Cat (concatenation) command is one of the more commonly used commands in Linux/unix open source systems. We can use the Cat command to create one or more files, view the contents of the file, concatenate the files and output the content to the end device or new files, this article will be an example of the Linux cat commands in some simple ways.

The cat (short for "concatenate") command is one of the most frequently used command in Linux/unix like operating systems. Cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in Terminal or files. In this article, we is going to find out handy use of the cat commands with their examples in Linux.

Read Also:learn How to use the ' cat ' and ' TAC ' (Reverse of cat Command) in Linux

Basic Linux Cat commands in 13

Basic Linux Cat Commands

General syntax format

General Syntax
Cat [OPTION] [FILE] ...
1. Display Contents of file (displays the contents of files)

In the below example, it'll show contents of/etc/passwd file.

The following example shows the contents of the/etc/passwd file.

# cat/etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinnarad:x:500:500::/home/narad:/ Bin/bash
2. View Contents of multiple files in terminal (display multiple file contents on device)

In below example, it would display contents of test and Test1 file in terminal.

The following example shows the test file and the contents of the Test1 file in the device

# Cat Test Test1hello Everybodyhi World,
3. Create a file with cat command (creating files using cat commands)

We'll create a file called Test2 file with below command.

We will create the Test2 file with the following command format

# Cat >test2

Awaits input from the user, type desired text and press Ctrl+d (Hold CTRL Key and type ' D ') to exit. The text would be written in Test2 file. You can see the content of file with following Cat command.

Enter the above command and click the Enter button to wait for the user to enter the file contents while holding down ctrl+d to exit the edit. View the contents of the file by following the command.

# cat Test2hello Everyone, how does do?
4. Use the cat command with the more & less options (using cat commands with the more and less parameters)

If file has large number of content won ' t fit in output terminal and screens scrolls up very fast, we can use Param Eters more and less with cat command as show above.

If the file has a large amount of content, the device output is not clear or the screen scroll bar is scrolling too fast, we can make the content display more controllable by using the more and less parameters.

# Cat Song.txt | more# Cat Song.txt | Less
5. Display lines Numbers in file (content line number within the document)

With-n option could see the line numbers of a file song.txt in the output terminal.

The-n parameter allows the contents of the Song.txt file to be displayed in the device with a line number

# cat-n Song.txt1  "Heal the World" 2  There's A place In3  Your Heart4 and  I Know that It's Love5 and  T His place Could6 is  Much7  brighter Than Tomorrow8 and  If you really Try9  ' ll Find there ' s No need10
   
    to Cry11 in this place you  ' ll Feel12  there ' s No hurt Or Sorrow
   
6. Display $ at the end of the file (the $ symbol at the end of the document)

In the below, you can see WITH-E option that ' $ ' was shows at the end of line and also in space showing ' $ ' if there are an Y gap between paragraphs. This options are useful to squeeze multiple lines.

as follows, the $ symbol appears at the end of the file content and at the space, using the-e parameter. This option is useful for extruding multiple rows in a row.

# cat-e Testhello Everyone, how does you do?$ $Hey, am fine. $How ' s your training going on?$$
7. Display tab Separated Lines in file (The tab delimiter is displayed in the contents of the document)

In the below output, we could see TAB space is filled up with ' ^i ' character.

In the output below, we can see that tab-delimited content is populated with ' ^i '.

# cat-t Testhello ^ieveryone, how does do? Hey, ^iam fine.^i^ihow ' s your training ^igoing on? Let's do ^isome practice in Linux.
8. Display multiple files at Once (show multiple file contents at once)

In the below example we are three files test, test1 and Test2 and able to view the contents of those file as shown above. We need to separate the file with; (semi colon).

In this example, we can view the contents of test, Test1, test2 three files at the same time, use between three files, and (semicolon) separate.

# Cat Test; Cat test1; Cat Test2this is Test filethis is test1 file. This is test2 file.
9. Use standard outputs with redirection Operator (using the redirect operator as the normal output)

We can redirect standard output of a file to a new file else existing file with ' > ' (greater than) symbol. Careful, existing contents of test1 would be overwritten by contents of test file.

We can use the greater than sign > to copy the contents of the file to a new file or to an existing file, note that existing content is overwritten when copied to an existing file.

# Cat Test > Test1
Appending standard Output with redirection Operator (append content using the redirect operator)

Appends in existing file with ' >> ' (double greater than) symbol. Here, contents of test file is appended at the end of test1 file.

Use two greater than sign >> append new content to the end of the existing content.

# Cat Test >> Test1
Redirecting standard Input with redirection Operator (content output using the redirect operator)

When you use the redirect with standard input ' < ' (less than symbol), it is use of file name Test2 as a input for a command a nd output would be is shown in a terminal.

We can use less than number <, and display the contents of the Test2 file to the device.

# Cat < Test2this is test2 file.
Redirecting multiple files contain in a single file (merging multiple file contents into one)

This would create a file called Test3 and all output would be redirected in a newly created file.

Merge the contents of test, test1, test2 into the test3 file

# Cat Test test1 test2 > Test3
Sorting Contents of multiple files in a single file (merging the contents of multiple files into one document

This would create a file test4 and output of cat command is piped to sort and result would be redirected in a newly created File.

# Cat Test Test3 test2 test1 | Sort > Test4
For example, the test file contents are: This is test document content.
Test3 file contents: This is test3 file content.
Test2 file contents: This is test2 file content.
Test1 file contents: This is test1 file content.
Final output of the Test4 file content:

This is test1 file content.
This is test2 file content.
This is test3 file content.
The is test file content.

Basic Cat Command Examples in Linux (RPM) 13 essential usage of CAT commands in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.