shell-mastery — Server Survival
Quick Survival Commands (any Linux server)
Where am I, who am I
whoami; hostname; uname -a; cat /etc/os-release
pwd; echo $SHELL; echo $0
What shell is available
cat /etc/shells
chsh -l # same thing on some distros
echo $SHELL # current default
echo $0 # current running shell
If only sh is available (POSIX-only survival)
# No arrays, no [[ ]], no process substitution
# Use: [ ], $(cmd), test, case, while read
while IFS= read -r line; do
case "$line" in
*error*) echo "FOUND: $line" ;;
esac
done < logfile.txt
Check what tools exist
for cmd in vim vi nano awk sed perl python3 python curl wget jq yq; do
command -v "$cmd" >/dev/null 2>&1 && printf "%-10s %s\n" "$cmd" "$(command -v $cmd)" || printf "%-10s MISSING\n" "$cmd"
done
History tricks (works in bash and zsh)
!! # repeat last command
!$ # last argument of previous command
!^ # first argument of previous command
!:2 # second argument of previous command
!cmd # run most recent command starting with 'cmd'
^old^new # replace old with new in last command
ctrl-r # reverse search history