|
|
@@ -3,6 +3,7 @@
|
|
3
|
3
|
## Objectives
|
|
4
|
4
|
|
|
5
|
5
|
1. In this lab students will learn to navigate their file system using only the terminal
|
|
|
6
|
+
|
|
6
|
7
|
2. Students will practice using the following commands to traverse directories and investigate their contents: `pwd`, `ls`, `cd`, `cat`, `less`, `grep`
|
|
7
|
8
|
|
|
8
|
9
|
|
|
|
@@ -19,22 +20,18 @@ The console (also referred to as the command line, terminal, or shell) is an ess
|
|
19
|
20
|
- `grep` - search for the specified text or pattern
|
|
20
|
21
|
|
|
21
|
22
|
### Other commands worth knowing
|
|
22
|
|
-
|
|
23
|
23
|
- `head` - display the first lines of a file
|
|
24
|
24
|
- `tail` - display the last lines of a file
|
|
25
|
25
|
|
|
26
|
26
|
|
|
27
|
27
|
|
|
28
|
28
|
## Unit Test
|
|
29
|
|
-
|
|
30
|
29
|
This lab is an instructional introduction to terminal. There are no unit tests.
|
|
31
|
30
|
|
|
32
|
31
|
## Pre-lab && Important Notes
|
|
33
|
|
-
|
|
34
|
32
|
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
|
33
|
|
|
36
|
34
|
## Instructions
|
|
37
|
|
-
|
|
38
|
35
|
- Open up a terminal (Applications->Utilities->Terminal)
|
|
39
|
36
|
- Find out what directory you're in using `pwd`
|
|
40
|
37
|
- You know where you are, but not what's around you. Take a look around with `ls`
|
|
|
@@ -52,13 +49,11 @@ It's a good practice to have a directory called `Dev` inside your home directory
|
|
52
|
49
|
# Part 2
|
|
53
|
50
|
|
|
54
|
51
|
## Objectives
|
|
55
|
|
-
|
|
56
|
52
|
1. After this lab students will know how to create, remove, and modify files and directories using the terminal
|
|
57
|
53
|
3. By completing this lab students will gain exposure to man pages and the use of bash commands with arguments and options.
|
|
58
|
54
|
|
|
59
|
55
|
|
|
60
|
56
|
## Overview
|
|
61
|
|
-
|
|
62
|
57
|
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
|
58
|
|
|
64
|
59
|
- `touch`
|
|
|
@@ -74,14 +69,12 @@ In Part 1 lab you learned to navigate your file system in the terminal. In Part
|
|
74
|
69
|
|
|
75
|
70
|
|
|
76
|
71
|
### Creating files and directories
|
|
77
|
|
-
|
|
78
|
72
|
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
|
73
|
|
|
80
|
74
|
- the `touch` command.
|
|
81
|
75
|
- file editors.
|
|
82
|
76
|
|
|
83
|
77
|
#### Creating files with touch
|
|
84
|
|
-
|
|
85
|
78
|
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
|
79
|
|
|
87
|
80
|
- Use `touch` to create two files, called myfile1 and myfile2
|
|
|
@@ -91,10 +84,9 @@ To create a file using touch, simply type the command followed by the name of th
|
|
91
|
84
|
-
|
|
92
|
85
|
|
|
93
|
86
|
#### Editors
|
|
94
|
|
-
|
|
95
|
87
|
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
|
88
|
|
|
97
|
|
-- `:w` - Write; writes the current contents of the buffer (editor) to a file.
|
|
|
89
|
+- `:w` - Write; writes the current contents of the buffer (editor) to a file.
|
|
98
|
90
|
- `:q` - Quit; Quits VIM. Complains if you have unsaved changes (use `:w` first, or `:q!` to force quit and abandon your changes)
|
|
99
|
91
|
- `:set number`/`:set nonumber` - Enables/disables line numbers.
|
|
100
|
92
|
- `a` - Switch to insert mode after the current character. `A` goes to the end of the line instead.
|
|
|
@@ -118,8 +110,6 @@ There are many text editors out there. We will be using an editor called VIM (sh
|
|
118
|
110
|
- 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
|
111
|
|
|
120
|
112
|
#### Creating directories
|
|
121
|
|
-
|
|
122
|
|
-
|
|
123
|
113
|
- Use `mkdir` to make a directory with your name
|
|
124
|
114
|
- make two more directories inside of that one called "dir1" and "dir2"
|
|
125
|
115
|
- 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
|
|
|
@@ -133,7 +123,6 @@ There are many text editors out there. We will be using an editor called VIM (sh
|
|
133
|
123
|
- 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
|
124
|
|
|
135
|
125
|
### Destroying files and directories
|
|
136
|
|
-
|
|
137
|
126
|
- Make three files and two directories any way you like; create a file inside one of those directories.
|
|
138
|
127
|
- The `rm` command deletes files, try using that on one of the files you created.
|
|
139
|
128
|
- Another way to delete a file is the `unlink` command. Use this to delete the second file.
|
|
|
@@ -145,7 +134,6 @@ There are many text editors out there. We will be using an editor called VIM (sh
|
|
145
|
134
|
|
|
146
|
135
|
|
|
147
|
136
|
### The Art of Redirection
|
|
148
|
|
-
|
|
149
|
137
|
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
|
138
|
|
|
151
|
139
|
- `<` - Redirects input to come from the specified file. By default input comes from a stream called STDIN (STandarD INput)
|
|
|
@@ -153,9 +141,6 @@ Remember in the previous lab when you typed the command `echo "Hello Terminal" >
|
|
153
|
141
|
- `>>` - Appends output to the specified file instead of overwriting it
|
|
154
|
142
|
- `|` - redirects the output from the previous command to be the input of the next command.
|
|
155
|
143
|
|
|
156
|
|
--
|
|
157
|
|
-
|
|
158
|
|
-
|
|
159
|
144
|
- 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
|
145
|
- 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
|
146
|
- 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`.
|
|
|
@@ -163,7 +148,6 @@ Remember in the previous lab when you typed the command `echo "Hello Terminal" >
|
|
163
|
148
|
- 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
|
149
|
|
|
165
|
150
|
#### Backgrounding
|
|
166
|
|
-
|
|
167
|
151
|
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
|
152
|
|
|
169
|
153
|
- 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.
|
|
|
@@ -178,7 +162,6 @@ Now let's practice one more backgrounding technique with a different command.
|
|
178
|
162
|
|
|
179
|
163
|
|
|
180
|
164
|
## Other Resources
|
|
181
|
|
-
|
|
182
|
165
|
- For more practice with the basics of the terminal, try the [Command Line Crash Course](http://learnpythonthehardway.org/book/appendixa.html).
|
|
183
|
166
|
|
|
184
|
167
|
- `vimtutor` -- type this command on your command line for a built-in lesson in VIM basics.
|
|
|
@@ -192,4 +175,3 @@ The next lab is [here](https://github.com/Zipcoder/ZCW-Microlabs-Git).
|
|
192
|
175
|
2. "more or less" http://unix.stackexchange.com/questions/81129/what-are-the-differences-between-most-more-and-less
|
|
193
|
176
|
3. "Command Line Crash Course" http://learnpythonthehardway.org/book/appendixa.html
|
|
194
|
177
|
4. "VIM 101" https://www.linux.com/learn/vim-101-beginners-guide-vim
|
|
195
|
|
-
|