Connect with us

Tech

13 Most Used Cat Command Examples In Linux

Published

on

The cat command in Linux is one of the most used commands that give you a wide variety of options to perform executions. You can create a file, view the content of single and multiple files, make modifications in the file, add data, remove data, and much more with the cat commands.

Cat commands are widely used in Linux and Unix-like systems and are very helpful to do your work only with short commands. These commands read the file data and display it in the output terminal. Know the cat commands in Linux and use them for your system executions.

cat command in linux

Here, we will show you 13 basic cat command examples in Linux. Make sure to go through all of them and use the commands for your operations.

Cat Command In Linux

The Cat commands in Linux are used to read the data and to give the output. Here, one thing you have to note is the syntax of cat commands. There is a fixed general form for the cat commands, and you have to make your program entries as it says to do.

General Syntax For Cat Command

Whenever you want to write a command for Concatenating the files, the syntax must be like this:

cat [OPTION] [FILE]...

Create A File

To create a file, give this cat command in Linux.

# cat >test2

Once you create a file, you have to enter the content or input. Type whatever input you want to save and press Ctrl+D. The text will be saved in the created file.

# cat test2

hello everyone, how do you do?

View Contents Of Single File

For viewing a specific file’s content, you have to give the command as shown below.

# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash

View Contents Of Multiple Files

To view more than one file content, you can give a cat command as shown in below. From this command, you will get the content from test and test1 files.

# cat test test1

Hello everybody
Hi world,

View Multiple Files At Once

Besides the above command, you can also use this method for viewing multiple files. Here, you have to give multiple filenames with the cat command. The semicolon (;) is a must to include at the end.

# cat test; cat test1; cat test2

This is test file
This is test1 file.
This is test2 file.

For More & Less Options

If you want to reduce the size of the file or contrast to it, you can use this cat command in Linux. Since large files don’t fit in the output terminal and might create output errors, these commands will be helpful.

# cat song.txt | more
# cat song.txt | less

Display Line Numbers Of File

If you want to view the line numbers of a file, then you have to use the number option “-n”. You can view the number of lines of the file name given. The command should be as this:

# cat -n song.txt

1  "Heal The World"
2  There's A Place In
3  Your Heart
4  And I Know That It Is Love
5  And This Place Could
6  Be Much
7  Brighter Than Tomorrow
8  And If You Really Try
9  You'll Find There's No Need
10  To Cry
11  In This Place You'll Feel
12  There's No Hurt Or Sorrow

Display $ In File

To fit multiple lines in a single line, and also to easily notice the unnecessary gaps in paragraphs, you can add a $ in the file with the “-e” option.

# cat -e test

hello everyone, how do you do?$
$
Hey, am fine.$
How's your training going on?$
$

Display Tab Separated Lines

By using “-T” in the cat command, you can separate the Tab used lines from the normal lines. The TAB separated lines will show “^I” notations at the beginning.

# cat -T test

hello ^Ieveryone, how do you do?

Hey, ^Iam fine.
^I^IHow's your training ^Igoing on?
Let's do ^Isome practice in Linux.

Redirect Standard Output

You can also redirect the standard output of a file to another file. For example, you can transfer the output of file1 to file2 with the help of the redirection operator (>). Here, the file2 content will be replaced with file1 content. So, file2 content will be erased.

# cat test > test1

Appending Standard Output

The appending redirection operator symbol is “>>” and is used to add the content to an existing file without removing the content. For example, if you want to add file1 content to file2, the cat file1>>file2 command will add the file1 content at the end of file2.

# cat test >> test1

Recommended Guides:

Redirect Standard Input

The standard input redirection operator symbol “<” transfers the input from a file to another file and gives as the output.

# cat < test2

This is test2 file.

Redirect Multiple Files

A new output file is created and the redirected multiple files will be saved in the output file and displayed in the output terminal. Here test3 is the newly created file. Give the below Linux cat command to direct multiple standard output files.

# cat test test1 test2 > test3

Sort Contents Of Multiple Files

When you want to sort all the files, you have to create a new file that will store the output results of multiples in an arranged manner. Give the below cat command in Linux.

# cat test test1 test2 test3 | sort > test4

How To Use Cat Command In Linux

To use the cat command in Linux, just follow the simple steps given here.

  1. Go to the terminal program on your Linux system.
  2. Now, type the cat command based on the operation that you want to perform.
  3. And press Enter to run the command.

This was the simple procedure to use the cat command in the Linux system. The commands vary based on your operation. You can add, modify, concatenate, and many such operations with cat commands. Use the cat command in Linux that we have given above using this procedure.

Conclusion

We hope you found this article helpful. Here we have listed the cat command in Linux that are widely used and are easy too. If you liked this article about cat command Linux, share it with your friends. For more updated cat commands, keep in touch with us on our website Tech 170.

Continue Reading
Click to comment

Leave a Reply

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

Trending

Copyright © 2021 All Rights Reserved by Tech 170