├── README.md └── terminal.png /README.md: -------------------------------------------------------------------------------- 1 | # Terminal-Cheatsheet 2 | My personal cheatsheet for the Mac OS Terminal 3 | 4 | ![Cheat: Terminal Cheatsheet](https://raw.githubusercontent.com/SebastianBoldt/Cheat/master/terminal.png) 5 | 6 | ## The Shell 7 | In order to use commands you'll need a program that reads and executes them. That program 8 | is called shell, which runs inside the Terminal. Some commands are builtin and some commands are external programs. You can check that by using the **type** command. 9 | 10 | | Command | Output | 11 | | ----------- | ----------- | 12 | | type ls | ls is an alias for ls -G | 13 | | type pod | pod is Users/username/.rvm/gems/ruby-2.0.0-p247/bin/pod | 14 | 15 | ### Wildcards 16 | | Command | Description | 17 | | ----------- | ----------- | 18 | | ls a\* | List all Files that begin with lowercase a | 19 | | ls \*20 | List files that end with 20 | 20 | 21 | ### Shell variables 22 | You can define a shell variables and their values by assigning them: 23 | ``` 24 | VAR=3 25 | ``` 26 | Print it out using a leading $ 27 | 28 | ``` 29 | echo $VAR 30 | ``` 31 | 32 | The Shell comes with some default Variables. 33 | 34 | | Variables | Description | 35 | | ----------- | ----------- | 36 | | DISPLAY | The name of your display for opening X Windows | 37 | | HOME | Your home directory | 38 | | LOGNAME | Your login name | 39 | | MAIL | Your incoming mailbox | 40 | | OLDPWD | You shells's previous directory, prior to the last cd command | 41 | | PATH | Your shell search path: directories separate by colons | 42 | | PWD | Your shells current directory | 43 | | SHELL | The path to your shell | 44 | | TERM | The type of your terminal | 45 | | USER | Your login name | 46 | 47 | Use **printenv** to print all of them. 48 | If you want to create an environment variable that is available to all programms started from that shell you need to use 49 | ``` 50 | export VAR=3 51 | ``` 52 | 53 | or 54 | 55 | ``` 56 | export VAR 57 | ``` 58 | 59 | if you previously defined VAR. 60 | 61 | ### Search path 62 | Programs are scattered all over the filesystem, in directories like /bin and /usr/bin. The variable **PATH** tells the shell where to look.**PATH** is a sequence of directories separated by colons 63 | 64 | Modify it using: 65 | **PATH=$PATH:/your/directory** 66 | 67 | if you want to make it permanent you need to put that command into your **.bash_profile** or **.zshrc** 68 | 69 | ### Aliases 70 | ``` 71 | alias ll='ls -l' 72 | ``` 73 | 74 | ``` 75 | unalias ll 76 | ``` 77 | 78 | ### Input & Output redirection 79 | 80 | The shell can redirect standard in, standard out and standard error to and from files. 81 | Any command that reads from standard input can read from a file instead with the shells **<**, **>** and **2>** operator 82 | 83 | | Command | Description | 84 | | ----------- | ----------- | 85 | | command < infile | command should read from file | 86 | | command > outfile | command should write to a file | 87 | | command >> outfile | command should append its output to file | 88 | | command 2> errorfile | write standard error to file & stream | 89 | | command > outfile 2> errorFile | write to outfile and things the command writes to stderror will be redirected to errorFile | 90 | 91 | ### Pipes 92 | Redirection of standard input of one command can be redirected to another command using the **|** operator. 93 | 94 | | Command | Description | 95 | | ----------- | ----------- | 96 | | ls \| wc -l | redirect ls output to word count command | 97 | | ls -1 \| cut -d. -f2 \| sort | Show all file types | 98 | 99 | ### Combining Commands 100 | | Command | Description | 101 | | ----------- | ----------- | 102 | | command1 ; command2 | If anyone fails, the sequence continues | 103 | | command1 && command2 | The sequence will stop if any command fails | 104 | | command1 \|\| command2 | The sequence will stop if one command succeeded | 105 | 106 | ### Quoting & Escaping 107 | If you want a word contain whitspaced you need to surround it with single or double quotes to make the shell treat it as a unit. 108 | Double quotes will result in evaluating the actual content and replacing shell variables with there actual content. 109 | 110 | | Command | Description | 111 | | ----------- | ----------- | 112 | | wc 'Value of PATH: $PATH' | Will print "Value of PATH: $PATH" | 113 | | wc "Value of PATH: $PATH" | Will print "Value of PATH: /users/sebastian/.." | 114 | 115 | If a character has a special meaning to the shell but you want it used literally, precede the character with a backslash \ 116 | 117 | | Command | Description | 118 | | ----------- | ----------- | 119 | | echo a\\* | Will print **a***, so * will not be interpreted as the wildcard symbol | 120 | 121 | ### Command History 122 | The shell allows you to recall previous commands. All commands you are entered are stored inside the command history. Here are some handy command you can use for it 123 | 124 | | Command | Description | 125 | | ----------- | ----------- | 126 | | history | print your history | 127 | | history N | print most recent n commands | 128 | | history -c| clear your history | 129 | | !! | re run previous command | 130 | | !N | rerun command N in history | 131 | | !-N | rerun command you typed n times ago | 132 | | !$| last parameter from previous command | 133 | | !* | all parameters from last command | 134 | | up arrow | go to previous command | 135 | | down arrow | go to next command | 136 | 137 | ### Jobs 138 | 139 | All shells have job control: the ability to run programs in the background (multitasking) und foreground (running as the active process at your shell). Jobs are higher level than processes. 140 | 141 | | Command | Description | 142 | | ----------- | ----------- | 143 | | jobs | List your Jobs | 144 | | emacs myfile & | put emacs to the background | 145 | | ^Z | suspend the current forground job | 146 | | bg [%jobnumber] | send suspended job to the background | 147 | | fg [%jobnumber] | bring it into the foreground | 148 | | suspend | make supsended job run in the background | 149 | 150 | ## Filesystem 151 | | Command | Description | 152 | | ----------- | ----------- | 153 | | cd | change to your home directory | 154 | | cd /dir | change to **dir** | 155 | | cd ../dir | change to parents sub directory **dir** | 156 | | cd ./dir | change to current sub directory **dir** | 157 | | pwd | print name of current working directory | 158 | | echo ~ | print path of home directory | 159 | 160 | ## File Protection 161 | 162 | Example: 163 | ``` 164 | -rw-r--r-- 165 | ``` 166 | 167 | | Postion | Meaning | 168 | | ----------- | ----------- | 169 | | 1 | File type. -(plain file), d(Directory), l(symbolic link), p(named Pipe), c(character device) b(block device) | 170 | | 2-4 | Owner permissions, read, write, and execute permissions for the files's owner | 171 | | 5-7 | Group permissions, read, write, and execute permissions for the files's group | 172 | | 8-10 | World permissions, read, write, and execute permissions for all other users | 173 | 174 | ## Commands 175 | 176 | .... 177 | -------------------------------------------------------------------------------- /terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SebastianBoldt/Terminal-Cheatsheet/7184f891824d718999ba96dd801dcd56247b8375/terminal.png --------------------------------------------------------------------------------