How to Use cat Command in Linux (with Examples)

If you happen to’re utilizing a Linux pc, operations are vastly completely different as in comparison with Home windows and macOS. You get each a graphic person interface and a command line interface. Whereas GUI appears to be the simple choice to execute operations, CLI does have its personal advantages. If you’re well-versed in all … The post How to Use cat Command in Linux (with Examples) appeared first on Ferdja.

May 3, 2023 - 19:00
 2
How to Use cat Command in Linux (with Examples)

If you happen to’re utilizing a Linux pc, operations are vastly completely different as in comparison with Home windows and macOS. You get each a graphic person interface and a command line interface. Whereas GUI appears to be the simple choice to execute operations, CLI does have its personal advantages. If you’re well-versed in all of the necessary Linux Terminal instructions, you may get issues carried out very quickly. One of the crucial used instructions on Linux is the cat command. It comes preinstalled as part of the coreutils bundle on all Linux distributions, and the syntax is similar for all distros. That mentioned, we’ll present tips on how to use the cat command with some sensible examples on this article.

cat Command in Linux: Defined (2023)

Earlier than we take a look at the examples, let’s perceive what’s the cat command together with its syntax and choices. Then, we’ll learn to use the cat command effectively to view single or a number of recordsdata, merge recordsdata, kind them, and extra.

What’s the cat Command in Linux

The cat command stands for concatenate, and it is likely one of the most necessary instructions in each Linux person’s toolbox. It was first made for the UNIX working system however was later tailored by Linux and macOS. The primary objective of this command is file administration, and it allows the person to create new recordsdata, view file contents, overwrite recordsdata, merge two or extra recordsdata, and so forth.

Tips on how to Use cat Command: Syntax & Choices

Earlier than we will dive into some sensible examples, let’s see the syntax for the cat command in Linux. The syntax is straightforward and easy. Right here’s the syntax, the place you want to use an possibility together with the file names relying on the duty you want to carry out.

cat

A number of the frequent choices to make use of with the cat command are:

Choices Description
-n Present line numbers for all strains
-T Present each tab character within the file
-e Present the tip of each line within the file
-s Merge successive empty strains on the finish of the file as one
-b Present solely non-empty strains

cat Command Examples in Linux Terminal

View a Single File

The most typical utilization of the cat command is to view a single file. You need to use the next syntax to view a single file utilizing the cat command:

cat

view single file

View A number of Recordsdata

By including the identify of the recordsdata one after the opposite, seperated by areas and with none commas, you too can use the cat command to view a number of recordsdata. Take a look at the next syntax:

cat

view multiple files

Show Line Numbers

By default, the cat command doesn’t show the road numbers of the file contents it outputs. To indicate line numbers, use the -n flag with the cat command in Linux:

cat -n

display line numbers using cat command

Create a New File with cat Command

Usually, we use the contact command to create a brand new file or a textual content editor to create and edit a file. Clearly, the cat command can’t substitute these instruments, however you should use the cat command for some fast modifying of recordsdata. With the cat command, you possibly can create a brand new file and add some content material to it. The syntax to create a brand new file utilizing the cat command is:

cat >

Right here, the “>” is called the overwrite operator and is used to overwrite any file with new content material. Because the file is totally empty, no matter you write, will get written to the file. If you end up carried out writing to the brand new file, press “ENTER” after which use “CTRL + d" to exit the immediate.

create new file using the cat command

Within the instance above, you possibly can see {that a} new file “test1.txt” is created utilizing the cat command, and the file contents are proven by the output of the second cat command.

Merge Two Recordsdata right into a New File

Utilizing the syntax under, you possibly can even use the cat command to mix two recordsdata into one. We might be utilizing the append operator (“>>“) so as to add the contents of the primary file on the finish of the second file utilizing the command under.

cat >>

Merge two files using the cat command

Within the above instance, the contents of the file “test1.txt” are added on the finish of the “test2.txt” utilizing the cat command. The brand new contents may be verified with the second cat command’s output, the place we view the second file.

Copy the Content material of One File to One other

You possibly can even copy the content material of a file to a different file utilizing the cat command, as defined under. Right here, the “>” is used to overwrite the contents of file_1 to file_2.

