Linux/Unix - Top 6 cat command examples with tutorial

This tutorial explains about cat command in linux with below examples

  • Read files using the cat command
  • Create files Using the cat command
  • Copy file content using the cat command.

Basic Cat command examples in Linux/Unix

Cat command is a simple command used by every Unix/Linux user. It is one of the most frequently used commands in Linux/Unix Systems. Cat command works on bash, PowerShell, and shell options.

Cat command behaves differently based on the different types of usage.

By using the cat command, we can do the following functionalities.

  • Read files using the cat command
  • Create files Using the cat command
  • Copy file content using the cat command.

Cat command works with UNIX, Linux as well as Ubuntu terminals.

Cat Command Syntax

cat command has files with options.

Cat (options) (one or more filename)

Let us see the various cat command examples below.

Read or view the file using the cat command with examples in Linux/Unix

It is a simple command to read the file contents and output the content to Standard output like a console.

$cat cloud.txt
It is a test file containing test content.

If it is a small file, we can view it using the cat command, but if the file size is large we can use more or fewer pipe options in the cat command.

How to create a file using the Cat command in Unix

The Cat command is a basic command to create files. To create a file demo.txt

$ cat >demo.txt
This test file contains
test content.

Once the above command is entered, the new demo.txt file is created and the console waits for the user to type input and whatever data typed by the user is placed in the file.> symbol specifies that data is to place in a file typed by a user. You need to type CTRL with typing D to save data to the file and exit from the console.

How to merge files using the cat command in Linux/Unix

Cat commandalso used to merge different files and create a new file

cat file1 file2 > output.txt

First, file1 data copy to output.txt, next, file2 data is copied to the end of file1’s data.

How to view the line numbers of each line using the cat command

If an error occurs in a Java program, an Error/Exception is thrown with a print stack trace that includes the filename and line number where the exception occurred.

In that case, to view the java code with line numbers, we can use the cat command with option –n

$ cat –n cloud.txt
1 This is a test file that contains
2 test content.

Append/concatenate data to an existing file using the cat command

$cat >>cloud.txt

New data is appended to an existing file We have to press the CTRL+D option to save/append the new data to existing data in a file.

How to view files with showing spaces, and tabs using the cat command with examples

In any text editor, we have the option to be enabled to show spaces and tabs codes.

Cat has also had the option to view spaces and tabs and end of lines using option –T

cat –T filename

Cat command options

The basic cat command has different options and can be used with all flavors of Linux like AIX

  • -T output spaces, tabs, and End of lines characters in the file
  • -s suppress the multiple blank lines
  • -n output number on each line of the file

That’s it from the usage of cat command in the daily life of software development.

Please click +1 if you like this post.