Skip to main content

Command Palette

Search for a command to run...

Linux CAT command

Updated
3 min readView as Markdown
Linux CAT command

Uses of cat command.

cat(concatenate) is one of the most basic commands in Linux operation systems. every programmer should know about the cat command. It can be used for doing a lot of things as mentioned below.

1) Create a file

The command is:

$ cat > file1

output:-

A new file with the name file1 will be created. Now you can write in the new text file once you are finished writing in the file press ctrl + d to save and exit.

example:- $ cat > file1 I am Advin Suryavanshi. I am a student. I am pursuing my bachelor of technology in computer science. ctrl + d

2) To view a file

The command is:

$ cat file1

output:-

This command is used to see the content of the file with the name file1.

3) To view multiple files

The command is:

$ cat file1 file2

output:-

This command is used to see the content of the file with the name file1 and file2.

4) To view the contents of a file with line numbers.

The command is:

$ cat -n file1

output:-

It shows content with line numbers.

example: $ cat -n file1

1 I am Advin Suryavanshi.

2 I am a student.

3 I am pursuing my bachelor of technology in computer science.

5) To write in an already existing file.

The command is:

$cat >> file1

hello reader.

output:-

It Will append the text "hello reader." to the end of the file1.

so if we will now view the file using $ cat file1.

the output will be

I am Advin Suryavanshi. I am a student. I am pursuing my bachelor of technology in computer science. hello reader.

6) To display content in reverse order

The command is:

$ tac file1

output:-

hello reader.

I am pursuing my bachelor of technology in computer science.

I am a student.

I am Advin Suryavanshi.

7) To see the content of all text files in the folder.

The command is:

$ cat *.txt

output:-

It Will show the content of all text files present in the folder.

8) Copy the contents from one file to another.

The command is:

$ cat [source file] > [destination file]

output:-

content for the source file will get copied into the destination file.

example: $ cat file1 > file2

now, file2 will have the content of file1.

9) To append one file's content to the end of another.

The command is:

$ cat file1 >> file2

output:-

content of file1 is appended at the end of file2.

10) To suppress repeated empty lines

The command is:

$ cat -s file1

output:-

In case your file has a lot of repeated empty lines. This command will reduce it to a single empty line.

11) To merge multiple files into one

The command is:

$ cat "file1" "fille2" "file3" > "final_file"

output:-

The content of file1, file2, and file3 will be inserted into the final_file in the given order.

12) To highlight the end of every line.

The command is:

$ cat -E "file1"

output:-

end of every line will be highlighted.

There are some other uses of the cat command too. here I have mentioned the most common uses of the cat command.

GITHUB linkedIn YOUTUBE INSTAGRAM