cat >

overwrite a file with another

Within the above instance, we have now overwritten the contents of the file “test1.txt” with the contents of the file “test2.txt” utilizing the overwrite operator.

Show Invisible Characters

By default, the cat command doesn’t mark the road endings whereas printing the contents of a file. To indicate the road endings, use the -E flag together with the command:

cat -E

It will mark the ending of every line with a "$" image. To print the tabs as a substitute of 4 clean areas, use both the -T flag, as per the syntax proven under:

cat -T

It will print all tab characters as “^I. To print all different invisible characters, use the -v flag with the cat command, as proven within the syntax under:

cat -v

display invisible characters using the cat command

As you possibly can see within the instance above, all the road endings are marked with a “$” image, and the tabs are marked with a “^I” character.

Mix A number of Empty Strains as One

Typically there could also be some empty strains within the file that you don’t want to print. To merge all empty strains as one, use the -s flag with the unique cat command.

cat -s

merging empty lines as one using the cat command

View File Contents in Reverse Order (tac Command)

Usually, the cat command shows the file content material in top-down format. However, whereas storing some reside stream information or viewing some massive log file, the most recent information will get appended at that finish and it may be troublesome to scroll by the massive textual content block. In such instances, you should use the tac command in Linux, another model of the cat command, which prints the file contents in reverse order. The syntax to make use of the tac command is:

tac

view contents of a file in reverse order

Sorting Output Contents of Recordsdata

In Linux, you possibly can mix two or extra instructions with the assistance of shell redirectors. They redirect the output of 1 command to the enter of the following command. You need to use the overwrite operator (>) and the append operator (>>), that are generally known as I/O shell redirectors.

There may be additionally a second kind of shell redirector generally known as shell piping which is used to run two or extra instructions concurrently. This implies the output of 1 command might be redirected to the following command because the enter. Because the command execution follows a particular assemble, such a assemble or idea is called a pipeline. The pipe operator ( | ) creates a pipeline for these instructions to execute in a particular sequence.

By now, you should be effectively conscious that the cat command prints the file contents in the identical order as they’re saved within the file. Because the identify suggests, the kind command classifies the output in ascending or descending order. However by sending the output of the cat command through the pipe operator to the kind command, you may get the ultimate output within the desired sorted order. This may sound complicated and sophisticated, however the instance under will filter every little thing. The syntax to make use of the 2 instructions utilizing a pipe operator is:

cat | kind

sorting file contents on alphabetical order using the cat command and the sort command

Within the above instance, as a substitute of printing the contents of the file “test3.txt”, the cat command sends the contens to the kind command which then types it in line with alphabetical order and eventually prints the sorted output.

View Giant Recordsdata Utilizing cat Command

Typically, even a system with nice specs can stutter in exhibiting the contents of a giant file. For such massive recordsdata, it’s best to use the much less command and the cat command together with the pipe operator. Because the much less command solely hundreds part of the file at a time, it doesn’t devour a ton of assets. You possibly can scroll up or down to go to the opposite components of the file utilizing the arrow keys. The syntax to make use of the much less command with the cat command is:

cat | much less

  • How to Use cat Command in Linux (with Examples)
  • How to Use cat Command in Linux (with Examples)

Within the above instance, if you execute the command as per the above syntax, the file doesn’t get printed on the identical terminal immediate, as a substitute, it reveals the file contents in a brand new terminal view as proven within the second image. Right here you possibly can scroll by the textual content utilizing the arrow keys. To resolve the textual content use “GG” and to get to the highest of the textual content, use “gg”. To exit the brand new terminal view, press “q”.

cat Command Sensible Examples

The cat command, together with the tac command, significantly simplifies file administration for customers snug utilizing the Linux Terminal. With choices and extra operators, the cat command may be immensely useful in simplifying your workflow. On this article, we have now shared some sensible examples of tips on how to use the cat command to create, append, and consider recordsdata in your Linux system. If you wish to be taught extra concerning the cat command, go to its official man page. If you happen to face any points when utilizing this command, tell us within the feedback under.

The post How to Use cat Command in Linux (with Examples) appeared first on Ferdja.