Linux/Unix - Top 6 cat command examples with tutoral

Basic Cat command examples in Linux/Unix

Cat command is simple command used by every Unix/Linux user. It is one of frequently used command in Linux/Unix Systems.Cat command works on bash, and powershell and shell options.
Cat command behaves different based on the different types of usage.

By using cat command, we can do the following functionalities

1. Read files using cat command
2. Create files Using cat command
3. Copy files content using cat command.

Cant command works unix,linux as well as ubuntu terminals

Cat command Syntax

Cat (options) (one or more file name)

Let us see the various cat command examples as below.

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

This is simple command to read the contents of file and output the file content to Standard output like console
$cat cloud.txt

This is test file contains test content.
If it is small file, we can view it using cat command, but file size is large we can use more or less with pipe options in cat command.

How to create a file using Cat command in Unix

Cat command is a basic command to create files. To create a file demo.txt
$ cat >demo.txt
This is test file contains
test content.
Once the above command is entered, new demo.txt file is created and console waits from user to type input and whatever data typed by user is placed in file.> symbol specifies that data to placed in a file typed by a user. You need to type CTRL with typing D to save data to file and exit from console.

How to merge files using cat command in Linux/Unix

Cat command also used to merge different files and create a new file
$ cat file1 file2 > output.txt

First file1 data is copied to output.txt and next file2 data is copied to end of file1’s data.

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

In java programs, if any error happens, Error/Exception thrown with print statck trace which will give filename as well as line number where exception is occurred. In that case, to view the java code with line numbers, we can use cat command with option –n
$ cat –n cloud.txt
1 This is test file contains
2 test content.

Append/concatenate data to existing file using cat command

$cat >>cloud.txt
New data is appending to existing file
We have to press CTRL+D option to save/append the new data to existing data in a file

How to view file with showing spaces, tabs using cat command with examples.

In any text Editors, we have option to be enabled to show spaces and tabs codes

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

Cat command options

Basic cat command has different options, 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 file

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

Please click +1 if you like this post.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.