CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
  • Week 4
  • Week 5
    • 1.What Is a Shell?
    • 2.Shell Variables and Environment Variables
    • 3.Environment Configuration Files
    • 4.Redirection · Aliases · History
    • 5.Week 5 Comprehensive Review: The Shell on One Page
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 5/Day 4

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 5 · Day 4~19 min read

Day 4 - Redirection · Aliases · History

📌 Key Points

  • Every command has three channels: standard input (0), standard output (1), and standard error (2).
  • > overwrites, >> appends. If you mix these two up, the file is gone.
  • Redirect errors separately with 2>. > alone does not put error messages in the file.
  • Create an alias with alias, remove it with unalias. To keep it, write it in ~/.bashrc.
  • View history with history, and re-run commands with !! (the previous one) and !number.

Three Channels

                  ┌─────────────┐
 Std input (0) ──►│   Command   │─► Std output (1)   results
    keyboard      │             │─► Std error (2)    error messages
                  └─────────────┘        screen
NameNumberDefault connection
Standard input (stdin)0Keyboard
Standard output (stdout)1Screen
Standard error (stderr)2Screen
  • Expect questions that ask for the numbers 0, 1, 2 directly. Input is 0, output is 1, error is 2.
  • Output and error both come out on the screen, but they are different channels. That is why they can be redirected separately.

Redirecting Output

ls > list.txt         # result to a file (overwrite)
ls >> list.txt        # result to the end of a file (append)
SymbolWhat it does
>Standard output to a file — erasing the existing contents
>>Standard output to a file — appended at the end
2>Standard error to a file (overwrite)
2>>Standard error to a file (append)
&>Output and error together
<A file as standard input

⚠️ > empties the file first. A command like cat a.txt > a.txt wipes the file entirely. For logs that keep a record, use >>.

Errors Only, Separately

find / -name "*.conf" > result.txt          # only results to a file, errors on screen
find / -name "*.conf" 2> error.txt          # only errors to a file
find / -name "*.conf" > out.txt 2> err.txt  # each separately
find / -name "*.conf" &> all.txt            # both into one file
find / -name "*.conf" 2> /dev/null          # discard errors
  • /dev/null is where you throw things away. Anything sent there disappears. It is useful when lots of permission-error messages come out.
  • > out.txt 2>&1 is also a notation for merging the two. 2>&1 means "send errors to the same place output goes."

The Order of 2>&1 Matters

ls /etc /nodir > out.txt 2>&1     # both output and error go to out.txt
ls /etc /nodir 2>&1 > out.txt     # only output goes to out.txt, error still on screen
# ls: cannot access '/nodir': No such file or directory
  • The shell processes redirections from left to right. 2>&1 means "send 2 to wherever 1 points right now."
  • In the second line, at the time of 2>&1, 1 still points at the screen. Only afterward does 1 change to the file, so the error stays on the screen.
  • If you drop the & and write 2>1, the error is saved in a file named 1.
NotationWhere standard output goesWhere standard error goes
command > aaScreen
command 2> aScreena
command > a 2> bab
command > a 2>&1aa
command &> aaa
command 2>&1 > aaScreen
command > /dev/null 2>&1DiscardedDiscarded
  • > is shorthand for 1>. If you omit the number, it is 1 (standard output).

noclobber to Prevent Overwrite Accidents

set -o noclobber      # turn on
ls > list.txt
# bash: list.txt: cannot overwrite existing file
ls >| list.txt        # force overwrite just this once
set +o noclobber      # turn off
  • With this option on, > cannot overwrite an existing file. >> (append) is not affected.
  • -o turns it on and +o turns it off. The signs are the opposite of what intuition suggests.
  • If you write only > file with no command, an empty file is created. If the file already exists, its contents are emptied.

Redirecting Input

wc -l < list.txt        # file as input
sort < names.txt
  • < takes input from a file instead of the keyboard.
wc -l list.txt       # 12 list.txt   ← the file name is printed too
wc -l < list.txt     # 12            ← no name, because only the contents were passed in
SymbolWhere input comes from
<A file
<<Content typed directly until an end marker appears (Here Document)
cat << EOF > hello.txt
first line
second line
EOF
  • Input is taken until the word written after << (EOF) appears again. You can choose any word.
  • If you redirect only the output without input, as in cat > memo.txt, whatever you type on the keyboard is saved to the file. When you are done typing, finish with Ctrl+D.

Connecting with Pipes

ps -ef | grep httpd | wc -l      # you can chain several
cat /etc/passwd | sort | head -5
ls -l | grep "^d"                # directories only
  • | (pipe) passes the standard output of the earlier command as the standard input of the later command.
  • It differs from redirection in that it passes to a command, not to a file.
CategoryRedirection >Pipe
Passes toA fileThe next command
Examplels > a.txtls | grep txt
find / -name "*.conf" | wc -l                 # error messages do not travel through the pipe and appear on screen as is
find / -name "*.conf" 2> /dev/null | wc -l    # discard errors and count only the results
find / -name "*.conf" 2>&1 | grep denied      # pass errors through the pipe too
  • A pipe passes only standard output (1). To pass errors too, merge them first with 2>&1.
  • Both sides of a pipe must be commands. If you write ls | list.txt, it tries to run list.txt as a command and fails.

Other Symbols That Connect Commands

SymbolCondition for the following command to runExample
;In sequence, regardless of whether the previous one succeededcd /tmp ; ls
&&Only if the previous one succeededmkdir work && cd work
||Only if the previous one failedcd work || echo "no dir"
|Always — takes the previous one's output as inputls | wc -l
&Sends the preceding command to the backgroundsleep 100 &
  • | and ||, and & and &&, are completely different symbols. One character changes the meaning.
  • The criterion for success or failure is the exit status $? we saw yesterday. 0 means success.

Filters Often Used Together

CommandWhat it does
sortSort (-r reverse, -n numeric)
uniqRemoves consecutive duplicate lines (usually after sort)
wcCounts of lines, words, characters
head / tailThe first / last part
grepLines that match a condition
cutCut out columns (-d delimiter, -f field)
teeShows on screen while also saving to a file
cut -d: -f1 /etc/passwd | sort        # extract only user names and sort
ls -l | tee list.txt                  # watch and save at the same time
cut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
#      24 /sbin/nologin
#       3 /bin/bash
#       1 /sbin/shutdown
#       1 /sbin/halt
#       1 /bin/sync
  • It extracts only the login shell (7th field) → sorts → counts identical ones → sorts again from most to least. This is the typical form of doing a big job by chaining small commands with pipes.
  • uniq compares only adjacent lines. It cannot catch duplicates that are apart, so run sort first. -c also shows the counts.
  • For wc, -l is lines, -w is words, -c is bytes.
  • tee overwrites by default. To append, use tee -a.

Wildcards

SymbolMeaning
*Any number of characters (including zero)
?Exactly one character
[abc]One character from inside the brackets
[a-z]One character within the range
[!abc]One character that is not inside the brackets
ls *.txt          # everything ending in txt
ls a?.txt         # a1.txt, ab.txt — one character after a
ls [ab]*.txt      # txt files starting with a or b
  • The difference between * and ? is an exam point. * has no limit on count, while ? is exactly one character.
ls
# a.txt  a1.txt  a12.txt  b1.txt  note.md
ls a?.txt         # a1.txt                  ← a.txt (0 characters) and a12.txt (2 characters) are left out
ls a*.txt         # a.txt  a1.txt  a12.txt
ls [!a]*          # b1.txt  note.md
ls *.xyz
# ls: cannot access '*.xyz': No such file or directory
  • It is the shell that replaces wildcards with file names. If no file matches, the literal text *.xyz is passed to the command, producing the error above.
  • * does not match hidden files that start with a dot (.). For hidden files, write the dot yourself, as in ls .*.
  • To stop the shell from expanding it, wrap it in quotes. This is why find . -name "*.txt" has quotes — find, not the shell, must interpret the *.

⚠️ Wildcards and regular expressions are different. The * used in file names means "any number of any characters," but in grep regular expressions * means "the character just before it repeated zero or more times." In regular expressions, "any single character" is ..

Aliases — alias

alias                       # list of currently defined aliases
alias ll='ls -alh'          # create
alias rm='rm -i'            # ask before deleting
unalias ll                  # remove
\ls                         # ignore the alias and run the original command
  • unalias is the delete command. Remember it by the name.
  • Do not put spaces on either side of the equals sign. It is the same rule as for variables.
  • An alias created on the command line disappears when you close the shell. To keep using it, write it in ~/.bashrc.
alias
# alias cp='cp -i'
# alias ll='ls -l --color=auto'
# alias ls='ls --color=auto'
# alias mv='mv -i'
# alias rm='rm -i'
alias ll
# alias ll='ls -l --color=auto'
type ll
# ll is aliased to `ls -l --color=auto'
  • An alias takes effect before the real command of the same name. In the environment above, typing rm runs rm -i, which asks before deleting.
Ways to bypass an aliasExample
A backslash in front\rm file
Run with an absolute path/usr/bin/rm file
command in frontcommand rm file
  • If the value contains spaces or options, wrap it in quotes. alias ll=ls -l is an error.
  • unalias -a removes all defined aliases.
  • Aliases cannot be passed with export. They are not passed to child shells or scripts, so write them in ~/.bashrc, which is read every time a shell is launched.

History

history               # list of executed commands (with numbers)
history 10            # the last 10
!!                    # re-run the previous command
!25                   # re-run command number 25
!ls                   # the most recent command starting with ls
history -c            # clear the list
NotationWhat it does
!!Re-runs the previous command
!numberRe-runs the command with that number
!stringThe most recent command starting with that string
Ctrl+RReverse search as you type
  • History is saved in ~/.bash_history, and its size is set by the environment variable HISTSIZE.
  • sudo !! is a common idiom meaning "run the last command again with sudo."
NotationWhat it does
!$The last argument of the previous command
!-2Re-runs the command two back (!-1 is the same as !!)
!?string?The most recent command that contains that string
^old^newRe-runs the previous command with text replaced
mkdir /var/www/project
cd !$                   # runs as cd /var/www/project
history | tail -3
#   101  mkdir /var/www/project
#   102  cd /var/www/project
#   103  history | tail -3
  • !! is the whole command, and !$ is one last argument. Notations starting with ! are replaced with the actual command before execution, shown once on screen, and then run.
Variable / fileRole
HISTSIZENumber of commands the shell remembers in memory
HISTFILESIZENumber of lines kept in the file
HISTFILELocation of the history file (default ~/.bash_history)
  • History is not written to the file the moment you type it. It is recorded in ~/.bash_history when the shell exits. That is why, if you keep two terminals open, you cannot see each other's latest commands.
  • The value of HISTSIZE varies with distribution settings. The Red Hat family sets it to 1000 in /etc/profile.

Keys That Help with Input

KeyWhat it does
TabAutocompletes command and file names (press twice for a list of candidates)
↑ / ↓Recall previous / next command
Ctrl+A / Ctrl+EGo to the start / end of the line
Ctrl+UDelete everything before the cursor
Ctrl+LClear the screen (same as clear)
Ctrl+CCancel the line being typed or the running command
Ctrl+DEnd of input. Pressed at an empty prompt, it logs out

Writing commands chained like this into a file and running them all at once is a shell script. How to write them is within the Level 1 scope, so just know the name here.

Tomorrow we wrap up this week's shell content on a single page.

📖 Glossary

  • Standard input (stdin, 0) : The channel through which a command receives input. The default is the keyboard.
  • Standard output (stdout, 1) : The channel through which a command sends its results. The default is the screen.
  • Standard error (stderr, 2) : The channel through which error messages go out. Separate from output.
  • Redirection : Changing the direction of input/output to a file, etc. >, >>, 2>, <.
  • /dev/null : A special device file that discards whatever is sent to it.
  • Pipe (|) : A symbol that passes the output of the earlier command as the input of the later command.
  • Alias : A feature that gives a short name to a long command. Removed with unalias.
  • History : The record of executed commands. Saved in ~/.bash_history.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which of the following symbols saves a command's result at the end of an existing file without erasing its contents?

Question 2

Which of the following symbols saves only the error messages produced while a command runs to a file?

Question 3

Which of the following lists the file descriptor numbers of standard input, standard output, and standard error, in that order?

Question 4

Which of the following commands removes a defined alias?

Question 5

Which of the following notations re-runs the command executed immediately before?

PreviousEnvironment Configuration FilesWeek 5 · Day 3Next Week 5 Comprehensive Review: The Shell on One PageWeek 5 · Day 5

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Points
  • Three Channels
  • Redirecting Output
  • Errors Only, Separately
  • The Order of `2>&1` Matters
  • noclobber to Prevent Overwrite Accidents
  • Redirecting Input
  • Connecting with Pipes
  • Other Symbols That Connect Commands
  • Filters Often Used Together
  • Wildcards
  • Aliases — alias
  • History
  • Keys That Help with Input
  • Glossary
  • Practice Questions