One of the most commonly used commands in Linux is the cat command. Short for concatenate, Linux cat command allows users to perform the following operations.
- View contents of a file
- Redirect output of files
- Create files and input content
In this tutorial, we take a look at cat command with a few examples of how it can be used.
Syntax:
$ cat [option] [file]
Table of Contents
- 1 1. Display content of a file
- 2 2. Viewing Multiple Files
- 3 3. Number the lines of content in a file
- 4 4. Use cat command with less or more options
- 5 5. Create files using cat command
- 6 6. View multiple files at once
- 7 7. Appending standard output to another file
- 8 8. Redirect output of a file onto another file using the redirection operator
- 9 Conclusion
1. Display content of a file
To view a file’s content, use the syntax.
$ cat file-name
For example to view the file /etc/passwd
run:
$ cat /etc/passwd
Sample output
2. Viewing Multiple Files
In addition to viewing a single file, you can view multiple files in one command using the syntax as shown below.
$ cat file1 file2
For example, we have 2 files with the following content.
To view the files simultaneously, run:
$ cat file1.txt file2.txt
Sample output
3. Number the lines of content in a file
If you want to number the lines of content in a file, use the -n
as shown below.
$ cat -n filename
For example,
$ cat -n /etc/passwd
Sample Output
4. Use cat command with less or more options
The more
or less
parameters enable a user to easily scroll down long content that scrolls down very fast, making it difficult to follow . The synatx is quite simple.
$ cat filename | less
OR
$ cat filename | more
5. Create files using cat command
To create a file, use the >
sign as shown in the syntax below.
$ cat > filename
Next, type your desired text input and at the very end, press CTRL + D to exit.
The text will be written onto the text file. You can verify this using the cat command as seen in the first example.
6. View multiple files at once
Assume you have several files that you’d want to view at a go, use the colon mark ;
to separate the files being displayed as shown.
cat file1.txt; cat file2.txt; cat file3.txt; cat file4.txt ...
7. Appending standard output to another file
You can append content onto a file using the double greater sign redirection operator >>
as shown.
cat file1 >> file2
This appends the contents of file 1 onto file 2.
Sample output
8. Redirect output of a file onto another file using the redirection operator
You can redirect standard output of a file into a new file with ‘>‘ (greater than) symbol.
cat file1 > file2
As seen in the output above, extra caution should be taken when doing this because this operation overwrites the contents of the destination file.
To redirect multiple outputs into a single file use the syntax
$ cat file1 file2 file3 file4 > file5
Conclusion
Linux cat command is a simple utility to view file contents, create a text file with some content, and append data to a text file.