Cat stands for Concatenate. It is one of the powerful linux command which is used for various purposes like creating files, showing file contents, appending, merging, line numbers, etc,…
Here you can find 6 examples of cat command in unix / linux terminal with examples. They are
- Creating a file
- Displaying contents of file
- Text Appending to File
- Merging Multiple Files into Single File
- Displaying line numbers in file
- Displaying Version and Help about Cat Command
1. Creating a file
- Cat command is used to create a file on linux or unix OS
- This is done with help of the output redirection operator >
Syntax
cat > filename
Where,
filename is an user defined name.
The operator > is called as output redirection operator which is used to redirect (save) the output of cat command into a single file.
Example
2. Displaying contents of file
- Cat command is also used to display the contents of single file as well as multiple files
- This command is also used to show the file contents using the input redirection operator named <.
Syntax
cat filename // displaying contents of single file
cat file 1 file 2 … file n // displaying contents of multiple files at a time
Example – Showing file contents (without using < operator)
Example – Showing File Contents with using < input redirection operator
3. Text Appending to File
- Text message can be appended to file using cat command with append operator >>
- Here input must be an existing filename (old filename) else it will create a new file with input text.
Syntax
cat >> old-file
Example
4. Merging Multiple Files into Single File
- Using cat command, multiple files can be merged using the output redirection operator named >
- Here more than one input files have to be given for merging process
Syntax
cat file 1 file 2 … file n > res.txt
where,
- res.txt is an user defined name. So you can give any name to the target file.
Example
5. Displaying line numbers in file
- Cat command is also used to display the file contents along with line numbers using the special option -n
Syntax
cat -n filename
Example
6. Displaying version and help
- To check the version of cat command, type cat –version
- To get more help about cat command, type cat –help
Example – Displaying version details about cat command
Example – Displaying help details about cat command
Bluehost: It is one of the most popular hosting providers, recommended by WordPress. They offer a user-friendly interface, excellent uptime, and 24/7 customer support.
Bluehost: It is one of the most popular hosting providers, recommended by WordPress. They offer a user-friendly interface, excellent uptime, and 24/7 customer support. [url=http://webward.pw/]http://webward.pw/[/url].