Update shell_cheatsheet.md
[swc-modular-shell.git] / shell_cheatsheet.md
1 # Basic Shell Commands
2 ***
3
4 ## 1. Shell Basics:
5
6 | Symbol         | 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 * **`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
19 * **`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)
20
21 ### b) How to delete files and directories...
22 #### _Remember that deleting is forever. There is NO going back_
23 * **`rm ./filename`** --> deletes a file called `filename` from the current directory 
24 * **`rmdir ./dirname`** --> deletes the directory `dirname` from the current directory. _Note:_ `dirname` must be empty for `rmdir` to run.
25
26 ### c) How to copy and rename files and directories...
27 * **`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`
28 * **`cp tmp/filename .`** --> copies the file `filename` from the directory `tmp` to the current directory. _Note:_ _(i)_ the original file is still there
29
30
31
32 ## 3. Pipes and Filters
33 ### a) How to use wildcards to match filenames...
34 Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. 
35
36
37 ** Table of commonly used wildcards **
38
39 | Wildcard               | Matches                                        |  
40 |------------------------|------------------------------------------------|  
41 | `*`                    | zero or more characters                        |  
42 | `?`                    | exactly one character                          |  
43 | `[abcde]`              | exactly one of the characters listed           |  
44 | `[a-e]`                | exactly one character in the given range       |  
45 | `[!abcde]`             | any character not listed                       |  
46 | `[!a-e]`               | any character that is not in the given range   |  
47 | `{software,carpentry}` | exactly one entire word from the options given |  
48
49
50
51 ### b) That wildcards are expanded by the shell before commands are run...
52 ### c) How to redirect a command's output to a file...
53 ### d) How to redirect a command's input from a file...
54 ### e) How to use the output of one command as the input to another with a pipe...
55 ### f) That combining single-purpose filters with pipes is the most productive way to use the shell...
56 ### g) That if a program conforms to Unix conventions, it can easily be combined with others...
57
58
59
60 ## 4. Variables
61 ### a) Assignment
62 * **`varname=1`** -->
63
64 ### b) Indexing 
65 * **`varname[0]`** --> _Note:_ the shell is zero indexed.  That means you always start counting from zero
66
67 ### c) Referencing
68 * **`${varname}` -->
69 * **`${varname[@]` --> 
70
71  
72
73 ## 5. Loops
74 NEED TO DO VARIABLE ASSIGNMENT FIRST!!!!
75 ### a) How to repeat operations using a loop...
76 * **`for`** -->  
77     `for filename in *.dat
78     do
79       mv ${filename} ${newname}
80     done`
81     
82 * **`while`** -->
83     `count=0   
84      while ${count} -lte 6
85      do
86        COMMAND HERE
87      done`
88
89 ### b) That the loop variable takes on a different value each time through the loop...
90 ### c) The difference between a variable's name and its value...
91 ### d) Why spaces and some punctuation characters shouldn't be used in files' names...
92 ### e) How to display history and re-use commands...
93 * **`history`** --> displays your command history to the standard output (usually the screen)
94
95
96
97 ## 6. Shell Scripts
98 ### a) How to store shell commands in a file...
99 ### b) How to run a shell script...
100 ### c) How to pass filenames into a shell script...
101
102
103
104 ## 7. Finding Things
105 ### a) How to select lines matching patterns in text files...
106 * **`grep [options] day haiku.txt`** --> finds every instance of the string `day` in the file haiku.txt and pipes it to standard output. 
107         * **`-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
108         * **`-i`** --> makes matching case-insensitive
109         * **`-n`** --> limits the number of lines that match to the first n matches
110         * **`-v`** --> shows lines that do not match the pattern (inverts the match)                    
111         * **`-w`** --> outputs instances where the pattern is a whole word
112
113 ### b) How to find files with certain properties...
114 * **`find . -type d` -->
115         * **`-type [df]`** --> d lists directories; f lists files
116         * **`-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
117         * **`-mindepth n`** --> starts `find`'s search n levels below the working directory
118         
119 ### c) How to use one command's output as arguments to another command...
120
121 ### d) How are text and binary files different?...
122