0% found this document useful (0 votes)
6 views

cat command

The cat command in Linux is a versatile tool for file operations such as viewing, creating, and concatenating files. It allows users to create files, view contents with or without line numbers, copy, and append file contents. Additionally, the tac command can be used to display file contents in reverse order.

Uploaded by

prashantsutar024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

cat command

The cat command in Linux is a versatile tool for file operations such as viewing, creating, and concatenating files. It allows users to create files, view contents with or without line numbers, copy, and append file contents. Additionally, the tac command can be used to display file contents in reverse order.

Uploaded by

prashantsutar024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

cat Command

-----------

The cat command in Linux is more than just a simple tool;


it’s a versatile companion for various file-related operations, allowing users to
view, concatenate, create, copy, merge,
and manipulate file contents.

Basic Syntax of `cat` Command


The basic syntax of the ‘cat’ command is as follows:
cat [OPTION] [FILE]
Here,
[OPTION] : represents various command-line options.
[FILE] : the name of the file(s) to be processed.

1. How to Create a file and add content in Linux Using `cat` Command
If you want to create a new file or overwrite an existing file with new content,
you can use ‘cat’ with the output redirection (`>`):
Syntax:
cat > newfile_name
Example: If we want to create a newfile_name = vishal.txt
cat > vishal.txt
Hi Team,
Good Morning
ctrl+d

This will allow you to type text directly into the terminal, and when you press
Ctrl + D, the entered text will be saved to vishal.txt.
`ls` command is used to display all files and directories in the current location.
-------------------------------------------------------------------------------

2. How to View the Content of a Single File in Linux


The most basic use of ‘cat’ is to display the contents of a file on the terminal.
This can be achieved by simply providing the filename as an argument:
Syntax:
cat file_name
Example: If our file_name = vishal.txt
cat vishal.txt

output->
Hi Team,
Good Morning
-----------------------------------------------------------------------------------
----

3. How to View the Content of Multiple Files in Linux


Syntax:
cat file_name1 file_name2
Example: If we have two files , file1 and file2.
cat file1 file2

output-> It will display the data of both files.


-----------------------------------------------------------------------------------
-----

4. How to View Contents of a File preceding with Line Numbers in Linux


Adding the -n option to cat introduces line numbers, making it convenient to
identify and reference specific lines within the file.
Syntax:
cat -n file_name
Example: If our file_name is vishal.txt
cat -n vishal.txt

Output->
1 Hi Team,
2 Good Morning

-----------------------------------------------------------------------------------
------------

5. How to Copy the Contents of One File to Another File in Linux


As the name suggests, ‘cat’ can concatenate multiple files into a single file.
This example illustrates how to copy the entire content of “file1” into “file2”
using the cat command along with redirection (>).
Syntax:
cat file1.txt file2.txt > merged_file.txt
This command combines the content of file1.txt and file2.txt into a new file named
merged_file.txt.
-----------------------------------------------------------------------------------
---------------

6. How to Append the Contents of One File to the End of Another File
If you want to add the content of one file to another, ‘cat’ can be used along with
the append (>>) operator:
Syntax:
cat file_name1 >> file_name2
Example:
cat file1 >> file2
This will append the content of `file1` to the end of `file2`
-----------------------------------------------------------------------------------
---------------------

7. How to Display Content in Reverse Order Using `tac` Command in Linux


The ‘tac’ command is the reverse of ‘cat’ and is used to display the content of a
file in reverse order. The syntax is simple:
Syntax:
tac file_name
Example: tac vishal.txt
This command will print the content of ‘vishal.txt’ in reverse order, displaying
the last line first, followed by the second-to-last line, and so on.
Output->
Good Morning
Hi Team,
-----------------------------------------------------------------------------------
-----------------------------

8. Cat Command to Append to an Existing File:


To append text to an existing file, use the ‘>>’ operator along with ‘cat’:
Syntax:
cat >> vishal.txt
The newly added text.

This will append the text “The newly added text.” to the end of the ‘vishal.txt’
file.
Output->
Hi Team,
Good Morning
The newly added text.

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

You might also like