Skip to content

Instantly share code, notes, and snippets.

@Fractalbonita
Last active April 11, 2024 13:28
Show Gist options
  • Save Fractalbonita/229065ea09fdf238f02014a352d07aa6 to your computer and use it in GitHub Desktop.
Save Fractalbonita/229065ea09fdf238f02014a352d07aa6 to your computer and use it in GitHub Desktop.

Unix

Introduction

Unix commands ...

  • ... are small programs (commands and operators).
  • ... are composable, repeatable, and fast.
  • ... have a single purpose.
  • ... are tested and proven.
  • ... enable interaction with operating system.
  • ... were developed during the 60s.
  • ... are commands of the unix operating system.

Standard streams

  • stdout: = standard output
  • stdin: = standard input
  • stderr: = standard error

Shell

  • Access and interaction with the operating system, which provides different services
  • Control remote computers
  • Automation of processes
  • GUI or CLI
  • Terminals are used to enter commands, e.g. iTerm, hyper, Git bash, wsl2
  • e.g. zsh, bash, powershell

Shell shortcuts

  • arrow up — move upwards in history
  • arrow down — move downwards in history
  • code . — open VS code (must be installed)
  • \[search term] + ENTER — search for [search term] inside the manual and jump to first match
  • N — search and jump down to next search match
  • shift + N — search and jump up to previous search match
  • J — move upwards
  • K — move downwards
  • H — move to the left
  • L — move to the right
  • tab — autocompletion

Mac

  • cd ~ — change to home directory
  • cmd + K — clear history
  • cmd + N — open new window
  • ctrl + C — stop running command
  • ctrl + R — search in history
  • ctrl + arrow up — move to the left by word separator
  • ctrl + arrow down — move to the right by word separator
  • z [project] — navigate into folder including [project] (tool must be installed)

Basic commands

Options

  • Additions to commands
  • Various options can be combined
  • e.g. mkdir OPTION [directory]

General options (flags)

  • -a — hidden files and directories
  • -b — branch flag
  • -h — help flag
  • -l — show list with all rights
  • -p — path / parent flag
  • -s — sort files
  • -v — verbose flag: shows the execution of the operation step by step
  • -f — follow file
  • -g — stands for globally
  • -r — recursive

Find options

  • -name — name of the file
  • -type — type of the file
    • d — directory
    • f — file
  • -empty — is the file?
  • -executable — is the file executable?
  • -writeable — is the file writable?

Orientation

  • cd [directory] — navigate into the existing specified directory aka change directory
  • ls — list files and directories in the current working directory aka list directory contents
  • ls -la — list all files and directories, also hidden ones, in the current working directory
  • ls -lah — list all files and directories as a table with header (human readable)
  • man [command] — show the manual page of the specified command aka command manual
  • help — shows all internally defined shell commands
  • help [command] — shows more information about the specified command
  • history — show all previous commands
  • pwd — print pathname of the working directory
  • ps — report a snapshot of the current processes
  • [program] -v – show version of program installed

Create

  • mkdir [directory]— make a new directory inside the current working directory
  • mkdir [parent_directory]/[directory] — add a new folder named [directory] to the existing directory [parent_directory]
  • mkdir -p [parent_directory] [directory] add a new subfolder [directory] to the parent folder [parent_directory]
  • touch [file_path] — create a new file with the specified pathname or update the change-timestamp of the file

Read

  • cat [file] — print out the current content of the specified file aka show file contents
  • cat [file1] > [file2] — save content of file [file1] to another file [file2]
  • cat [file1] >> [file2] — append content of [file1] to [file2]
  • cat hello.md | say — read out content of file "hello.md" via speakers
  • less [path] — open a pager program for a text file and show file contents by page
  • open [file_path] — open file with the path [file_path] with default app
  • head — show the top x lines of a file
  • tail — show the last x lines of a file

Update

  • mv [initial_file] [final_file] — rename file
  • mv [initial_directory] [final_directory] — rename directory
  • mv [file] [path] — move file, e.g. [path] as ./[directory]
  • mv [directory] [path] — move directory, e.g. [path] as ../
  • cp [file] [path] — copy or rename file
  • echo 'Hi' > [file] — create or replace file named [file] with text content "Hi" aka repeat input
  • echo 'Ciao' >> [file] — add text content "Ciao" to end of file [file] warnings

Delete

  • rmdir [directory] — remove the specified directory
  • rm [file_path] — remove specified file permanently
  • rm -i [file_path] — ask before removing the file
  • rm -rf [file_path] — remove file recursively and forced to suppress warnings

