Graduate Program KB

Linux Notes

Flags

  • Can be used more than once in a command and concatonated together using the first letter of each flag (eg. -asdf)
  • Parameters can be added using --flag=parameter
  • ~ is the root directory
  • / is the home directory
  • tab key autocompletes file, folder or command name
    • Double press tab key to view directories inside the one you cd'd into

Bash History

  • history tail - displays the last 10 lines of your bash history.
  • history n - displays n number of lines in the history
  • /.bash_history - will find the hidden history file.
  • Can be deleted to remove the history
    • Useful if a password is stored
  • !! - run a previous command again

Basic VIM Commands

  • i - enter interactive or edit mode
  • :dn - delete the top n number of lines
  • :d - delete the selected line
  • :wq - save and quit
  • :q - to quit

Managing and Reading Files

  • cat file name - print the contents of a file
  • mkdir - make a new directory
  • touch file name - creates a file if it does not already exist
  • mv file name destination directory - move file to different directory
  • rm file name - removes a file
  • rm -r directory name - remove directory
  • cp source file destination file - copies the contents of one file to another
  • cp -R source directory destination directory - copies the contents of a directory to another

Superusers

  • sudo - runs a single command as a superuser
  • sudo useradd name - creates a new user
  • sudo passwd name - adds a password to specified user
  • su name - switch to that user
  • sudo usermod -aG group name user name - add specified user to group

Exit Codes

  • 0 = Success
  • 1 = Error
  • 2 = Bash Error
  • 126 = No Permissions
  • 127 = Command Not found
  • 130 = Ended with Ctrl + C (By the user)
  • 137 = Ended program with SIGKILL
  • 255 = Out of Bounds Error

SSH and Wget

  • SSH = Secure SHell
  • Allows you to remotely connect to and tun commands on a separate computer
  • wget link - downloads files from a specified server
  • chmod +x file name - turns a file into an executable

Curl

  • curl url - prints the contents of a webpage
  • curl url file name - prints the contents of a webpage to a file
  • Able to send information between servers using GET, POST etc.
  • Includes cookies, can be used to save cookie information to a file from the internet
  • Can be used to diagnose connection request issues

APT and Package Management

  • sudo apt install program - installs specified packages/programs
  • sudo apt autoremove - removes unneeded packages
  • sudo apt update - updates the list of packages and programs that can be updated
  • sudo apt upgrade - updates the packages in the update list
  • sudo apt full-upgrade - runs autoremove and upgrade at the same time

Writing Scripts

  • Scripts can be written in terminal with nano or vim editors
  • Scripts can be executed with source filename bash filename OR . filename
  • Hashbang (#! language file path) at the top of the file tells bash how to interpret the code

Code and Syntax

  • env variables should be constants $CONSTANT_VARIABLE
  • if statements can be written using:
if [1 -gt 10]; then
    echo "greater than ten"
elif [1 -lt 10]; then
    echo "less than 10"
else
    echo "equal to 10"
fi
  • else/else if statements can be written using:
if [1 -gt 10]; then
    echo "greater than ten"
elif [1 -lt 10]; then
    echo "less than 10"
else
    echo "equal to 10"
fi
  • case (switch) statements can be written in terminal using:
case $1 in
    "smile")
        echo ":)"
        ;;
    "sad")
        echo ":("
        ;;
    "laugh")
        echo ":D"
        ;;
    *)
        echo "wildcard, input doesn't match the switch cases"
        ;;
esac
  • arrays can be written using:
friends=(Kyle Mark Jem "K Bowers" Sarah)
  • arrays can be accessed using:
${friends[i]}
  • for loops can be written using:
for friend in ${friends[*]}
do
    echo friend: $friend
done
  • get array length with:
${#friends[*]}