08. cat command - muneeb-mbytes/linux_course GitHub Wiki

cat command

cat short for ‘concatenate’, is a very useful command. Using the cat command, you can create a file, view file content, concatenate files, and file output redirection.

cat command and it's variations

The below figure represents the cat command variations.

cat command variations

                                   Figure.1.Representation of cat command variation

cat command cheat sheet

Sr No. cat command with different options description
1 cat filename Displays the file contents
2 cat -n filename Display contents of a file with line numbers
3 cat --help List all cat command options
          Tabular column.1. cat command cheat sheet

cat filename.txt

cat filename.txt command displays the file contents. The most basic and common usage of the cat command is to read the contents of files.Instead of displaying the output on the terminal, you can redirect it to a file.

Syntax: cat filename

Example: cat file1.txt

The below command will copy the contents of file1.txt to file2.txt using the (>) operator, this is called as Routing.

cat file1.txt > file2.txt

The below figure shows the output of cat file1.txt, where the file1.txt content is displayed on the terminal.

1

             Fig.1 :Read the Content of file1.txt and give the output on the terminal

The below figure shows the output of cat file1.txt >> file2.txt, where the output of cat file1.txt will be routed to file2.

2

                Fig.2 Routing the content of file1.txt to file2.txt        

The below GIF shows the process of sourcing scripting file and running cat command with routing that output to another file, file2.txt.

Animation

               gif.1: Process of sourcing cat file1.txt file and routing the output to file2

Link to GitHub lab: https://github.com/muneeb-mbytes/linux_course/blob/b7_team_kachori/cat_command/cat.csh


cat -n filename

cat -n filename command used to display contents of a file with line numbers.

Syntax: cat -n filename

Example: cat -n filename.txt

The below figure shows the output of cat -n file1.txt, where it will display the content of file1 with the line numbers.

3

                Fig.4: Display the content in the file1.txt along with the line numbers in the terminal

The below GIF shows the whole process from sourcing scripting file till the output.

cat-n

                          gif.2: Process of sourcing cat -n file1.txt

Link to GitHub lab: https://github.com/muneeb-mbytes/linux_course/blob/b7_team_kachori/cat_command/cat-n.csh


cat --help

Syntax: cat --help

To list all cat command options, type cat --help. The system lists all available options.


To learn echo command: https://github.com/muneeb-mbytes/linux_course/wiki/09.-echo_command