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 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Standard Input Output and Redirection

Managing Standard Input and Output with Redirection

Standard Input, Output and Error Output

In general, computer commands or programs process input and generate output. Standard input and output define where data come from and where processed data go to.

Standard Input (Stdin)

Standard input defines where input data comes from. Linux OS's preset standard input is generally the keyboard. When you type in the command line, the typed string becomes the input of the command.

Standard Output (Stdout)

Standard output defines where output data are generated. Linux OS's preset standard output is generally command-line (terminal).

Standard Error (Stderr)

Linux OS's preset standard error output is generally command-line (terminal).

Here is the summary of the above.

Linux Standard Input, Output and Error Output

Redirection

Redirection is used to change Stdin, Stdout, or Stderr. In programming, the most frequently used redirection option is redirection via a text file.

Redirection of stdin (<)

To redirect standard input, use < after a command followed by new standard input.

Linux redirection of stdin

For example, if you want to sort the file_a contents, run the command below.

Command Line - INPUT
sort < file_a

Redirection of stdout (> or >>)

To redirect standard output, use > or >> after a command followed by a new standard output.

When you use >, the output will overwrite the existing contents while when you use >>, the output will be added at the end of the existing contents.

Linux redirection of stdout

For example, run the following command to save the output of the ls command for the / (root) directory in the list.txt file. To avoid a permission error, run the command as the superuser.

Command Line - INPUT
sudo su
ls / > list.txt

You can see that the list of directories and files under the / (root) directory is saved in the file.

Command Line - INPUT
cat list.txt
Command Line - RESPONSE
bin
boot
dev
:

Following the above process, run the two commands. One is to list directories and files of the /usr directory with > and the other is to list the same directory with >>.

First, run the command with >> shown below. You can see that the list of the /usr directory is added after the /(root) directory list.

Command Line - INPUT
ls /usr >> list.txt
cat list.txt
Command Line - RESPONSE
bin
boot
dev
etc
home
lib
lib32
lib64
libx32
lost+found
:
bin
games
include
lib
lib32
lib64
libexec
libx32
local
sbin
share
src

Next, run the same command but with > like shown below. You can see that the list of the /usr directory overwrote the original content of the file.

Command Line - INPUT
ls /usr > list.txt
cat list.txt
Command Line - RESPONSE
bin
games
include
lib
lib32
lib64
libexec
libx32
local
sbin
share
src

Redirection of stderr (2> or 2>>)

To redirect a standard error, use 2> or 2>> after a command followed by a new standard error.

Linux redirection of stderr

When you use 2>, the output will overwrite the existing contents while when you use 2>>, the output will be added at the end of the existing contents. To see how it works, run the command below as a normal user. As the normal user doesn't have the read permission to the /lost+found directory, you'll get an error message.

Command Line - INPUT
ls /lost+found
Command Line - RESPONSE
ls: cannot open directory '/lost+found': Permission denied

If you want to redirect the error message to the error.txt file, run the same command with redirection. By running the cat command, you can see that the error message is saved in the error file.

Command Line - INPUT
ls /lost+found 2> error.txt
cat error.txt
Command Line - RESPONSE
ls: cannot open directory '/lost+found': Permission denied

Redirection of stdout and stderr in the same file

If you want to record standard output and error in the same file, use >& symbol.

Linux redirection of stdout and stderr in the same file

Redirection of stderr (2> or 2>>)

To redirect a standard error, use 2> or 2>> after a command followed by a new standard error.

When you use 2>, the output will overwrite the existing contents, while when you use >>, the output will be added at the end of the existing contents.

Redirection of stdin and stdout with the same command

You can redirect both standard input and output with the same command.

Linux redirection of stdin and stdout with the same command

For example, to reverse the order of the content of the list.txt file and save it in the list_r.txt file, run the command below. By running the cat command, you can see that the item order has been reversed in the list_r.txt file.

Command Line - INPUT
sort -r < list.txt > list_r.txt
cat list_r.txt
Command Line - RESPONSE
src
share
sbin
local
libx32
libexec
lib64
lib32
lib
include
games
bin

Here is the summary of redirection symbols.

Linux redirection symbol summary

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Understanding CRUD in Web Applications

CRUD Web Application

Working with Wildcards in Linux

Wildcard

Git & GitHub Overview

Chapter 1. Git & GitHub Overview

Git & GitHub Basic Life Cycle

Git & GitHub Basic Life Cycle

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Understanding CRUD in Web Applications

CRUD Web Application

Working with Wildcards in Linux

Wildcard

Git & GitHub Overview

Chapter 1. Git & GitHub Overview

Git & GitHub Basic Life Cycle

Git & GitHub Basic Life Cycle

Chapter 6. Docker Compose and Deployment Practice

Chapter 6. Docker Compose and Deployment Practice

Tags:

Redirection

Standard Input

Standard Output

Standard Error

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