Update shell_cheatsheet.md
[swc-modular-shell-hearing.git] / shell_cheatsheet.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
14
15
16 ## 2. Creating Things:
17 ### a) How to create new files and directories..
18
19 | Command           | Definition                                                                                                                                                                                                                                                                                                                                            |  
20 |-------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
21 | `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                                                                                                                                                                                                    |  
22 | `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) |  
23
24 ### b) How to delete files and directories...
25 #### _Remember that deleting is forever. There is NO going back_
26
27 | Command           | Definition                                                                                                       |  
28 |-------------------|------------------------------------------------------------------------------------------------------------------|
29 | `rm ./filename`   | deletes a file called `filename` from the current directory                                                      |  
30 | `rmdir ./dirname` |  deletes the directory `dirname` from the current directory. _Note:_ `dirname` must be empty for `rmdir` to run. |  
31
32 ### c) How to copy and rename files and directories...
33
34 | Command | Definition                                                                                                                                                                                                                    |  
35 |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
36 | `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` |  
37 | `cp tmp/filename .` | copies the file `filename` from the directory `tmp` to the current directory. _Note:_ _(i)_ the original file is still there                                                                                      |  
38
39
40
41 ## 3. Pipes and Filters
42 ### a) How to use wildcards to match filenames...
43 Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. 
44 Wildcards are particularly useful when you are looking for directories, files, or file content that can
45 vary along a given dimension.  These wildcards can be used with any command that accepts file names or 
46 text strings as arguments.
47
48 #### Table of commonly used wildcards 
49
50 | Wildcard               | Matches                                        |  
51 |------------------------|------------------------------------------------|  
52 | `*`                    | zero or more characters                        |  
53 | `?`                    | exactly one character                          |  
54 | `[abcde]`              | exactly one of the characters listed           |  
55 | `[a-e]`                | exactly one character in the given range       |  
56 | `[!abcde]`             | any character not listed                       |  
57 | `[!a-e]`               | any character that is not in the given range   |  
58 | `{software,carpentry}` | exactly one entire word from the options given |  
59
60 See the cheatsheet on regular expressions for more "wildcard" shortcuts.
61
62 ### b) How to redirect to a file and get input from a file ...
63 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).
64
65
66 | Command | Description                                                                                                                     |  
67 |---------|---------------------------------------------------------------------------------------------------------------------------------|  
68 | `>`     | write `stdout` to a new file; overwrites any file with that name (e.g., `ls *.md > mardkownfiles.txt`)                          |  
69 | `>>`    | append `stdout` to a previously existing file; if the file does not exist, it is created (e.g., `ls *.md >> markdownfiles.txt`) |  
70 | `<`     | assigns the information in a file to a variable, loop, etc (e.g., `n < markdownfiles.md`)                                       | 
71
72
73
74 #### b.1) How to use the output of one command as the input to another with a pipe...
75 A special kind of redirection is called a pipe and is denoted by `|`. 
76
77
78 | Command | Description                                                                                                                                           |  
79 |---------|-------------------------------------------------------------------------------------------------------------------------------------------------------|  
80 | &#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) |  
81
82
83
84
85
86 ##### Example:   
87
88     ls *.md | head | sed -i `s/markdown/software/g`
89    
90 changes all the instances of the word `markdown` to `software` in the first 5 `*.md` files in your current directory.
91
92
93  
94
95 ## 4. How to repeat operations using a loop...
96 Loops assign a value in a list or counter to a variable that takes on a different value each time through the loop.
97 There are 2 primary kinds of loops: `for` loops and `while` loops.
98
99 ### a) For loop
100 For loops loop through variables in a list
101
102
103     for varname in list
104     do
105         command 1
106         command 2
107     done
108
109 where,
110
111 *  `for`, `in`, `do`, and `done` are keywords
112 *  `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:
113
114 --
115
116     list[0]=Sam
117     list[1]=Lynne
118     list[2]=Dhavide
119     list[3]=Trevor
120     .
121     .
122     .
123     list[n]=Mark
124     
125 which is referenced in the loop by:
126
127     for varname in ${list[@]}
128     do
129         command 1
130         command 2
131     done
132
133
134 _Note:_ Bash is zero indexed, so counting always starts at `0`, not `1`.
135     
136
137 ### b) While Loop
138 While loops loop through the commands until a condition is met. For example
139     
140     COUNTER=0
141     while [ ${COUNTER} -lt 10 ]; do
142         command 1
143         command 2
144         COUNTER=`expr ${COUNTER} + 1` 
145     done
146
147 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).
148
149 *  `while`, `do`, and `done` are keywords
150
151
152 #### b.1) Commonly used conditional operators
153
154 | Operator | Definition               |  
155 |----------|--------------------------|  
156 | `-eq`    | is equal to              |  
157 | `-ne`    | is not equal to          |  
158 | `-gt`    | greater than             |
159 | `-ge`    | greater than or equal to |
160 | `-lt`    | less than                |
161 | `-le`    | less than or equal to    |
162
163
164
165
166
167 ## 6. Finding Things
168 ### a) How to select lines matching patterns in text files...
169 To find information within files, you use a command called `grep`.
170
171 | Example command                | Description                                                                                    |
172 |--------------------------------|------------------------------------------------------------------------------------------------|
173 | `grep [options] day haiku.txt` | finds every instance of the string `day` in the file haiku.txt and pipes it to standard output |                                                                                                                                                                                                                |  
174
175 #### a.1) Commonly used `grep` options
176
177 |      | `grep` options                                                                                                                                                                                                       |  
178 |------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|  
179 | `-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 |  
180 | `-i` | makes matching case-insensitive                                                                                                                                                                                      |  
181 | `-n` | limits the number of lines that match to the first n matches                                                                                                                                                         |   
182 | `-v` | shows lines that do not match the pattern (inverts the match)                                                                                                                                                        |  
183 | `-w` | outputs instances where the pattern is a whole word                                                                                                                                                                  |  
184
185 ### b) How to find files with certain properties...
186 To find file and directory names, you use a command called `find`
187
188 | Example command  | Description |  
189 |------------------|-------------|
190 | `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 |  
191
192
193 #### b.1) Commonly used `find` options
194
195 |               | `find` options                                                                                                                                          |  
196 |---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|  
197 | `-type [df]`  | `d` lists directories; `f` lists files                                                                                                                  |  
198 | `-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 |
199 | `-mindepth n` | starts `find`'s search `n` levels below the working directory                                                                                           |
200
201