How To Text The File Using Linux Cat Command

 Overview

========

Here we look at how to write text into a file using the Linux cat command.


The cat Command

================

Cat command's common usages is to print the content of a file onto the standard output stream.

Other than that, the cat command allows us to write some texts into a file.


The Syntax

==========

Please look at the general syntax of the cat command:


cat [OPTION] [FILE]

Copy

First, OPTION is a list of flags we can apply to modify the command’s printing behavior, 

whereas FILE is a list of files we want the command to read.



Making cat Read From stdin

==========================

Let’s execute the cat command:


cat


Now try to enter some texts into the terminal:


cat


This is a appsolworld

This is a appsolworld


Once we are done, we can terminate the command by pressing CTRL+D.


[demantra@dsiddem demantra]$ cat >> a

appsolworld

^C

[demantra@dsiddem demantra]$ cat a

sfsfsfsd

appsolworld

[demantra@dsiddem demantra]$ cat > a

this

[demantra@dsiddem demantra]$ cat a

this



Writing to a File Using cat

===========================

To write to a file, we’ll make cat command listen to the input stream and then redirect the output of 

cat command into a file using the Linux redirection operators “>”.


To verify our result, we can use the cat command once again:


cat readme.txt

This is a readme file.

This is a new line.



Appending Text to File Using cat!

================================

One thing we should note in the previous example is that it’ll always overwrite the file readme.txt.


If we want to append to an existing file, we can use the “>>” operator:


cat >> readme.txt

This is an appended line.


To verify that the last command has appended the file, we check the content of the file:


cat readme.txt

This is a readme file.

This is a new line.

This is an appended line.


The line we enter is appended to the end of the file instead of replacing the entire document.


Here Document

=============

It is also worth noting that the here document syntax can be used with the cat command:


cat > a.txt << EOF

This is an input stream literal

EOF

Copy

EOF tells the cat command to terminate when it sees such a token in the subsequent lines.


Comments

Popular posts from this blog

How to fix Oracle SQL Developer connection issue "Got minus one from a read call"

How to troubleshoot Long Running Concurrent Request in EBS 12.2

Few Important steps of Oracle Database Clone