Find

  • find — search file names
  • find [directory] [options] "[file]/folder]" — find file or directory by name, e.g. find /foo -name bar.md
  • grep — search file contents
  • grep [options] [search expression] [directory] — search for expression inside the specified directory, e.g. grep -i "foo" /bar/buzz
  • grep -iRl [text] [path] — search for text in content of path, show resulting path
  • zgrep [file] — search inside gzip file

Destroy

  • pkill — kill process by name

Advanced commands

  • alias — create and show aliases
  • curl [url] — download and print online resource (curl stands for client url)
  • curl [url] | bash — download online resource and execute in shell
  • curl [url] > [file] — download online resource and save to the specified file
  • tree [path] — print file tree aka graphical presentation of directories and files (must be installed via brew install tree on Mac or Windows)
  • tree.com — print directory tree of the current directory (Git bash on Windows)
  • tree.com //f — print tree with subdirectories and files of the current directory
  • tree.com //f //a — print tree with subdirectories and files of the current directory as ascii folder structure
  • reboot — reboot
  • ssh — connect to online endpoint via secure shell

Glob patterns

  • . — current directory
  • .. — parent directory
  • ... — two directories up
  • name?.text — any single character
  • name??.text — any two characters
  • *.log — any number of characters (aka any log file)
  • ./**/index.html — any nested directory (aka any index.html file)
  • ./**/*.html — any number of characters in nested directory (aka any HTML file)
  • client_[123].txt — any of the given character (1, 2, or 3)
  • ./{index,home}.html — any of the given strings (index or home)

Operators

Control operators

  • [command] ; [command] — separate commands
  • [command] && [command] — logical and operator: second command only runs, if first is successful
  • [command] | [command] — pipeline: send result of first to second command (read from stdout)
  • [command] & [command] — run first command in background and second command in foreground (e.g. start server, server request, automatic reload if data has been changed)

Redirection operators

  • [command] > [file_path] — write stdout to file (send output of command (stdout) to file [file_path])
  • [command] >> [file_path] — append stdout to file (append output of command to the text of the file [file_path])
  • [command] < [file_path] — read from stdin (send command to file)
  • [command] 2> [file_path — read from stderr (send error of command (stderr) to file [file_path])

Bash aliases

  • Shortcuts for unix and Git commands

Syntax

alias name='command(s)'

Instructions

e.g. on Windows

  1. Create a .bash_profile file in the user directory (another common filename: .bashrc)
touch .bash_profile
# pathname: ~/.bash_profile
  1. Create an alias "lah"
echo "alias lah='ls -lah'" > .bash_profile
# Comments are optional
cat .bash_profile
  1. Add another alias "example"
echo "example='x y z'" >> .bash_profile
# Comments are optional
cat .bash_profile
  1. Restart the terminal to load aliases or just source the changed file
source ~/.bash_profile

Bash functions

  • Bash aliases with one or more arguments
  • Good to avoid repetition and improve readability
  • Easy syntax
  • Two different formats are available to declare functions

Syntax

Format 1

function function_name {
  commands
}

Format 2

function_name () {
  commands
}

Instructions

  1. Open ~/.bash_profile in a text editor
code ~/.bash_profile
# or
nano ~/.bash_profile
  1. Create new bash function and save file
function mkcd {
  mkdir -p -- '$1' && cd -P -- '$1'
}
  1. Reload the changed file
source ~/.bash_profile

See also:

Bash Functions How to Create Bash Aliases

SSH-Key

  • SSH keys are saved to the ~/.ssh folder
  • The ~/.ssh folder also contains the known_hosts file
  • There are several types of keys: rsa, dsa, ed25519, ecdsa ...
  • See the manual page for more details man ssh-keygen

Generate a SSH key of type ed

ssh-keygen -t ed25519 -C "[email protected]"

Generate a SSH key of type rsa

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Add SSH key to the ssh-agent

# Windows
# 1. STEP: Start the ssh-agent in the background
eval `ssh-agent -s`
# 2. STEP Add SSH private key to the ssh-agent
ssh-add ~/.ssh/id_ed25519
# 3. STEP Add SSH public key to clipboard
clip < ~/.ssh/id_ed25519.pub

# Mac
# 1. STEP: Start the ssh-agent in the background
eval "$(ssh-agent -s)"
# 2. STEP Add SSH private key to the ssh-agent (modify the config beforehand if necessary)
ssh-add -K ~/.ssh/id_ed25519
# 3. STEP Add SSH public key to clipboard
pbcopy < ~/.ssh/id_ed25519.pub

See also:

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