{

Linux/Unix - Top 6 cat command examples with tutorial


Linux cat command examples

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.

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

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

Cat Command Syntax

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

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.
CTRL +D

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 command also 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 test file 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 file with showing spaces, tabs using 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.