Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Explained basic cat command and it's options with examples in Linux

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 253
    Comment on it

    This blog depicts basic commands that will help a user to learn different cat command options.Here we will learn about different options of the “cat” command in Linux, “cat” is an acronym for concatenate. We use this command most frequently in Linux. We can create multiple or single files view the contents of the file, concatenate content of one file to the another and redirect the output to the terminal. We will explain different options with different examples as below:


    Syntax: cat [option] [file]

    1. When we want to display the content of the file we will use the following command.

    Let us create a file say demo.txt in the path given below:

    username@machinename:~$ touch demo.txt

    this file is created in the home directory.Now let us write some text in the file.

    Now again we will run the below command.Now we can see all the content of

    the file demo.txt in the terminal.

    username@machinename:~$ cat demo.txt

    2. If we want to display the contents of more than one files in terminal we will run the following commnad using “cat” command.

    Let us want to display the content of two files such as text.php index.php also make sure that both the files are in the home directory. We expect that both the files are in the home directory.

    username@machinename:~$ cat text.php index.php

    on execution we can see the content of text.php first and than the content of the second file.


    3. We can create a file using “cat” or “touch” command.

    Let us create a file using cat command:

    We will create a file called test2 file with below command.

    username@machinename:~$ touch text_2.php  // this file will be created in the home directory.
    
    username@machinename:~$ cat text_1.php  // this file will be created in the home directory.
    
       
    username@machinename:~$ cd /var/www/html
    
    username@machinename:/var/www/html/$ cat > text_2.php   // text_2.php created in /var/www/html

    Now we can input some text or simplley to exit from the file press the combination of the key I mentioned “ CTRL + D”


    4. Use “cat” Command with More & Less Options

    If content in the file is very large and the content scrolls up very fast to the end of the file we use the following command

    username@machinename:/var/www/html/$ cat text_2.php | more
    
    			or
    
    username@machinename:/var/www/html/$ cat text_2.php | less

    After running the command we will see a text “--More--” or “(End)”. Keep on hitting the enter button until we are not reached the last line of the file or in case of less we can use the up and down arrow.


    5. If we want to display line numbers for each line

    We can use option “-n” with the “cat” command

    username@machinename:/var/www/html/$ cat -n text_2.php

    For example:

    1 First line

    2 Second line

    3

    4 Third line

    5

    6 Sixth line

    we can see the output in the above format.


    6. Diplay all lines with numbers except the blank.

    We can use option “-b” with the “cat” command

    username@machinename:/var/www/html/$ cat -b text_2.php

    For example:

    1 First line

    2 Second line

    3 Third line

     

    4 Fourth line

    we can see the output in the above format.


    7. Using the option “-e” with cat

    If we use -e option with cat we can see “$” sign at the end of every line and also between the two paragraphs.

    Example:
    username@machinename:/var/www/html/$ cat -e text_2.php

    OUTPUT:

    hello world, we are learning linux commands?$

    $

    Learn, cat command.$

    Learning Linux is always fun?$

    $


    8. Using the option “-s” option with cat

    We use “-s” options to remvoe the repeated blank lines in the output using below command..

    Example:

    let us say the content in file test_2.php is as below:

     

    //here we have 3 blank lines

     

     

    hello world, we are learning linux commands?

     

    //here there are 4 blank lines

     

     

    Learn, cat command.

    Learning Linux is always fun?

    username@machinename:/var/www/html/$ cat -s text_2.php

    output:

    let us say the content in file test_2.php is as below:

     

    hello world, we are learning linux commands?

     

    Learn, cat command.

    Learning Linux is always fun?


    9. Using “-T” option with “cat”

    	let us say the content in file test_2.php is as below:
    
    hello 	world, we are	 learning linux commands?
    
    		Learn, cat command.
    Learning 	Linux is always fun?
     
    username@machinename:/var/www/html/$ cat -T text_2.php
    
    output after executing the command:
    
    ^Ilet us say the content in file test_2.php is as below:
    
    hello ^Iworld, we are^I learning linux commands?
    
    ^I^ILearn, cat command.
    Learning ^ILinux is always fun?
    
    Tabs in the file will be replaced by ^I. 

    10. Using Redirection Operator

    username@machinename:/var/www/html/$ cat index.php > text_2.php

    The above command will redirect the standard output of a file into the existing file and the existing content will be overwritten.


    11. Appending Standard Output

    username@machinename:/var/www/html/$ cat index.php >> text_2.php

    The above command the appends the code from one file to other using “>>” double greater than symbol.


    12. Redirecting Standard Input

    We redirect with standard input (less than symbol '<').Here the name of the file is taken as an input command and the output will be displayed in the terminal.

    username@machinename:/var/www/html/$ cat < index.php

    13. Redirecting the contents of multiple files into a single file

    This will create a file called test3 and all output will be redirected in a newly created file.

    username@machinename:/var/www/html/$ cat demo1 demo2 demo3 > finaldemo

    Here all the content of multiple files will be redirected to a single file.


    14. Contents Sorting of Multiple Files into a Single File

    Here the finaldemo file will be created and output of cat command is piped to sort and this result will be redirected to finaldemo file.

    username@machinename:/var/www/html/$ cat demo1 demo2 demo3 | sort > finaldemo

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: