Bash cheat sheet

Bash cheat sheet

Navigate the Filesystem

  • pwd ~ view current directory

  • cd ~ move into a directory

    • .. ~ move up a directory
  • ls ~ list what is in a directory

    • -a ~ hidden files and folders

    • -l ~ long listing, see more information

View Files and Folders

  • cat ~ see content of a file

    • -n ~ see content with line numbers
  • less ~ see content of a file in editor

  • open ~ open files in the directory

Create and Delete Files and Folders

  • touch ~ create files

  • mkdir ~ create folders

    • -p ~ create nested folders a/b/c
  • echo ~ log string or write to file

    • > ~ write to file

    • >> ~ append at the end of file

  • rm ~ remove file or folder

    • -r ~ remove folder recursively

Move and Copy Files and Folders

  • mv ~ used to move and rename files and folders

    • mv source/* target/ ~ move everything inside source to target folder
  • cp ~ copy files and folders

    • cp -R source/* target/ ~ copy everything recursively from source to target folder

Find Files and Folders

  • find ~ find files and folders

    • find images/ -name "*.png" ~ find files that end with png .

      • -iname ~ find files, but case insensitive
    • find . -type d -name "images" ~ find all folders in current directory with name images

    • -delete ~ flag to delete what was found

Search for Text

  • grep ~ search for text

    • grep "text" folder/file.js ~ search for text in whatever file

    • --color ~ search but colorize what we're looking for

    • -n ~ include the line numbers

    • -e ~ search but use regex

    • man grep ~ see all flags available

Make HTTP Requests

  • curl ~ make requests, by default GET

    • -i ~ inspect headers

    • -iL ~ inspect headers and follow redirect to see response

    • curl -iL "..."

    • -H ~ pass headers

    • curl -H "Authorization: ..."

    • -X ~ change the request method

    • curl -X POST -H "Content-Type: ..." -d '{ title: "Curling" }'

    • \ ~ to do things on new lines nicely formatted

    • curl -i -X PUT \

Create and Run Scripts

  • .sh ~ file has to end with this

  • chmod x+u ./file.sh ~ make file executable

    • The command stands for change mode, giving the user the executable permissions
  • $ ~ Accept arguments in your script. They will be passed in chronological order when running the script: ./file.sh argument1 argument2

  • $(<command>) ~ run commands inside the bash script

  • # ~ write comments in bash

Store and Use Values

  • var=value ~ set a variable's value for a session

  • echo $var ~ print the variable

  • unset var ~ unset the variable

  • export var ~ make the variable available to all child processes

Understand and Use Functions

Create and call a function in bash:

hi() {
    echo "Hello world"
}

hi

Function with an argument passed:

hi() {
    echo "$1 world"
}

hi "Hello"

Assign function's return statement to a variable:

hi() {
    echo "$1 world"
}

hiResult=$(hi "Hello")

echo "the result from hi is $hiResult"
  • local ~ create local variables only available within the function

Understand Exit Statuses

  • echo $? ~ see the exit status

  • 0 for ok

  • 1 for error

Use Conditional Statements

# Some conditional primaries that can be used in the if expression:
#   =, !=      string (in)equality
#   -eq, -ne   numeric (in)equality
#   -lt, -gt   less/greater than
#   -z         check variable is not set
#   -e         check file/folder exists
#    elif      else if statement

if [[ $USER = 'naruto' ]]; then
  echo "true"
else
  echo "false"
fi
# Inline format, similar to ternaries

[[ $USER = 'naruto' ]] && echo "yes" || echo "no"

Create Aliases in .bash_profile

  • On Ubuntu, if you don't create the .bash_profile file, it will use the existing configuration file ~/.profile .

  • gitm="git add . && git commit -m 'commit'" ~ example of an alias

Create and Copy multiple files with Brace Expansions

  • Items in the list of brackets.

  • touch index.js{,.backup} ~ create two files, index.js and index.js.backup , first item in the list is empty

  • mkdir -p packages/{pkg1,pkg2,pkg3}/src ~ create multiple packages inside packages folder, each containing src folder

  • echo {1..10} , echo {a..z} ~ print out ranges

  • You can use it for other things like creating multiple files

History expansion

  • !! ~ run the previous command

Default arguments

  • dir=${1:-$PWD} ~ users would pass the directory as the first argument, but it defaults to the current directory

Bash Keyboard Shortcuts

  • Ctrl+A - go to the beginning of a line

  • Ctrl+E- go to the end of a line

  • Ctrl+K - clears line up to the cursor

  • Ctrl+W - delete last word

  • Ctrl+L - clear the screen (equivalent of the clear command)

Complicated Conditional Statements

Case statements, they can also be on multiple lines:

case "$1" in
  firstValue) echo "firstValue matched";;
  secondValue) echo "secondValue matched";;
  thirdValue) 
    echo "thirdValue matched"
  ;;
esac