Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 2. Linux Key Commands

cat (Display File Content)

cat (Display File Content)

Viewing File Contents with cat Command

The cat (conCATenate) command is used to display the contents of a file without opening it. The command can be used to create a new file with redirection of standard output (>). To know more about redirection of standard output, check Chapter 5 Standard Input Output and Redirection.

Display the content of a file

To display the content of a file, use the file path as an argument of the cat command.

For example, if you want to display the content of file_a under your working directory, run the command below.

Command Line - INPUT
cat file_a
Command Line - RESPONSE
file_a content

Create and edit a file with the cat command

Before explaining how to create or edit a file with the cat command, let's check how the cat command works without any argument.

When running the cat command without any argument, the cursor moves to the beginning of the line below and the command line waits for your input. After typing some words and hitting the enter key...

Command Line - INTERACTIVE
ubuntu $ | cat
test test

... you'll see that the command line returns the same text as the one you typed.

Command Line - INTERACTIVE
ubuntu $ | cat
test test
test test

You stay in the typing mode until you quit it by pressing Ctrl + C or Ctrl + D.

Redirecting standard output

You can redirect the output to a new file or an existing file using the redirection special character (> or >>) followed by the file name.

There are differences between > and >>.
> : overwrite contents
>> : add contents

Note: Redirection and standard output will be explained in Chapter 5 Standard Input Output and Redirection.

1. Create a new file with text content

When you use > followed by a file path with a new file name, you can create a new file. For example, to create a new file named sample_cat.txt under your working directory, run the command below.

Command Line - INPUT
cat > sample_cat.txt

After hitting the enter key, you can type multiple lines of text. For example, you can type some lines like shown below.

Command Line - INTERACTIVE
sample text
sample text
sample text

Quit the typing mode with Ctrl + C. By running the ls command, you can confirm that there is a new file named sample_cat.txt. By running the cat command, you can see that the new file contains the text you typed.

Command Line - INPUT
ls
Command Line - RESPONSE
sample_cat.txt
Command Line - INPUT
cat sample_cat.txt
Command Line - RESPONSE
sample text
sample text
sample text

2. Add text to an existing file

When you use >> followed by an existing file path, you can add contents to the existing file. For example, to add new text to the sample_cat.txt file, run the command below.

Command Line - INPUT
cat >> sample_cat.txt

After hitting the enter key, you can type multiple lines of text. For example, you can type some lines like shown below.

Command Line - INTERACTIVE
added text
added text

After quitting the typing mode with Ctrl + C and running the cat command, you can confirm that the new lines of text are added to the file.

Command Line - INPUT
cat sample_cat.txt
Command Line - RESPONSE
sample text
sample text
sample text
added text
added text

3. Overwrite an existing file

When you use > followed by an existing file path, you can overwrite the content of the file. For example, to overwrite the sample_cat.txt file, run the command below.

Command Line - INPUT
cat > sample_cat.txt

After hitting the enter key, you can type multiple lines of text. For example, you can type the lines shown below.

Command Line - INTERACTIVE
overwrite text
overwrite text

After quitting the typing mode with Ctrl + C and running the cat command, you can confirm that the file was overwritten with the text you typed.

Command Line - INPUT
cat sample_cat.txt
Command Line - RESPONSE
overwrite text
overwrite text

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Deleting Text in Vim Editor Normal Mode

Normal Mode (2) – Delete

Deleting Text in Vim Editor Normal Mode

Normal Mode (2) – Delete

Tags:

Linux Command

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server