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