leafleafDocy banner shape 01Docy banner shape 02Flower illustration
Other

Linux Cat Command Usage with Examples

What is Cat Command?

The cat command (short for ‘concatenate‘) is one of the most frequently used command in Linux/Unix, Apple Mac OS X 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. It is a standard Unix program used to concatenate and display files. The cat command display file contents to a screen. Cat command concatenate FILE(s), or standard input, to standard output. With no FILE, or when FILE is -, it reads standard input. Also, you can use cat command for quickly creating a file. The cat command can read and write data from standard input and output devices. It has three main functions related to manipulating text files: creating them, displaying them, and combining them.

The cat command is used for:

  • Display text file on screen
  • Create a new text file
  • Read text file
  • Modifying file
  • File concatenation

The basic syntax of cat command is as follows:

$ cat filename

OR

$ cat > filename

OR

$ cat [options] filename

Cat Command Examples

1) To view a file using cat command, you can use the following command.

$ cat filename

2) You can create a new file with the name file1.txt using the following cat command and you can type the text you want to insert in the file. Make sure you type ‘Ctrl-d’ at the end to save the file.

$ cat > file1.txt

This is my new file in Linux.

The cat command is very useful.

Thanks

Now you can display the contents of the file file1.txt by using the following command.

$ cat file1.txt

This is my new file in Linux.

The cat command is very useful.

Thanks

3) To create two sample files and you need to concatenate them, use the following command.

$ cat smaple1.txt

This is my first sample text file

$ cat sample2.txt

This is my second sample text file

Now you can concatenate these two files and can save to another file named sample3.txt. For this, use the below given command.

$ cat sample1.txt sample2.txt > sample3.txt

$ cat sample3.txt

This is my first sample text file

This is my second sample text file

4) To display contents of all txt files, use the following command.

$ cat *.txt

This is my first sample text file

This is my second sample text file

5) To display the contents of a file with line number, use the following command.

$ cat -n file1.txt

1 This is my new file in Linux.

2 The cat command is very useful.

3 Thanks

6) To copy the content of one file to another file, you can use the greater than ‘>’ symbol with the cat command.

$ cat file2.txt> file1.txt

7) To append the contents of one file to another, you can use the double greater than ‘>>’ symbol with the cat command.

$ cat sample1.txt >> sample2.txt

If you need any further assistance please contact our support department.

Share this Doc

Linux Cat Command Usage with Examples

Or copy link

Table Of Contents