The cat command stands for “concatenate” and it is widely used in Linux OS. Using it you can read, write, concatenate files and redirect output to terminal. Commonly, cat is most used to display the contents of single or multiple text files, create new files, combine files, append content from one file to another file.
Basic Syntax of cat Command
First of all you should know the syntax of cat command before going to use it. Below is the basic syntax of
cat [OPTIONS] [FILE_NAMES]
- OPTIONS – You can specify the options. Get all available options using cat –help.
- FILE_NAMES – Specify single or multiple file names.
Display Contents of File
The cat command is commonly used to read contents of files. Below example will show the contents of the
Display Contents of Multiple Files
You can view content of multiple files using below command:
cat file1.txt file2.txt
Redirect Contents of File
You can store output to a file instead of displaying to the screen. Below command will copy content from first file to second file:
cat file1.txt > file2.txt
Above command will create a file if second file does not exists. If it is available then it will overwrite the content of file.
Appending Output with Redirection Operator
You can append contents of one to another file without overwrite using below command:
cat file1.txt >> file2.txt
Redirecting Standard Input with Redirection Operator
When you want to take input from second file to terminal then you can use standard input
'<' (less than) symbol as given below:
cat < file1.txt
Create a File with Cat Command
It is very easy to create a file using cat command. To create file using cat command you just have to use redirection operator and the name of the file. Hit the Enter key and now you can type whatever you wants to write. Save the file using
Ctrl + D.
cat > file.txt
In above example we are creating a file name with
file.txt. If file with that name exists then it will be overwritten otherwise it will create a new file.
Display Line Numbers
If you want to display file content with line number then you can use
-n option with cat command as below:
cat -n file1.txt
1 This is Line Number One
2 This is Second Line Number
Display Tab separated Lines in File
You can use
-T argument to differentiate tabs and spaces:
cat -T /etc/hosts
The TAB characters will be shown as
Display $ at the End of File
You can display line ending with
'$' also can display if any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.
cat -e /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"$
Concatenating Multiple Files in a Single File
When you pass more then one file name as arguments with cat command then contents of files will be concatenated. It will read file as given sequence and will append to display same.
In below command it will read the contents of
file2.txt and display the result in the terminal:
cat file1.txt file2.txt
You can concatenate the contents of file
file2.txt and store them to another file using redirection operator as below:
cat file1.txt file2.txt > file3.txt
If the file is not present then it will be created otherwise it will be overwritten.
Finally, you have learned how to use the Linux cat command.
If you have any question or feedback, feel free to leave a comment.
Leave a Reply