The cat command, quick for “concatenate,” is a commonly-used instrument in Linux, enabling customers to view, create, and pay attention information, or redirect their output. On this publish, we’ll discover how each new and skilled Linux customers can leverage the cat command for quite a lot of duties.

The overall syntax of the cat command is as practice:

$ cat [OPTION] [FILE]...
1. Use cat to create a brand new report and upload content material
cat > filename

Instance:

The command cat > greens.txt takes the usual enter and redirects it to a report named “greens.txt”. While you execute this command, your terminal doesn’t show any output, however as an alternative waits so that you can input textual content from the keyboard.

No matter you kind will likely be written to “greens.txt”. To complete and save the report, you’ll want to press CTRL-D (or CTRL-Z on Home windows techniques the use of some terminal programs).

Right here’s an instance of the way chances are you’ll use it:

$ cat > greens.txt
Carrot
Broccoli
Spinach
^D

At this level, a report named “greens.txt” can be created with the next content material:

Carrot
Broccoli
Spinach
2. Show report’s content material with cat
cat filename

Instance:

Think the report accommodates an inventory of not unusual greens; executing the command cat greens.txt would show the contents of the report within the terminal.

Carrots
Broccoli
Spinach
Tomatoes
Cucumbers
Peppers
Onions
Potatoes
Kale
Lettuce
3. Show content material of more than one information with cat
cat filename_1 filename_2

Instance:

Assuming the contents of the information culmination.txt and greens.txt are respectively as follows:

Apple
Banana
Cherry
Carrot
Lettuce
Tomato

The command cat culmination.txt greens.txt would produce:

Apple
Banana
Cherry
Carrot
Lettuce
Tomato
4. Show content material with line numbering with cat
cat -n filename

Instance:

If the contents of culmination.txt are the similar as within the earlier instance:

Apple
Banana
Cherry

Then the command cat -n culmination.txt would produce:

     1	Apple
     2	Banana
     3	Cherry
5. Replica, substitute, or reflect a report’s content material the use of cat
cat filename new_filename

Instance:

The command cat culmination.txt > new_fruits.txt will take the contents of culmination.txt and write them into a brand new report named new_fruits.txt. This command received’t show any output to the terminal, assuming it executes effectively.

If culmination.txt has the similar content material as ahead of:

Apple
Banana
Cherry

After working the command, the content material of new_fruits.txt will likely be:

Apple
Banana
Cherry
6. Merge more than one information’ content material into one with cat
cat filename_1 filename_2 > filename_3

Instance:

The command cat culmination.txt vegetable.txt > grocery.txt will concatenate the contents of culmination.txt and vegetable.txt, then redirect the output to a report referred to as grocery.txt. If both of the enter information doesn’t exist, an error message will likely be displayed within the terminal, however you received’t see the concatenated contents within the terminal as a result of they’re being redirected to grocery.txt.

Assume culmination.txt accommodates:

Apple
Banana
Cherry

And vegetable.txt accommodates:

Carrot
Lettuce
Tomato

After working the command, the terminal received’t display any output (except there’s an error), however the report grocery.txt will comprise:

Apple
Banana
Cherry
Carrot
Lettuce
Tomato

If vegetable.txt was once misspelled or doesn’t exist, you could see an error within the terminal very similar to:

cat: vegetable.txt: No such report or listing
Extra Linux instructions:
Listing Operations rmdir · cd · pwd · exa · ls
Record Operations cat · cp · dd · much less · contact · ln · rename · extra · head
Record Device Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Seek and Textual content Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Device Knowledge and Control env · historical past · most sensible · who · htop · glances · lsof
Person and Consultation Control display · su · sudo · open

The publish Use the Cat Command in Linux seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-cat/

[ continue ]