As a Linux user, you need to know the usage of the best commands in the terminal to enhance your Linux experience. First of all, it’s good to know the basic commands used frequently.
One of these commands includes the “cat” command (short for concatenate). As a terminal user, the cat command lets you display text file contents and a lot more!
In today’s tutorial for beginners, we shall go through 10 Linux cat commands with examples.
Cat command usage in Linux with examples
Let’s get started.
1. File creation
One of the most common tasks that you can do with the “cat” command is to create a new file.
To do so, you need to use the following syntax.
$ cat > filename
This will instantly create a file named “filename,” and a cursor will start blinking. Now, you should be able to add content to your file.
In our example, we create a file name “learningcat.”
$ cat > learningcat
To return to your command line prompt, you need to press CTRL + D. It will quit and save the file.
2. Displaying file content
Cat command can also be used to view file content. All you need to do is run the cat command, followed by the filename.
$ cat filename
In our case, we can display the “learningcat” file that we just created.
$ cat learningcat
3. Display multiple file’s contents at once
The cat command also lets you show multiple file content at once.
$ cat filename1 filename2
$ cat learningcat helloworld!
4. Append text to file
Cat command is not only useful for creating new files and displaying their content, but you can also use it to append text to the file. This means that the content will be added at the end of the file.
5. Copying content from one file to another
Cat command is also useful when it comes to copying one file content to another. To do so, you need to use the following command.
$ cat filename1 > filename2
In the above command, filename1 content will be copied to filename2
$ cat learningcat > learningcatcopy
Check the screenshot below to get a better understanding.
6. Displaying line numbers and total line number
You can also use the cat command to display line numbers. To do so, you need to use the following command.
$ cat -n filename
In our case, we created a file named, you guessed it, “longfile.”
Let’s see the command in action.
$ cat -n longfile
You can also display the total number of lines of a file.
$ cat filename | wc -l
Now, let’s run it on the terminal.
$ cat longfile | wc -l
7. Display head and tail of the file
You can display the head and tail of the file by using the below commands
To display head, use the following syntax.
$ cat filename | head - number
The -number denotes the number of lines you want to show from the beginning of the file.
If you change “head” to tail, then it will showcase the last lines of the files. The syntax to do so is as below
$ cat filename | tail - number
Let’s see both in action.
8. Insert $ at each line’s end
You can display $ at the end of each line using the following cat command
The syntax is as below.
$ cat -E filename
$ cat - E longfile
9. View content in reverse order
If you want to see the file content in reverse, you can use the tac command. It is an exact reverse of the “cat” command.
$ tac filename
10. Displaying non-printing characters
To view non-printing characters, you need to use -v option.
$ cat -v filename
Conclusion
This leads us to the end of our 10 Linux Cat command examples for Beginners.
Want to share some thoughts? Comment below and let us know!