How to use cat command in Linux

0
119

cat (concatenate) is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files. Cat is very popular command which is used in every Linux Operating System. In this article, we will use different cat example which may helpful for you.

Cat Syntax

cat [OPTION] [FILE]

View a file Content

So using below command I will show content of /etc/selinux/config file

cat  /etc/selinux/config

View multiple file content

If you want to see multiple files content, simply use following command.

cat file1 file2 file3 

In result you will see all three files content respectively on screen.

Create a File with Cat Command

cat > test

After that, you can type your desired text into the file and press CTRL+d to exit from file. sign “>” used to add text.

Now you can see the added text into test file using below command.

cat test

Note: if you have an existing file and add text in it using “>” sign, it will overwrite previous content with new, it mean you will lose your data. for clearly below is an example.

In above scenario we have created a test file and add text in it.

Now add more text in test file.

cat > test

After adding text, press CTRL+d to make changes and exit.

Now again see “test” file and you will see that previous text is removed and new is placed in in.

cat test

Append text in a file

Now if you want to add text in a file without losing existing content use “>>” signs in command.

cat >> test

Now see its content and you will see data is appended instead of overwritten.

cat test

Copy multiple files content in a single file.

If you want to copy multiple files data in a single file, simply use following command

cat test test1 > test2

That’s it, you have learned that how to use cat command in various ways. See manual using command “man cat” for more information.

LEAVE A REPLY

Please enter your comment!
Please enter your name here