Skip to content

Instantly share code, notes, and snippets.

@pida42
Last active October 31, 2021 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pida42/5180e8f7cc743ecf5c023169e64e4b56 to your computer and use it in GitHub Desktop.
Save pida42/5180e8f7cc743ecf5c023169e64e4b56 to your computer and use it in GitHub Desktop.
Admin's command line cheat sheet

Admin's command line cheat sheet

Arguments, variables and parameters

Argumets

$#	                                    Number of arguments
$*	                                    All arguments
$@	                                    All arguments, starting from first
$1	                                    First argument
$_	                                    Last argument of the previous command

Default values

${FOO:-val}	                            $FOO, or val if not set
${FOO:=val}	                            Set $FOO to val if not set
${FOO:+val}	                            val if $FOO is set
${FOO:?message}	                        Show error message and exit if $FOO is not set

The :is optional (eg, ${FOO=word} works).

Numeric calculations

$((a + 200))                            # Add 200 to $a
$((RANDOM%=200))                        # Random number 0..200

Parameter expansions

name="John"
echo ${name}
echo ${name/J/j}                        #=> "john" (substitution)
echo ${name:0:2}                        #=> "Jo" (slicing)
echo ${name::2}                         #=> "Jo" (slicing)
echo ${name::-1}                        #=> "Joh" (slicing)
echo ${name:(-1)}                       #=> "n" (slicing from right)
echo ${name:(-2):1}                     #=> "h" (slicing from right)
echo ${food:-Cake}                      #=> $food or "Cake"
STR="/path/to/foo.cpp"
echo ${STR%.cpp}                        # /path/to/foo
echo ${STR%.cpp}.o                      # /path/to/foo.o
echo ${STR##*.}                         # cpp (extension)
echo ${STR##*/}                         # foo.cpp (basepath)
echo ${STR#*/}                          # path/to/foo.cpp
echo ${STR##*/}                         # foo.cpp
echo ${STR/foo/bar}                     # /path/to/bar.cpp
STR="Hello world"
echo ${STR:6:5}                         # "world"
echo ${STR:-5:5}                        # "world"
SRC="/path/to/foo.cpp"
BASE=${SRC##*/}                         #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE}                        #=> "/path/to/" (dirpath)

Special variables

$?	                                    Exit status of last task
$!	                                    PID of last background task
$$	                                    PID of shell
$0	                                    Filename of the shell script

String manipulation

STR="HELLO WORLD!"
echo ${STR,}                            #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,}                           #=> "hello world!" (all lowercase)
STR="hello world!"
echo ${STR^}                            #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^}                           #=> "HELLO WORLD!" (all uppercase)

Length

${#FOO}	                                Length of $FOO

Substrings

${FOO:0:3}	                            Substring (position, length)
${FOO:-3:3}	                            Substring from the right

Substitution

${FOO%suffix}	                        Remove suffix
${FOO#prefix}	                        Remove prefix
${FOO%%suffix}	                        Remove long suffix
${FOO##prefix}	                        Remove long prefix
${FOO/from/to}	                        Replace first match
${FOO//from/to}	                        Replace all
${FOO/%from/to}	                        Replace suffix
${FOO/#from/to}	                        Replace prefix

Conditions

Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.

String, numeric and expression conditions

[[ -z STRING ]]	                        Empty string
[[ -n STRING ]]	                        Not empty string
[[ STRING == STRING ]]	                Equal
[[ STRING != STRING ]]	                Not Equal
[[ NUM -eq NUM ]]	                    Equal
[[ NUM -ne NUM ]]	                    Not equal
[[ NUM -lt NUM ]]	                    Less than
[[ NUM -le NUM ]]	                    Less than or equal
[[ NUM -gt NUM ]]	                    Greater than
[[ NUM -ge NUM ]]	                    Greater than or equal
[[ STRING =~ STRING ]]	                Regexp
(( NUM < NUM ))	                        Numeric conditions
[[ -o noclobber ]]	                    If OPTIONNAME is enabled
[[ ! EXPR ]]	                        Not
[[ X ]] && [[ Y ]]	                    And
[[ X ]] || [[ Y ]]	                    Or

File conditions

[[ -e FILE ]]	                        Exists
[[ -r FILE ]]	                        Readable
[[ -h FILE ]]	                        Symlink
[[ -d FILE ]]	                        Directory
[[ -w FILE ]]	                        Writable
[[ -s FILE ]]	                        Size is > 0 bytes
[[ -f FILE ]]	                        File
[[ -x FILE ]]	                        Executable
[[ FILE1 -nt FILE2 ]]	                1 is more recent than 2
[[ FILE1 -ot FILE2 ]]	                2 is more recent than 1
[[ FILE1 -ef FILE2 ]]	                Same files

Arrays and dictionaries

Arrays

Defining arrays

Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"

Working with arrays

echo ${Fruits[0]}                       # Element #0
echo ${Fruits[@]}                       # All elements, space-separated
echo ${#Fruits[@]}                      # Number of elements
echo ${#Fruits}                         # String length of the 1st element
echo ${#Fruits[3]}                      # String length of the Nth element
echo ${Fruits[@]:3:2}                   # Range (from position 3, length 2)

Operations

Fruits=("${Fruits[@]}" "Watermelon")    # Push
Fruits+=('Watermelon')                  # Also Push
Fruits=( ${Fruits[@]/Ap*/} )            # Remove by regex match
unset Fruits[2]                         # Remove one item
Fruits=("${Fruits[@]}")                 # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`)                 # Read from file

Iteration

for i in "${arrayName[@]}"; do
  echo $i
done

Dictionaries

Defining

declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"

Working with dictionaries

echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]}   # All values
echo ${!sounds[@]}  # All keys
echo ${#sounds[@]}  # Number of elements
unset sounds[dog]   # Delete dog

Iterate over values

for val in "${sounds[@]}"; do
  echo $val
done

Iterate over keys

for key in "${!sounds[@]}"; do
  echo $key
done

Hardware and system

Hard disk

hdparm -tT /dev/sda                     Perform a read speed test on disk sda

Network

tcpdump -i eth0                         Capture and display all packets on interface eth0
tcpdump -i eth0 'port 80'               Monitor all traffic on port 80 (HTTP)

Search Files

find /dir/ -name name*                  Find files starting with name in dir
find /dir/ -user name                   Find files owned by name in dir
find /dir/ -mmin num                    Find files modifed less than num minutes ago in dir
find /home/john -name 'prefix*'         Find files in /home/john that start with "prefix".
find /home -size +100M                  Find files larger than 100MB in /home

Users

lsof -u user                            List files opened by user

IO Redire­ction

cmd < file                              Input of cmd from file
cmd1 <(cmd2)                            Output of cmd2 as file input to cmd1
cmd > file                              Standard output (stdout) of cmd to file
cmd > /dev/null                         Discard stdout of cmd
cmd >> file                             Append stdout to file
cmd 2> file                             Error output (stderr) of cmd to file
cmd 1>&2                                stdout to same place as stderr
cmd 2>&1                                stderr to same place as stdout
cmd &> file                             Every output of cmd to file cmd refers to a command.

Loops

Basic for loop

for i in /etc/rc.*; do
  echo $i
done

C-like for loop

for ((i = 0 ; i < 100 ; i++)); do
  echo $i
done

Ranges

for i in {1..5}; do
  echo "Welcome $i"
done

With step size

for i in {5..50..5}; do
  echo "Welcome $i"
done

Infinite loop

while :; do
  echo "Press [CTRL+C] to stop.."
  sleep 1
done
while true; do
  echo "Press [CTRL+C] to stop.."
  sleep 1
done
while :; do echo 'Hit CTRL+C'; sleep 1; done
while true; do echo 'Hit CTRL+C'; sleep 1; done
for (( ; ; )); do
  echo "Pres CTRL+C to stop..."
  sleep 1
done
for (( ; ; )); do
  echo "Pres CTRL+C to stop..."
  sleep 1
  if (disaster-condition); then
    break
  fi
done

Miscellaneous

Case/switch

case "$1" in
  start | up)
    vagrant up
    ;;

  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac

Directory of script

DIR="${0%/*}"

Getting options

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do 
  case $1 in
    -V | --version )   echo $version; exit    ;;
    -s | --string )    shift; string=$1       ;;
    -f | --flag )      flag=1                 ;;
  esac; shift; 
done
if [[ "$1" == '--' ]]; then shift; fi

Heredoc

cat <<END
hello world
END

Source relative

source "${0%/*}/../share/foo.sh"

Pipes

cmd1 | cmd2                             stdout of cmd1 to cmd2
cmd1 |& cmd2                            stderr of cmd1 to cmd2

Returning values

myfunc() {
   local myresult='some value'
   echo $myresult
}
result="$(myfunc)"

User Info and Management

useradd -c "John Smith" -m john         Create an account named john, with a comment of "John Smith" and create the user's home directory.
usermod -aG sales john                  Add the john account to the sales group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment