10 ‘cat’ Command Examples for Beginners
“cat” is one of the commands that is used mostly in our everyday activities, so far as the terminal is concerned. It is mostly used with text files for displaying, combining (concatenating) and creating new ones.
Here are 10 useful examples of such cat commands for beginners.
1. Creating a new file
The most common use of cat is viewing text files even though you can do this by using a GUI text editor like gedit, this is faster and saves you from all those double clicking.
To create a new file you use the syntax below, hit the Enter key and type the content of the file:
$ cat > filename
After entering the content of the file save it and quit by pressing Ctrl + D. Always press Ctrl + D to effect changes to the file.
$ cat > unixmen.txt Unixmen.com is a website for Linux/Unix news, HowTos and Tutorials
2. Displaying files
$ cat unixmen.txt
You can also use cat to display multiple files with one line of command.
$ cat unixmen1.txt unixmen2.txt
To clearly view the files, you can insert blank lines before and after the texts in the files by pressing the Enter key.
3. Appending text to file
cat >> unixmen.txt
The above command keeps the existing content of the unixmen.txt file and appends your new text entered at the end of it.
4. Displaying line numbers
This feature of cat displays the line numbers beside each line in the terminal by using the following:
$ cat -n filename or $ cat -b filename
Using -n on cat numbers all lines including blank lines:
$ cat -n unixmen.txt
To display line numbers excluding blank lines use -b instead of -n:
$ cat -b unixmen.txt
5. Display total line number
$ cat filename |wc -l
This prints out or displays the total number of lines in the text file.
The command below displays the total number of characters in the text file.
$ cat filename |wc -c
6. Copying files
cat can be used to produce duplicates of file giving them another names. Assuming you want copy the contents of a file named unixmen.txt to unixmen3.txt, below is the syntax to use:
$ cat unixmen.txt > unixmen3.txt
Displaying both files gives you the same output.
The “>” (mostly called greater than) known as redirection operator in this case. It tells cat command to copy to the content of one file to another.
7. Concatenating files
Concatenation simply means bringing together multiple files to form one file. The files concatenated are not in anyway affected. As shown in the example below two files have been put together to make a single file named unixmen3.txt:
$ cat unixmen1.txt unixmen2.txt > unixmen3.txt
8. Squeezing blank lines
Adding -s to cat command squeezes repeating blank lines into one:
$ cat -s filename
9. Show non-printing characters
To display non-printing characters such as ^V and ^M when you press Ctrl+V and Ctrl+M respectively, in the terminal. Using -u with cat command will output them:
$ cat -v filename
10. More displaying options
To display number of lines from the beginning of a text file use the syntax below:
$ cat filename |head -number
$ cat unixmen.txt |head -3
The command above works like this; Open the unixmen.txt file, the |head -3 tells cat command to print out only the first 3-lines (from the beginning) of the file.
To do this in the reverse way, that is output the last 3-lines (from the end) of the file.
Replace head with tail, resulting in:
$ cat unixmen.txt |tail -3
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- News from Kaspersky Lab: Tor is protecting criminals : In a previous article I wrote a bit about Tor. Free browser, relays bouncing your IP all over the ...1 comment |
- How To Access A LVM Drive / Partition in Ubuntu : Hey Guys, I installed Fedora on an external drive (LVM) recently and was trying to access it from U...0 comments |
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...2 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker) : In our previous tutorial, we have shown you how to install corosync and pacemaker. We will show y...0 comments |
- Install Satis On Ubuntu Sever 13.10/13.04 : Satis is a simple static composer repository generator. It is used in any composer.json file as i...0 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker)
- News from Kaspersky Lab: Tor is protecting criminals
- Install Satis On Ubuntu Sever 13.10/13.04
- How To Access A LVM Drive / Partition in Ubuntu
- How To Disable Remember Open Files in Sublime Text 3
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
- How To “Fix E: Could not open file /var/lib/dpkg/status”
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .