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 ### c) How to redirect a command's output to a file...
63 ### d) How to redirect a command's input from a file...
64 ### e) How to use the output of one command as the input to another with a pipe...
65 ### f) That combining single-purpose filters with pipes is the most productive way to use the shell...
66 ### g) That if a program conforms to Unix conventions, it can easily be combined with others...
67
68
69
70 ## 4. Variables
71 ### a) Assignment
72 * **`varname=1`** -->
73
74 ### b) Indexing 
75 * **`varname[0]`** --> _Note:_ the shell is zero indexed.  That means you always start counting from zero
76
77 ### c) Referencing
78 * **`${varname}` -->
79 * **`${varname[@]` --> 
80
81  
82
83 ## 5. Loops
84 NEED TO DO VARIABLE ASSIGNMENT FIRST!!!!
85 ### a) How to repeat operations using a loop...
86 * **`for`** -->  
87     `for filename in *.dat
88     do
89       mv ${filename} ${newname}
90     done`
91     
92 * **`while`** -->
93     `count=0   
94      while ${count} -lte 6
95      do
96        COMMAND HERE
97      done`
98
99 ### b) That the loop variable takes on a different value each time through the loop...
100 ### c) The difference between a variable's name and its value...
101 ### d) Why spaces and some punctuation characters shouldn't be used in files' names...
102 ### e) How to display history and re-use commands...
103 * **`history`** --> displays your command history to the standard output (usually the screen)
104
105
106
107 ## 6. Shell Scripts
108 ### a) How to store shell commands in a file...
109 ### b) How to run a shell script...
110 ### c) How to pass filenames into a shell script...
111
112
113
114 ## 7. Finding Things
115 ### a) How to select lines matching patterns in text files...
116 * **`grep [options] day haiku.txt`** --> finds every instance of the string `day` in the file haiku.txt and pipes it to standard output. 
117         * **`-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
118         * **`-i`** --> makes matching case-insensitive
119         * **`-n`** --> limits the number of lines that match to the first n matches
120         * **`-v`** --> shows lines that do not match the pattern (inverts the match)                    
121         * **`-w`** --> outputs instances where the pattern is a whole word
122
123 ### b) How to find files with certain properties...
124 * **`find . -type d` -->
125         * **`-type [df]`** --> d lists directories; f lists files
126         * **`-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
127         * **`-mindepth n`** --> starts `find`'s search n levels below the working directory
128         
129 ### c) How to use one command's output as arguments to another command...
130
131 ### d) How are text and binary files different?...
132