10 ‘cat’ Command Examples for Beginners
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