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.
- stdout: = standard output
- stdin: = standard input
- stderr: = standard error
- 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
arrow up— move upwards in historyarrow down— move downwards in historycode .— open VS code (must be installed)\[search term] + ENTER— search for [search term] inside the manual and jump to first matchN— search and jump down to next search matchshift + N— search and jump up to previous search matchJ— move upwardsK— move downwardsH— move to the leftL— move to the righttab— autocompletion
Mac
cd ~— change to home directorycmd + K— clear historycmd + N— open new windowctrl + C— stop running commandctrl + R— search in historyctrl + arrow up— move to the left by word separatorctrl + arrow down— move to the right by word separatorz [project]— navigate into folder including [project] (tool must be installed)
- Additions to commands
- Various options can be combined
- e.g.
mkdir OPTION [directory]
-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
-name— name of the file-type— type of the filed— directoryf— file
-empty— is the file?-executable— is the file executable?-writeable— is the file writable?
cd [directory]— navigate into the existing specified directory aka change directoryls— list files and directories in the current working directory aka list directory contentsls -la— list all files and directories, also hidden ones, in the current working directoryls -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 manualhelp— shows all internally defined shell commandshelp [command]— shows more information about the specified commandhistory— show all previous commandspwd— print pathname of the working directoryps— report a snapshot of the current processes[program] -v– show version of program installed
mkdir [directory]— make a new directory inside the current working directorymkdir [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
cat [file]— print out the current content of the specified file aka show file contentscat [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 speakersless [path]— open a pager program for a text file and show file contents by pageopen [file_path]— open file with the path [file_path] with default apphead— show the top x lines of a filetail— show the last x lines of a file
mv [initial_file] [final_file]— rename filemv [initial_directory] [final_directory]— rename directorymv [file] [path]— move file, e.g. [path] as ./[directory]mv [directory] [path]— move directory, e.g. [path] as ../cp [file] [path]— copy or rename fileecho 'Hi' > [file]— create or replace file named [file] with text content "Hi" aka repeat inputecho 'Ciao' >> [file]— add text content "Ciao" to end of file [file] warnings
rmdir [directory]— remove the specified directoryrm [file_path]— remove specified file permanentlyrm -i [file_path]— ask before removing the filerm -rf [file_path]— remove file recursively and forced to suppress warnings
find— search file namesfind [directory] [options] "[file]/folder]"— find file or directory by name, e.g.find /foo -name bar.mdgrep— search file contentsgrep [options] [search expression] [directory]— search for expression inside the specified directory, e.g.grep -i "foo" /bar/buzzgrep -iRl [text] [path]— search for text in content of path, show resulting pathzgrep [file]— search inside gzip file
pkill— kill process by name
alias— create and show aliasescurl [url]— download and print online resource (curl stands for client url)curl [url] | bash— download online resource and execute in shellcurl [url] > [file]— download online resource and save to the specified filetree [path]— print file tree aka graphical presentation of directories and files (must be installed viabrew install treeon 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 directorytree.com //f //a— print tree with subdirectories and files of the current directory as ascii folder structurereboot— rebootssh— connect to online endpoint via secure shell
.— current directory..— parent directory...— two directories upname?.text— any single charactername??.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)
[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)
[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])
- Shortcuts for unix and Git commands
alias name='command(s)'e.g. on Windows
- Create a
.bash_profilefile in the user directory (another common filename:.bashrc)
touch .bash_profile
# pathname: ~/.bash_profile- Create an alias "lah"
echo "alias lah='ls -lah'" > .bash_profile
# Comments are optional
cat .bash_profile- Add another alias "example"
echo "example='x y z'" >> .bash_profile
# Comments are optional
cat .bash_profile- Restart the terminal to load aliases or just source the changed file
source ~/.bash_profile- Bash aliases with one or more arguments
- Good to avoid repetition and improve readability
- Easy syntax
- Two different formats are available to declare functions
Format 1
function function_name {
commands
}Format 2
function_name () {
commands
}- Open
~/.bash_profilein a text editor
code ~/.bash_profile
# or
nano ~/.bash_profile- Create new bash function and save file
function mkcd {
mkdir -p -- '$1' && cd -P -- '$1'
}- Reload the changed file
source ~/.bash_profileSee also:
Bash Functions How to Create Bash Aliases
- SSH keys are saved to the
~/.sshfolder - The
~/.sshfolder also contains theknown_hostsfile - 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.pubSee also: