Almost all server work starts in a shell. This post covers the small set of commands and shortcuts that make up most of a working day, plus the habits that prevent the classic mistakes (deleting the wrong directory, running a command on the wrong server).
Versions in use on this date. Ubuntu 18.04.2 LTS ships bash 4.4 and GNU coreutils 8.28. CentOS 7.6 has bash 4.2, and everything shown here works on both.
What happens when you press Enter
flowchart TB
K[You type a line] --> RL[Readline<br/>editing and history]
RL --> EX[Expansion<br/>variables globs tilde]
EX --> LK{Command lookup}
LK -->|alias or function| AL[Run alias or function]
LK -->|builtin like cd| BI[Run inside the shell]
LK -->|file in PATH| EXE[Fork and exec program]
AL --> OUT[Output and exit code in $?]
BI --> OUT
EXE --> OUT
The shell expands *, ~ and $VAR before the program sees them. That is why rm *.log removes whatever the glob matched at that moment, and why quoting matters.
Moving and looking
pwd # where am I
ls -lah # long, all files, human sizes
ls -lt | head # newest first
cd /var/log # absolute path
cd - # back to the previous directory
cd # home
pushd /etc; popd # directory stack
tree -L 2 /etc/nginx # (apt install tree) quick overview
Finding things
find /var/log -name '*.log' -size +100M # big logs
find /home -type f -mtime -1 # changed in last 24 hours
find /tmp -type f -mtime +7 -print # preview before deleting
find /tmp -type f -mtime +7 -delete # then delete
locate nginx.conf # fast, uses a database (sudo updatedb)
grep -rn "listen 80" /etc/nginx/ # search file contents
Always run find ... -print first and only add -delete once the list looks right.
Copy, move, delete safely
cp -a source/ backup/ # archive mode keeps permissions and times
mv old.conf old.conf.bak # rename is a move
rm -i file # asks first
mkdir -p a/b/c # create parents
ls -ld /path/to/dir # check before: rm -rf /path/to/dir
There is no recycle bin. rm -rf with a typo in a variable (rm -rf $DIR/, with DIR empty) is a well-known way to wipe a server, so use set -u in scripts and read the line twice.
Viewing files
less /var/log/syslog # q to quit, /text to search, G for end, F to follow
head -n 20 file; tail -n 50 file
tail -f /var/log/auth.log # follow new lines, Ctrl-C to stop
wc -l file # count lines
file mystery.bin # what type is it
Shortcuts worth learning first
| Keys | Effect |
|---|---|
Tab (twice) |
Complete / list matches |
Ctrl-R |
Search command history |
Ctrl-A / Ctrl-E |
Start / end of line |
Ctrl-W / Ctrl-U |
Delete word / to line start |
Ctrl-L |
Clear screen |
!! |
Repeat last command (sudo !! after forgetting sudo) |
Alt-. |
Last argument of the previous command |
Prompt and aliases
Make the prompt show user, host and directory so you never type on the wrong server:
# ~/.bashrc
PS1='\u@\h:\w\$ '
alias ll='ls -lah'
alias grep='grep --color=auto'
HISTSIZE=10000
HISTCONTROL=ignoredups:ignorespace # a leading space keeps a command out of history
Run source ~/.bashrc to load changes. Use man ls or ls --help before searching the web; the manual is already on the machine.