cat Command in Linux with Examples

Posted in

cat Command in Linux with Examples
vinaykhatri

Vinay Khatri
Last updated on December 5, 2024

    Introduction

    Viewing the content of the file is a frequent operation while working with a Linux-based OS. Moreover, you don't always like to go to the file and open it by clicking on it since this consumes more time than simply typing 2 words and getting the content.

    In virtual machines, you cannot even open the files using GUI. The cat command assists us in viewing and managing the files through the terminal. This utility was written by Torbjorn Granlund and Richard Stallman . In this article, we will go through the description of cat commands along with a few examples of how to use them.

    What is the “cat” command in Linux?

    The cat command (short for "concatenate") is a popular command in the Linux/Unix and Apple Mac OS X operating systems. It allows us to create single or many files, read file contents, concatenate files, and redirect output to a terminal or files. It is a common Unix software that is used to concatenate and display files.

    Furthermore, the cat command displays the contents of a file on the screen. It joins FILE(s) or standard input to standard output. It reads standard input when there is no FILE or when FILE is -. You may also use the cat command to rapidly create a file. Linux cat command can read and write data from common input and output devices. It has three primary tasks for handling text files: generating, displaying, and merging them.

    The cat command is employed in order to,

    1. Show a text file on the screen
    2. Make a new text file.
    3. Read the text file
    4. Changing the file
    5. Concatenation of files

    Linux cat command can be used to send a file to a program that accepts plain text or binary data as input. When concatenating and exporting, the cat command does not delete non-text bytes. As a result, its two primary use cases are text files and certain types of binary files that are format-compatible. The syntax for the command is:

    $ cat [options] [file]

    Examples of using the “cat” command

    1. View a single file

    To view the content of a file, simply type the file name after the command

    $ cat [filename]

    2. View multiple files

    This command also allows you to view multiple files simultaneously.

    $ cat [file1] [file2]

    3. View files preceded by numbers

    $ cat -n [file]

    4. Create a file

    To create an empty file, use the below command.

    $ cat >[file] 

    5. Copy Content

    To copy the content of one file from to another, use the below syntax.

    $ cat [source] > [destination]

    6. Suppress empty lines

    To remove or suppress the empty lines which are being repeated, use the below command:

    $ cat -s [file]

    7. Append content

    To append the content of the file into the other one, use the following command.

    $ cat [source] >> [destination]

    8. Write into the file

    To write into the already existing file, use the below command.

    $ cat >> [file]

    Conclusion

    We went through the cat command in this article. This command is a frequently used utility that allows us to view content, copy content from one place to another, edit content, and other file management operations.

    People are also reading:

    Leave a Comment on this Post

    0 Comments