Use the Cat Command in Linux

by | Oct 16, 2023 | Etcetera | 0 comments

The cat command, fast for “concatenate,” is a commonly-used software in Linux, enabling consumers to view, create, and listen knowledge, or redirect their output. In this put up, we’ll uncover how each and every new and professional Linux consumers can leverage the cat command for fairly numerous tasks.

The entire syntax of the cat command is as apply:

$ cat [OPTION] [FILE]...
1. Use cat to create a brand spanking new file and add content material subject matter
cat > filename

Example:

The command cat > vegetables.txt takes the standard input and redirects it to a file named “vegetables.txt”. When you execute this command, your terminal doesn’t display any output, on the other hand instead waits in an effort to enter text from the keyboard.

Regardless of you type might be written to “vegetables.txt”. To finish and save the file, you’ll need to press CTRL-D (or CTRL-Z on House home windows ways using some terminal techniques).

See also  6 Perfect Buyer Critiques Plugins for WordPress When put next (2023)

Proper right here’s an example of the best way likelihood is that you’ll use it:

$ cat > vegetables.txt
Carrot
Broccoli
Spinach
^D

At this stage, a file named “vegetables.txt” can also be created with the following content material subject matter:

Carrot
Broccoli
Spinach
2. Display file’s content material subject matter with cat
cat filename

Example:

Suppose the file comprises a list of now not ordinary vegetables; executing the command cat vegetables.txt would display the contents of the file inside the terminal.

Carrots
Broccoli
Spinach
Tomatoes
Cucumbers
Peppers
Onions
Potatoes
Kale
Lettuce
3. Display content material subject matter of multiple knowledge with cat
cat filename_1 filename_2

Example:

Assuming the contents of the guidelines end result.txt and vegetables.txt are respectively as follows:

Apple
Banana
Cherry
Carrot
Lettuce
Tomato

The command cat end result.txt vegetables.txt would produce:

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

Example:

If the contents of end result.txt are the identical as inside the previous example:

Apple
Banana
Cherry

Then the command cat -n end result.txt would produce:

     1	Apple
     2	Banana
     3	Cherry
5. Copy, change, or replicate a file’s content material subject matter using cat
cat filename new_filename

Example:

The command cat end result.txt > new_fruits.txt will take the contents of end result.txt and write them into a brand spanking new file named new_fruits.txt. This command won’t display any output to the terminal, assuming it executes successfully.

If end result.txt has the identical content material subject matter as forward of:

Apple
Banana
Cherry

After operating the command, the content material subject matter of new_fruits.txt might be:

Apple
Banana
Cherry
6. Merge multiple knowledge’ content material subject matter into one with cat
cat filename_1 filename_2 > filename_3

Example:

See also  How you can Transform a Backend Developer: Essential Talents to Be informed

The command cat end result.txt vegetable.txt > grocery.txt will concatenate the contents of end result.txt and vegetable.txt, then redirect the output to a file known as grocery.txt. If either one of the input knowledge doesn’t exist, an error message might be displayed inside the terminal, on the other hand you won’t see the concatenated contents inside the terminal on account of they’re being redirected to grocery.txt.

Suppose end result.txt comprises:

Apple
Banana
Cherry

And vegetable.txt comprises:

Carrot
Lettuce
Tomato

After operating the command, the terminal won’t show any output (with the exception of there’s an error), on the other hand the file grocery.txt will contain:

Apple
Banana
Cherry
Carrot
Lettuce
Tomato

If vegetable.txt used to be as soon as misspelled or doesn’t exist, it’s essential see an error inside the terminal similar to:

cat: vegetable.txt: No such file or record
Additional Linux directions:
Record Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · much less · contact · ln · rename · extra · head
File Software Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Search and Text Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Software Wisdom and Keep an eye on env · historical past · most sensible · who · htop · glances · lsof
Particular person and Session Keep an eye on display · su · sudo · open

The put up Use the Cat Command in Linux appeared first on Hongkiat.

See also  8 Highest AI Gross sales Equipment to Maximize Income for Your Trade in 2023

WordPress Website Development

Supply: https://www.hongkiat.com/blog/linux-command-cat/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!