Use of cat command in linux

Use of Cat Command in Linux

Cat command used frequently in Linux/Unix similar like Operating Systems. Cat command is used to create files. Whenever you create a new file through Cat command you need to put redirection operator “>” between Cat command and name of your file

Cat Syntax

cat [OPTION] [FILE]

View a file Content

While using following command it will display content of /etc/selinux/config file

cat /etc/selinux/config
Use of cat command in linux 1
Use of cat command in linux 8
Sponsored

View multiple file content

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

cat newfile1 newfile2 newfile3

and it’s gonna list content of all three files respectivily.

Create a File with Cat Command

cat > test1
Use of cat command in linux 2
Use of cat command in linux 9

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

See also  10 Best Linux Desktop Distributions in 2021

Now you gonna see the added text in a test file using following command.

cat test1
Use of cat command in linux 3
Use of cat command in linux 10

Note: If you already have an existing file then you add text in it while using Redirection Opertor “>” it’s gonna overwrite previous content with new one in doing so, you gonna lose your old data. Check following command for a referance.

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

Sponsored

Now we gonna add more text in a test file.

cat > test1
Use of cat command in linux 4
Use of cat command in linux 11

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

Now check “test” file again and you will see that previous text has removed and new text taken its place.

cat test1
Use of cat command in linux 5
Use of cat command in linux 12

Append text in a file

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

See also  How to Install Konqueror File Manager on Ubuntu 20.04 LTS
cat >> test1
Use of cat command in linux 6
Use of cat command in linux 13

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

cat test1
Use of cat command in linux 7
Use of cat command in linux 14

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 test1 test2 > test3

That’s about it, you get an idea how to use cat command in many ways. See manual using command “man cat” for more information.

The post Use of Cat Command in Linux appeared first on Osgrove.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply