Based on the Linux Master 2 exam syllabusReport an error
> overwrites, >> appends. If you mix these two up, the file is gone.2>. > alone does not put error messages in the file.alias, remove it with unalias. To keep it, write it in ~/.bashrc.history, and re-run commands with !! (the previous one) and !number. ┌─────────────┐
Std input (0) ──►│ Command │─► Std output (1) results
keyboard │ │─► Std error (2) error messages
└─────────────┘ screen| Name | Number | Default connection |
|---|---|---|
| Standard input (stdin) | 0 | Keyboard |
| Standard output (stdout) | 1 | Screen |
| Standard error (stderr) | 2 | Screen |
ls > list.txt # result to a file (overwrite)
ls >> list.txt # result to the end of a file (append)| Symbol | What 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 likecat a.txt > a.txtwipes the file entirely. For logs that keep a record, use>>.
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."2>&1 Mattersls /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 directory2>&1 means "send 2 to wherever 1 points right now."2>&1, 1 still points at the screen. Only afterward does 1 change to the file, so the error stays on the screen.& and write 2>1, the error is saved in a file named 1.| Notation | Where standard output goes | Where standard error goes |
|---|---|---|
command > a | a | Screen |
command 2> a | Screen | a |
command > a 2> b | a | b |
command > a 2>&1 | a | a |
command &> a | a | a |
command 2>&1 > a | a | Screen |
command > /dev/null 2>&1 | Discarded | Discarded |
> is shorthand for 1>. If you omit the number, it is 1 (standard output).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> 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.> file with no command, an empty file is created. If the file already exists, its contents are emptied.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| Symbol | Where input comes from |
|---|---|
< | A file |
<< | Content typed directly until an end marker appears (Here Document) |
cat << EOF > hello.txt
first line
second line
EOF<< (EOF) appears again. You can choose any word.cat > memo.txt, whatever you type on the keyboard is saved to the file. When you are done typing, finish with Ctrl+D.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.| Category | Redirection > | Pipe |
|---|---|---|
| Passes to | A file | The next command |
| Example | ls > a.txt | ls | 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 too2>&1.ls | list.txt, it tries to run list.txt as a command and fails.| Symbol | Condition for the following command to run | Example |
|---|---|---|
; | In sequence, regardless of whether the previous one succeeded | cd /tmp ; ls |
&& | Only if the previous one succeeded | mkdir work && cd work |
|| | Only if the previous one failed | cd work || echo "no dir" |
| | Always — takes the previous one's output as input | ls | wc -l |
& | Sends the preceding command to the background | sleep 100 & |
| and ||, and & and &&, are completely different symbols. One character changes the meaning.$? we saw yesterday. 0 means success.| Command | What it does |
|---|---|
sort | Sort (-r reverse, -n numeric) |
uniq | Removes consecutive duplicate lines (usually after sort) |
wc | Counts of lines, words, characters |
head / tail | The first / last part |
grep | Lines that match a condition |
cut | Cut out columns (-d delimiter, -f field) |
tee | Shows 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 timecut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn
# 24 /sbin/nologin
# 3 /bin/bash
# 1 /sbin/shutdown
# 1 /sbin/halt
# 1 /bin/syncuniq compares only adjacent lines. It cannot catch duplicates that are apart, so run sort first. -c also shows the counts.wc, -l is lines, -w is words, -c is bytes.tee overwrites by default. To append, use tee -a.| Symbol | Meaning |
|---|---|
* | 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* 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*.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 .*.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 ingrepregular expressions*means "the character just before it repeated zero or more times." In regular expressions, "any single character" is..
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 commandunalias is the delete command. Remember it by the name.~/.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'rm runs rm -i, which asks before deleting.| Ways to bypass an alias | Example |
|---|---|
| A backslash in front | \rm file |
| Run with an absolute path | /usr/bin/rm file |
command in front | command rm file |
alias ll=ls -l is an error.unalias -a removes all defined aliases.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 # 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| Notation | What it does |
|---|---|
!! | Re-runs the previous command |
!number | Re-runs the command with that number |
!string | The most recent command starting with that string |
Ctrl+R | Reverse search as you type |
~/.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."| Notation | What it does |
|---|---|
!$ | The last argument of the previous command |
!-2 | Re-runs the command two back (!-1 is the same as !!) |
!?string? | The most recent command that contains that string |
^old^new | Re-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 / file | Role |
|---|---|
HISTSIZE | Number of commands the shell remembers in memory |
HISTFILESIZE | Number of lines kept in the file |
HISTFILE | Location of the history file (default ~/.bash_history) |
~/.bash_history when the shell exits. That is why, if you keep two terminals open, you cannot see each other's latest commands.HISTSIZE varies with distribution settings. The Red Hat family sets it to 1000 in /etc/profile.| Key | What it does |
|---|---|
Tab | Autocompletes command and file names (press twice for a list of candidates) |
↑ / ↓ | Recall previous / next command |
Ctrl+A / Ctrl+E | Go to the start / end of the line |
Ctrl+U | Delete everything before the cursor |
Ctrl+L | Clear the screen (same as clear) |
Ctrl+C | Cancel the line being typed or the running command |
Ctrl+D | End 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.
>, >>, 2>, <./dev/null : A special device file that discards whatever is sent to it.|) : A symbol that passes the output of the earlier command as the input of the later command.unalias.~/.bash_history.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?