exercises/hearing: Replace 2012-04-uchichicago commit with 2012-02-trieste
[swc-modular-shell.git] / cheat-sheet.md
1 # Basic Shell Commands
2 ***
3
4 ## 1. Shell Basics:
5
6 | Command        | Definition                                                                                                     |
7 |----------------|----------------------------------------------------------------------------------------------------------------|  
8 | `.`            | a single period refers to the current directory                                                                |  
9 | `..`           | a double period refers to the directory immediately above the current directory                                |  
10 | `~`            | refers to your home directory. _Note:_ this command does NOT work on Windows machines (Mac and Linux are okay) |  
11 | `cd ./dirname` | changes the current directory to the directory `dirname`                                                       |  
12 | `ls -F`        | tells you what files and directories are in the current directory                                              |  
13 |  `pwd`         | tells you what directory you are in (`pwd` stands for *p*rint *w*orking *d*irectory)                           |  
14 |  `history`     | lists previous commands you have entered. `history | less` lets you page through the list.                     |  
15 |  `man` *cmd*   | displays the *man*ual page for a command.                                                                      |  
16
17
18
19 ## 2. Creating Things:
20 ### a) How to create new files and directories..
21
22 | Command           | Definition                                                                                                                                                                                                                                                                                                                                            |  
23 |-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
24 | `mkdir ./dirname` | makes a new directory called dirname below the current directory. _Note:_ Windows users will need to use `\` instead of `/` for the path separator                                                                                                                                                                                                    |  
25 | `nano filename`   | if `filename` does not exist, `nano` creates it and opens the `nano` text editor. If the file exists, `nano` opens it. _Note:_ _(i)_ You can use a different text editor if you like.  In gnome Linux, `gedit` works really well too. _(ii)_ `nano` (or `gedit`) create text files. It doesn't matter what the file extension is (or if there is one) |  
26
27 ### b) How to delete files and directories...
28 #### _Remember that deleting is forever. There is NO going back_
29
30 | Command           | Definition                                                                                                       |  
31 |-------------------|------------------------------------------------------------------------------------------------------------------|
32 | `rm ./filename`   | deletes a file called `filename` from the current directory                                                      |  
33 | `rmdir ./dirname` |  deletes the directory `dirname` from the current directory. _Note:_ `dirname` must be empty for `rmdir` to run. |  
34
35 ### c) How to copy and rename files and directories...
36
37 | Command | Definition                                                                                                                                                                                                                    |  
38 |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
39 | `mv tmp/filename .` | moves the file `filename` from the directory `tmp` to the current directory. _Note:_ _(i)_ the original `filename` in `tmp` is deleted. _(ii)_ `mv` can also be used to rename files (e.g., `mv filename newname` |  
40 | `cp tmp/filename .` | copies the file `filename` from the directory `tmp` to the current directory. _Note:_ _(i)_ the original file is still there                                                                                      |  
41
42
43
44 ## 3. Pipes and Filters
45 ### a) How to use wildcards to match filenames...
46 Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. 
47 Wildcards are particularly useful when you are looking for directories, files, or file content that can
48 vary along a given dimension.  These wildcards can be used with any command that accepts file names or 
49 text strings as arguments.
50
51 #### Table of commonly used wildcards 
52
53 | Wildcard               | Matches                                        |  
54 |------------------------|------------------------------------------------|  
55 | `*`                    | zero or more characters                        |  
56 | `?`                    | exactly one character                          |  
57 | `[abcde]`              | exactly one of the characters listed           |  
58 | `[a-e]`                | exactly one character in the given range       |  
59 | `[!abcde]`             | any character not listed                       |  
60 | `[!a-e]`               | any character that is not in the given range   |  
61 | `{software,carpentry}` | exactly one entire word from the options given |  
62
63 See the cheatsheet on regular expressions for more "wildcard" shortcuts.
64
65 ### b) How to redirect to a file and get input from a file ...
66 Redirection operators can be used to redirect the ouput from a program from the display screen to a file where it is saved (or many other places too, like your printer or to another program where it can be used as input).
67
68
69 | Command | Description                                                                                                                     |  
70 |---------|---------------------------------------------------------------------------------------------------------------------------------|  
71 | `>`     | write `stdout` to a new file; overwrites any file with that name (e.g., `ls *.md > mardkownfiles.txt`)                          |  
72 | `>>`    | append `stdout` to a previously existing file; if the file does not exist, it is created (e.g., `ls *.md >> markdownfiles.txt`) |  
73 | `<`     | assigns the information in a file to a variable, loop, etc (e.g., `n < markdownfiles.md`)                                       | 
74
75
76
77 #### b.1) How to use the output of one command as the input to another with a pipe...
78 A special kind of redirection is called a pipe and is denoted by `|`. 
79
80
81 | Command | Description                                                                                                                                           |  
82 |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------|  
83 | &#124;     | Output from one command line program can be used as input to another one (e.g. ls \*.md &#124; head gives you the first 5 `*.md` files in your directory) |  
84
85
86
87
88
89 ##### Example:   
90
91     ls *.md | head | sed -i `s/markdown/software/g`
92    
93 changes all the instances of the word `markdown` to `software` in the first 5 `*.md` files in your current directory.
94
95
96  
97
98 ## 4. How to repeat operations using a loop...
99 Loops assign a value in a list or counter to a variable that takes on a different value each time through the loop.
100 There are 2 primary kinds of loops: `for` loops and `while` loops.
101
102 ### a) For loop
103 For loops loop through variables in a list
104
105
106     for varname in list
107     do
108         command 1
109         command 2
110     done
111
112 where,
113
114 *  `for`, `in`, `do`, and `done` are keywords
115 *  `list` contains a list of values separated by spaces. e.g. `list` can be replaced by `1 2 3 4 5 6` or by `Bob Mary Sue Greg`. `list` can also be a variable:
116
117 --
118
119     list[0]=Sam
120     list[1]=Lynne
121     list[2]=Dhavide
122     list[3]=Trevor
123     .
124     .
125     .
126     list[n]=Mark
127     
128 which is referenced in the loop by:
129
130     for varname in ${list[@]}
131     do
132         command 1
133         command 2
134     done
135
136
137 _Note:_ Bash is zero indexed, so counting always starts at `0`, not `1`.
138     
139
140 ### b) While Loop
141 While loops loop through the commands until a condition is met. For example
142     
143     COUNTER=0
144     while [ ${COUNTER} -lt 10 ]; do
145         command 1
146         command 2
147         COUNTER=`expr ${COUNTER} + 1` 
148     done
149
150 continues the loop as long as the value in the variable COUNTER is less than 10 (incremented by 1 on each iteration of the loop).
151
152 *  `while`, `do`, and `done` are keywords
153
154
155 #### b.1) Commonly used conditional operators
156
157 | Operator | Definition               |  
158 |----------|--------------------------|  
159 | `-eq`    | is equal to              |  
160 | `-ne`    | is not equal to          |  
161 | `-gt`    | greater than             |
162 | `-ge`    | greater than or equal to |
163 | `-lt`    | less than                |
164 | `-le`    | less than or equal to    |
165
166 Use `man bash` or `man test` to learn about other operators you can use.
167
168
169
170 ## 6. Finding Things
171 ### a) How to select lines matching patterns in text files...
172 To find information within files, you use a command called `grep`.
173
174 | Example command                | Description                                                                                    |
175 |--------------------------------|------------------------------------------------------------------------------------------------|
176 | `grep [options] day haiku.txt` | finds every instance of the string `day` in the file haiku.txt and pipes it to standard output |                                                                                                                                                                                                                |  
177
178 #### a.1) Commonly used `grep` options
179
180 |      | `grep` options                                                                                                                                                                                                       |  
181 |------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
182 | `-E` | tells grep you will be using a regular expression. Enclose the regular expression in quotes. _Note:_ the power of `grep` comes from using regular expressions. Please see the regular expressions sheet for examples |  
183 | `-i` | makes matching case-insensitive                                                                                                                                                                                      |  
184 | `-n` | limits the number of lines that match to the first n matches                                                                                                                                                         |   
185 | `-v` | shows lines that do not match the pattern (inverts the match)                                                                                                                                                        |  
186 | `-w` | outputs instances where the pattern is a whole word                                                                                                                                                                  |  
187
188 ### b) How to find files with certain properties...
189 To find file and directory names, you use a command called `find`
190
191 | Example command  | Description |  
192 |------------------|-------------|
193 | `find . -type d` | `find` recursively descends the directory tree for each path listed to match the expression given in the command line with file or directory names in the search path |  
194
195
196 #### b.1) Commonly used `find` options
197
198 |               | `find` options                                                                                                                                          |  
199 |---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|  
200 | `-type [df]`  | `d` lists directories; `f` lists files                                                                                                                  |  
201 | `-maxdepth n` | `find` automatically searches subdirectories. If you don't want that, specify the number of levels below the working directory you would like to search |
202 | `-mindepth n` | starts `find`'s search `n` levels below the working directory                                                                                           |
203
204