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
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
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.
Now you gonna see the added text in a test file using following command.
cat test1
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.
Now we gonna add more text in a test file.
cat > test1
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
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.
cat >> test1
Now see its content and you will see data is appended instead of overwritten.
cat test1
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.