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:

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.



2. Displaying files


You can also use cat to display multiple files with one line of command.


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

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:

Using -n on cat numbers all lines including blank lines:


To display line numbers excluding blank lines use -b instead of -n:


5. Display total line number 

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.


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:

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:


8. Squeezing blank lines

Adding -s to cat command squeezes repeating blank lines into one:

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:

10. More displaying options

To display number of lines from the beginning of a text file use the syntax below:

An example:


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:




未经允许不得转载:VPS啦 » 10 ‘cat’ Command Examples for Beginners

赞 (0) 打赏