Quellcode durchsuchen

Update README.md

Git-Leon vor 7 Jahren
Ursprung
Commit
aa6b3090c0
1 geänderte Dateien mit 192 neuen und 1 gelöschten Zeilen
  1. 192
    1
      README.md

+ 192
- 1
README.md Datei anzeigen

@@ -1 +1,192 @@
1
-# CR-MesoLabs-Console
1
+# Console Lab Level 1
2
+
3
+## Objectives
4
+
5
+1. In this lab students will learn to navigate their file system using only the terminal
6
+2. Students will practice using the following commands to traverse directories and investigate their contents: `pwd`, `ls`, `cd`, `cat`, `less`, `grep`
7
+
8
+
9
+## Overview
10
+
11
+The console (also referred to as the command line, terminal, or shell) is an essential tool for programmers to be familiar with. The command line shell we will be using is called Bash. This lab will focus on the basics for navigating and investigating the file system through the command line. In this lab we will only be using the command line in interactive mode (more on that in the Bash labs). Here are the commands we will focus on:
12
+
13
+- `pwd` - "Print Working Directory" prints out your current location (known as your current working directory or 'cwd')
14
+- `ls` - lists contents of the cwd
15
+- `cd` - change directory. This is how you move around the file system. You can specify the destination as an absolute or relative path.
16
+- `echo` - prints text to the terminal
17
+- `cat` - concatenates zero or more files. Often used to print the contents of a file to the terminal. [Often misused](https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat)
18
+- `less`/`more` - Display contents of a file one page at a time ...just remember that less is more ([more or less](http://unix.stackexchange.com/questions/81129/what-are-the-differences-between-most-more-and-less)) and you'll be fine.
19
+- `grep` - search for the specified text or pattern
20
+
21
+### Other commands worth knowing
22
+
23
+- `head` - display the first lines of a file
24
+- `tail` - display the last lines of a file
25
+
26
+
27
+
28
+## Unit Test
29
+
30
+This lab is an instructional introduction to terminal. There are no unit tests.
31
+
32
+## Pre-lab && Important Notes
33
+
34
+It's a good practice to have a directory called `Dev` inside your home directory for all development work. You can create one in Finder (right click -> New Folder) or in the terminal (`mkdir ~/Dev`). This directory will come in handy in case things go horribly wrong, or if you want to back up all of your work. You should always make sure you are working in Dev or a directory inside of it during this course.
35
+
36
+## Instructions
37
+
38
+- Open up a terminal (Applications->Utilities->Terminal)
39
+- Find out what directory you're in using `pwd`
40
+- You know where you are, but not what's around you. Take a look around with `ls`
41
+- You probably started in your home directory, but just in case, tell the terminal to move to your home directory with `cd ~`. Use `pwd` again to confirm.
42
+- Change into your Dev directory with `cd Dev`. If you don't have one you should create one (see above)
43
+-  Now let's try printing the contents of a file. First we need to create the file. One quick way to do that is with the command `echo "Hello Terminal" >myFile.txt`. We'll explain this command in greater detail in the next lab.
44
+-  Use `ls` to list the contents of your directory after running that command to see what changed.
45
+-  Now show the contents of that new file using the `cat` command.
46
+-  You can use `cat` to print the contents of multiple files; Create another file the same way you created myFile.txt and then print them both together (eg: `cat myFile.txt myFile2.txt`)
47
+-  Using `cat` is fine for small files, or when you just want to print the entire contents of a file, but if you want to read through a large file it's not great. Try using cat on the file `/etc/bashrc_Apple_Terminal`
48
+-  That file's a bit big for `cat`; try viewing it again with `less`. You can move around in `less` using ENTER, SPACE, and your arrow keys. To exit less, press the letter `q`. (note that if this is the first time you've used the terminal, your .bash_history file may still be very small)
49
+-  The last command for you to try out is `grep`. You can use this to search for a pattern in a file or other body of text. Try using it on that first file you created like this: `grep Hello myFile.txt`. Notice that the result is the whole line where the text you searched for occurs. What happens if you use grep to search for something that isn't in the file?
50
+
51
+
52
+# Part 2
53
+
54
+## Objectives
55
+
56
+1. After this lab students will know how to create, remove, and modify files and directories using the terminal
57
+3. By completing this lab students will gain exposure to man pages and the use of bash commands with arguments and options.
58
+
59
+
60
+## Overview
61
+
62
+In Part 1 lab you learned to navigate your file system in the terminal. In Part 2 you will learn how to actually make changes to the file system and where to find documentation to get a better grasp on commands you are already familiar with as well as those you haven't seen before. The commands you will practice in this lab are:
63
+
64
+- `touch`
65
+- `mkdir`
66
+- `rmdir`
67
+- `rm`
68
+- `cp`
69
+- `mv`
70
+- `<`,`>`,`>>`,`|`
71
+
72
+
73
+## Instructions
74
+
75
+
76
+### Creating files and directories
77
+
78
+In lab 1 you learned one way to create a file using the `echo` command and output redirection. That can be useful but there are other methods that make more sense sometimes. Here we'll focus on two other methods:
79
+
80
+- the `touch` command.
81
+- file editors.
82
+
83
+#### Creating files with touch
84
+
85
+To create a file using touch, simply type the command followed by the name of the file you want to create, like this: `touch filename`. Touch is actually meant to update the timestamp on files, but as a side effect it creates files that are named but don't already exist. It's time to practice `touch`ing files
86
+
87
+- Use `touch` to create two files, called myfile1 and myfile2
88
+- Try using `touch` to create three more files, but all in the same command. Just give touch the filenames you want one right after the other, separated by a space
89
+- Use the `ls -lT` (that's a lowercase 'L' after the dash) command to see the full timestamps of the files you just created. Notice that the first two are different but the last three are all the same.
90
+
91
+-
92
+
93
+#### Editors
94
+
95
+There are many text editors out there. We will be using an editor called VIM (short for VI iMproved -- it is a clone of the editor VI with some improvements). Other options that you can explore on your own include emacs and nano (a clone of a program called pico). Here are the VIM commands we'll use along with a few other useful basics (there are many, many, many many more):
96
+
97
+- `:w` - Write; writes the current contents of the buffer (editor) to a file. 
98
+- `:q` - Quit; Quits VIM. Complains if you have unsaved changes (use `:w` first, or `:q!` to force quit and abandon your changes)
99
+- `:set number`/`:set nonumber` - Enables/disables line numbers.
100
+- `a` - Switch to insert mode after the current character. `A` goes to the end of the line instead.
101
+- `i` - Switch to insert mode before the current character. `I` goes to the beginning of the line instead
102
+- `x` - Delete the character under the cursor.
103
+- `dd` - Delete the current line.
104
+- `h``j``k``l` - Work like the left, down, up, and right arrow keys respectively.
105
+
106
+-
107
+
108
+- Let's create a file with vim. Start by opening the program with the `vim` command.
109
+- This is the editor screen. VIM has two modes, insert mode and normal mode; it starts in normal mode. The `esc` key switches to normal mode from insert mode but not the other way -- if you ever get lost just hit escape.
110
+- We want to create a file, but we don't need anything in it yet. To save your current file you can use the `:w` command from normal mode. Type `:w myfile6` to save this empty file as "myfile6"
111
+- Quit VIM with the `:q` command and list the contents of your directory to confirm that you created a file (Shortcut: `:wq` allows you to save a file and quit with just one command)
112
+- Open that file by running vim with the filename. This time we'll add a little text.
113
+- Switch to insert mode by pressing the `a` key and type the message "Hello Editor!". Remember to press `esc` when you finish to switch back to normal mode.
114
+- Save the file (remember how?)
115
+- Let's make that message a little more specific. Delete the word 'Editor' and replace it with VIM. Look at the list of commands to see how to do this. Save the changed file.
116
+- One powerful feature in VIM is the ability to repeat commands automatically. Type the number 10 before using the `A` (note that it's capitalized) command to add text to the end of the line; Press enter to go to the next line and then type a short message. See what happens when you finish the message and press `esc`
117
+- Now that you have several lines in your file, line numbers might be helpful. Enable them using the `:set number` command.
118
+- Ten copies of the same message might be a bit too much. Move the cursor to line 2 and type `9dd`. You should now see only one line of your message.
119
+
120
+#### Creating directories
121
+
122
+
123
+- Use `mkdir` to make a directory with your name
124
+- make two more directories inside of that one called "dir1" and "dir2"
125
+- You can make directories in other places than your current directory, without `cd`ing into it, make a directory inside dir1. When you give the location of a file or directory relative to your current position that's called a relative path
126
+- Try making a directory inside dir2 by giving an absolute path. Absolute paths list the exact location of a file -- you've seen this before with the pwd command.
127
+
128
+### Copying, moving, and renaming
129
+- use the `cp` command to copy the file you wrote in the last section. Try using the man file for cp to see what the correct syntax is (anything between [] braces is optional)
130
+- Make a new directory and then move the copied file you just created into that directory using the `mv` command.
131
+- Let's change the name of that file. The `mv` command doesn't have to move a file to a new location; it can simply change the name of the file in place. Change the name to something that starts with a dot (.) such as ".myFile"
132
+- Now check the contents of your directory with `ls`. Where is the file you just renamed? In Linux and Unix systems, files and directories with names starting in "." are hidden, so by renaming your file you actually made it a hidden file. Let's see how we can view hidden files with `ls`
133
+- The `man` command lets you view the man(ual) pages for any command that is documented. Try viewing the man pages for ls, cp, and mv. Once you've looked at each of these and have a feel for how they are laid out, take another look at the ls man page and see if you can figure out how to view hidden files (remember, hidden files begin with a dot '.' ) <sub>**hint**: viewing man pages is like using less; you can move up and down in the file with the keyboard and when you want to exit you can press the 'q' key*</sub>
134
+
135
+### Destroying files and directories
136
+
137
+- Make three files and two directories any way you like; create a file inside one of those directories.
138
+- The `rm` command deletes files, try using that on one of the files you created.
139
+- Another way to delete a file is the `unlink` command. Use this to delete the second file.
140
+- Sometimes you want to make sure you are deleting the right files (particularly if you use the * wildcard). use `rm -i` to delete the last file and notice that you have to confirm it before it's deleted.
141
+- Try to delete one of those directories with `rm`. Notice that on its own it won't delete directories.
142
+- Use `rmdir` (or `rm -d`) to delete the empty directory, then try it on the directory with a file in it.
143
+- Directories can only be deleted when they are empty; this could get painful if you have a lot of files and directories inside of one you want to delete. Try deleting that directory again with the `rm -R` command. The `-R` flag tells `rm` to recursively delete the contents of each directory it encounters and then delete the directory.
144
+- **Bonus**: run the command `echo "Hello" >-toughFile` and then try to delete the file created by that command. There are at least two ways to do this.
145
+
146
+
147
+### The Art of Redirection
148
+
149
+Remember in the previous lab when you typed the command `echo "Hello Terminal" >myfile.txt`? That 'greater than' sign is actually a special character in bash used for redirecting output to a file. There are several of these operators for redirecting output and input. The main redirection operators to be aware of are as follows:
150
+
151
+- `<` - Redirects input to come from the specified file. By default input comes from a stream called STDIN (STandarD INput)
152
+- `>` - Redirects output to the specified file (if the file exists its contents are erased first). By default normal program output is directed to a stream called STDOUT (STandarD OUTput) and errors are directed to a stream called STDERR (STandarD ERRor -- Redirect STDERR using `2>` instead)
153
+- `>>` - Appends output to the specified file instead of overwriting it
154
+- `|` - redirects the output from the previous command to be the input of the next command.
155
+
156
+-
157
+
158
+
159
+- Use the `find` command to search for all files on your machine with "core' in the name. You can do that with `find / -name core`.
160
+- That's a lot of "Permission denied" errors, and they make it hard to see the useful output from that command. Fortunately that's where output redirection becomes helpful: Those errors all go to STDERR, while the output we want is going to STDOUT. Use the commands above to redirect STDERR to `/dev/null`, effectively throwing away the output from STDERR.
161
+- That's a little better. We want to search for specific core files in this output, so let's redirect STDOUT to a file called `corefiles.log`.
162
+- Next, use `grep` to search for any lines that contain the string 'lib'. We want the search to be case insensitive so we'll use the `-i` flag like so: `grep -i lib corefiles.log` (**note**: your file may have very few lines in it if your computer is rather new and has not had many programs installed yet. It may seem silly to use grep to search for a particular line in a file with only two lines, but when you're searching for an error that occurred only once in over 10000 lines of debugging output you will want to know this tool well.)
163
+- Great, but there's a quicker way to do this without creating a file and running two separate commands. Try using the `|` (pipe) character to redirect the output from `find` to the input for `grep`. Don't forget to throw out the STDERR output; we don't need grep to search through that since we already know it won't contain the files we're looking for.
164
+
165
+#### Backgrounding
166
+
167
+Did you notice how you had to wait a few seconds every time you ran that find command? Sometimes you may need to use a long-running command and don't want it to lock up your terminal the whole time it's running. That's when you would want to run it in the background (by default, any command you run on a terminal runs in the foreground. Let's play with backgrounding and foregrounding a bit.
168
+
169
+- Run that same `find | grep` command that you used in the last step, but add the background operator, `&`, to the end of it. Notice that you immediately get a prompt, but after a few seconds the result of the grep prints to the command line.
170
+- For now we won't worry about that output getting in the way, but this is another good example where you may want to redirect STDOUT to a file.
171
+
172
+Now let's practice one more backgrounding technique with a different command.
173
+
174
+- Run the command `python -m SimpleHTTPServer`. This will run forever until you kill it with ~~fire~~ `Ctrl`+`C`
175
+- Now you're stuck without your prompt; but we can fix that! Press `Ctrl`+`Z` to suspend (not kill) that program.
176
+- Now you've got your terminal back, but if you type `jobs` you'll see that python is currently stopped. You can bring it back with `fg`, but that will block your prompt again; instead, use `bg` to resume it in the background.
177
+- Now that you've resumed python in the background, you can see it listed as running in `jobs`. Bring it back to the foreground with `fg` and then kill it with `Ctrl`+`C`
178
+ 
179
+
180
+## Other Resources
181
+
182
+- For more practice with the basics of the terminal, try the [Command Line Crash Course](http://learnpythonthehardway.org/book/appendixa.html).
183
+
184
+- `vimtutor` -- type this command on your command line for a built-in lesson in VIM basics.
185
+
186
+## Links:
187
+
188
+1. "Often misused" https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat
189
+2. "more or less" http://unix.stackexchange.com/questions/81129/what-are-the-differences-between-most-more-and-less
190
+3. "Command Line Crash Course" http://learnpythonthehardway.org/book/appendixa.html
191
+4. "VIM 101" https://www.linux.com/learn/vim-101-beginners-guide-vim
192
+