Skip to content

Instantly share code, notes, and snippets.

@janstuemmel
Last active February 18, 2021 17:27
Show Gist options
  • Save janstuemmel/6e13ed59de61ba3195669964b646fcb0 to your computer and use it in GitHub Desktop.
Save janstuemmel/6e13ed59de61ba3195669964b646fcb0 to your computer and use it in GitHub Desktop.
Shell Cheatsheet

Shell Cheatsheet

a set of unix shell commands

Contents

  • Scripting Shell scripting
  • Basic Basic shell commands
    • du/df Show filesize / diskspace
    • tree Show a directory tree
    • find Find files
    • sed Replace strings in files
  • HTTP Commands for working with HTTP
    • curl Get/Post data from/to the internet
    • wget Does the same as curl with a different interface
    • wput Upload files with wget like interface
  • Git Git commands
  • Misc Other things

Scripting

Loops

for i in $(ls); do echo $i; done

Conditional

If/Else

Check exists

Basic

Filesize and Diskspace

df -h                               # shows total disk space
du <file|dir> -h                    # show file/directory size

du . --apparent-size                # real size on filesystem (rather then disk usage)
du . -h                             # human readable
du . -hc                            # show total
du . -hc --exclude="/mnt"           # exclude dir
du . -hcx                           # x excludes other drives (via symlink, mount, ...)
du . -hc --max-depth=1              # tree depth
du . -hc -d=1 | sort -hr            # sort result (-d shortcut for max-depth)

Tree

install tree

tree . -L 2                           # level
tree . -L 2 -a                        # print dotfiles
tree . -L 2 -a -I ".git|node_modules" # ignore files pattern
tree . -L 2 -a -P "*.js|*.json"       # only files with pattern

tree . -L 2 --du -hs                  # humanreadable file sizes
tree . -L 2 --dirsfirst               # directories first

Find

find . -type f  # lists files
find . -type d  # lists directories

# finds all jpg, but not png
find images/ -type f -name *.jpg -not -name *.png

# finds *.jpg and deletes each file
find images -type f -name *.jpg -delete

# finds *.jpg and executes a command in cwd, {} is the placeholder for the filename
find images/ -type f -name *.jpg -exec echo Hello_{} \;

# excutes command in the directory
find images/ -type f -name *.jpg -execdir zip archive_{}.zip {} \;

Sed

# replaces HOST with localhost in specified file
sed -i s/HOST/localhost/g <file>

HTTP

Curl

Wget

Wput

install wput

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment