cat command in Linux

Collapse

Unconfigured Ad Widget

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Annie_P
    Member
    • Aug 2022
    • 88

    cat command in Linux

    Hi there, I have just started using Linux OS, can any out there tell me the use of the cat command and How to use it in the command line interface?
  • Delaney martin
    Senior Member
    • Jun 2022
    • 102

    #2
    The cat command is used to read data from a file and display its content as output. It assists in tasks such as file creation, viewing, and concatenation.

    The cat command is restricted to text files.

    Syntax: cat [OPTION]... [FILE]...

    To generate new files and append content to them using the cat command.

    To create a sample.txt file use the following command

    cat >sample.txt

    The cursor will move to a new line, add sample text there.

    For e.g: This is sample test file1.

    To write the changes to the file, press Ctrl+d.
    Likewise, you can create other files and write to them.

    To display file content:
    $cat filename
    example: $cat sample.txt
    It will display the content of a given file

    To view multiple files
    $cat filename1 filename2
    The above command will display the content of filename1 and filename2.

    Redirecting Content
    $cat source.txt > destination.txt

    When executing this command, if the destination file does not exist, it will be created. In the case of an existing file with the same name, it will be overwritten.

    To append the contents of the source file to the destination file, utilize the ">>" option in conjunction with the "cat" command.
    $cat source.txt >> destination.txt

    To display the contents of a file with line numbers, use the following command:
    $cat -n filename

    To duplicate the contents of a file into another file
    $cat [name of the file-whose-contents-is-to-be-copied] > [destination-filename]

    The above command will copy the content to the destination file

    Using the cat command, it is possible to suppress repeated empty lines in the output.
    $cat -s sample.txt
    This command will suppress the repeated empty lines in the output

    If the file contains an extensive amount of content that exceeds the terminal's capacity, use the cat command to display additional content.
    $cat filename | more
    This command will display the content that could fit in the terminal & will ask to show more.

    To merge the contents of multiple files.
    $cat file1 file2 file3 > merged_filename
    The above command will merge the content of the file in respective order & will insert that content in merged_filename.

    To display the contents of all text files within a folder, use the following command:
    $cat *.txt
    The aforementioned command will display the content of all text files located within the folder.

    Comment

    Working...
    X