cat Command in Linux. Create a new file.

view file in linux

Introduction

Concatenate in short known as cat command is a very popular and widely used command in Linux and UNIX like operating system. The cat command is mainly used to display the contents of a file on the terminal. It is also used to create small text files.

Tip : Like other Linux commands cat also accepts more than one filename as argument.

Also Read:: Difference Between cat and touch command.

SYNOPSIS/Syntax

The general syntax for command of cat command is as below.

$ cat [option] [file(s)]

cat Options

There are lot of cat options, but may find two options which is very useful during working with cat command.

  • -v (Displaying Non-Printing character)
  • -n (Numbering Lines)

Displaying non-printing character (-v): We normally use use to display small text files. If you try to see a executable files (binary files like date, time command etc.) you will get junk type output on your terminal. You can use cat command with -v option to see non-printing ASCII character.

Numbering Lines (-n): When cat command is invoked with -n option it numbers lines. C compilers indicate the line number where errors are detected. And this numbering system helps a programmer to debug programs.

Examples

In this article will cover some handy use of cat command with examples.

Create a File with cat Command

As you known cat is also useful for creating a file. If you wish to create a text file with file name file.txt run the below command.

$ cat > file.txt
This is a new file. [Enter]
This is created now. [Enter]
{ctrl+D}
View a File with cat Command

You can view the contents of a file by invoking the below command

$ cat filename.txt

This above command helps to view file in Linux.

View content of multiple files

This is a very good feature of cat command as you can see content of multiple files with a single command. You have two files file1 and file2 and you wanted to see the content of the both files at a time , run the below command.

$ cat file1 file2
Display Line Numbers in a File

-n option is used to see the line numbers of a file. I have a file file1.txt, let’s see the line numbers.

Redirecting Multiple Files Contain in a Single File

If you want to redirect all the output of multiple files to a new file run the below command.

$ cat file1 file2 file3 > file4

This command created a new file named file4 and all output got redirected to the new file file4.

Conclusion

cat is a versatile command. It is used to create, display, concatenate and append to files. This article have basic details about cat command. You may refer man page of cat command if you want to explore more. If found helpful share and leave a comment. It is a very helpful command to view file in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *