You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/01-what-is-the-shell.md
+6-2Lines changed: 6 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,14 +10,18 @@
10
10
11
11
The shell (sometimes referred to as the “Unix shell”, for the operating system where it was first developed) is a program that allows you to interact with your computer using typed text commands. It is the primary interface used on Linux and Unix-based systems, such as macOS, and can be optionally installed on other operating systems such as older versions of Windows. As of Windows 10 the Unix shell comes pre-installed but must be turned on.
12
12
13
+
The most popular Unix shell is Bash (the Bourne Again SHell). Using the shell has a steep learning curve compared with the GUI. While a GUI presents you with choices to select, the command-line interface (CLI) does not present the choices to you. The syntax of the shell allows you to make powerful pipelines by coupling existing commands and handle repetitive tasks. Many shells support scripting as well.
14
+
15
+
In addition, the command line is often the best option to interact with remote machines and servers. As clusters and cloud computing systems become more common, researchers need to learn the necessary skills to tackle file handling and job submission tasks.
16
+
13
17
## Why should I use it?
14
18
15
19
Most of the time when we work with files on a computer the graphical user interface (GUI) that we are familiar with does the job. This is how we visually navigate, scroll, and click through information on our computers.
16
20
17
-
It's when we are dealing with a large number of files or files that are scattered across many different locations that the shell is very useful. Any time that you need to do something very repetitive to a large number of files, for example renaming everything in a folder or separating out all of a certain type of file, there is likely a way to use the shell to speed up your work.
21
+
It's when we are dealing with a large number of files or folders that are scattered across many different locations that the shell is very useful. Any time that you need to do something very repetitive to a large number of files, for example renaming everything in a folder or separating out all of a certain type of file, there is likely a way to use the shell to speed up your work.
18
22
19
23
## How do I get to it?
20
24
21
25
For Mac and Linux, which are Unix based systems, you can access the Unix shell through an application called "Terminal". It is findable like any other piece of software installed on your computer.
22
26
23
-
For Windows 10 users the Unix shell needs to be turned on but is available as a part of the operating system. Previous versions of Windows required you to install it separately.
27
+
For Windows 10 users, the Unix shell needs to be turned on but is available as a part of the operating system. You can find more information in per [Simplified Installation](https://docs.microsoft.com/en-us/windows/wsl/install-win10#simplified-installation-for-windows-insiders).Previous versions of Windows required you to install it separately.
Copy file name to clipboardExpand all lines: content/02-navigating-the-file-system.md
+49-6Lines changed: 49 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
---
7
7
# Navigating the file system
8
8
9
-
Let's start by opening the unix shell. There are a few things you are likely to see right away. A cursor that is flashing next to a $ indicates where the command line begins. If you see the dollar sign it means that the command line is ready to take commands.
9
+
Let's start by opening the unix shell. There are a few things you are likely to see right away. A cursor that is flashing next to a $, a **prompt**, indicates where the command line begins. If you see the dollar sign it means that the command line is ready to take commands.
10
10
11
11
~~~
12
12
$
@@ -15,7 +15,8 @@ $
15
15
You are communicating with your computer by giving it commands and having it react to those commands.
16
16
17
17
## Orienting yourself
18
-
Let's enter our first command "pwd" which stands for "print working directory" and will tell us where we are in the system. The term "directory" refers to what you might visually think of as a "folder" when browsing your file system.
18
+
19
+
Let's enter our first command `pwd` which stands for **print working directory** and will tell us where we are in the system. The term "directory" refers to what you might visually think of as a "folder" when browsing your file system.
19
20
20
21
Input
21
22
{: .label .label-green}
@@ -28,6 +29,8 @@ Output
28
29
/Users/egrguric
29
30
~~~
30
31
32
+
We can use commands to open GUI for applications. For example, `open` opens finders on Mac. A dot, `.`, points to the current working directory.
33
+
31
34
Input
32
35
{: .label .label-green}
33
36
~~~
@@ -36,6 +39,10 @@ $ open .
36
39
37
40

38
41
42
+
In a Unix filesystem, the **root directory** is referred by a single slash character, `/`. Inside this directory, there are several important directories: `bin` contains built-in programs, `data` contains miscellaneous data, `Users` contains user data, and `tmp` contains temporary files that do not need to stored long-term.
43
+
44
+
To see the content of the filesystem in the terminal, `ls` is used.
45
+
39
46
Input
40
47
{: .label .label-green}
41
48
~~~
@@ -50,6 +57,8 @@ Desktop Movies Vagrants
50
57
Documents Music
51
58
~~~
52
59
60
+
We can tailor the output of commands by adding options and arguments. For example, `-l` option makes `ls` use a long listing format showing additional information such as the file size and the time of its last modification.
The first field of information in the list above is the file type. A file is represented by a hyphen, `-`, and a directory is represented by the letter `d`. The rest of it represent the permission groups: owner, group, and other.
108
+
109
+
In Unix, there are three permission groups. The first three letters shows the permissions used by the assigned owner of the file or directory. Each permission group has three permissions, called a permission set. A permission set consists of read, write, and execute permissions, represented by `r`, `w`, and `x`, respectively. A dash symbol in place of a character in a permission set indicated that a particular permission is denied. Linux assigns initial permissions automatically when a new file or directory is created.
110
+
111
+
Every file is owned by a specific user (or UID) and a specific group (or GID). To change the user or group of a file, we can use `chown` command.
112
+
113
+
Input
114
+
{: .label .label-green}
115
+
~~~
116
+
$ chown user filename
117
+
$ chown user:group filename
118
+
~~~
119
+
120
+
The `chmod` command is used to alter the permission of a file. For example, to add execute permission for the owner of a file:
121
+
122
+
Input
123
+
{: .label .label-green}
124
+
~~~
125
+
$ chmod u+x filename
126
+
~~~
127
+
94
128
## Getting help
95
-
Commands come with instructions that you can access from the command line by invoking the "manual". You can also access this manual documentation online.
129
+
130
+
Every shell command, including `ls`, has lots of other options. There are two common ways to find out how to use a command and the options available for it. We can read the **manual** of a command with `man`
96
131
97
132
Input
98
133
{: .label .label-green}
@@ -129,11 +164,19 @@ DESCRIPTION
129
164
~~~
130
165
Notice the colon : which indicates that there is more text and you can keep paging through it by hitting "enter". To quit the manual hit the "q" key which stands for "quit".
131
166
132
-
You can only go to places that exist.
167
+
We can also pass `--help` optino to the command:
168
+
169
+
Input
170
+
{: .label .label-green}
171
+
~~~
172
+
$ ls --help
173
+
~~~
174
+
175
+
You can also find the manual pages on the web.
133
176
134
177
## Moving around
135
178
136
-
To move between directories we can use the "cd" command which stands for "change directory". This will move you one step forward in the system.
179
+
To move between directories we can use the `cd` command which stands for **change directory**. It should be followed by a directory name to change the working directory. To move to the `Desktop` directory:
137
180
138
181
Input
139
182
{: .label .label-green}
@@ -149,7 +192,7 @@ Output
149
192
150
193
When typing a directory name, the case of the directory doesn't matter on Mac or PC but does on Linux.
151
194
152
-
To move backwards in a directory use "cd .." which takes you back one step or "cd -" which jumps you back to the beginning and prints the directory (in the same way pwd would).
195
+
To move backwards in a directory use "cd .." which takes you back one step or "cd -" which moves you back to the previous directory and prints the directory (in the same way pwd would).
Copy file name to clipboardExpand all lines: content/03-working-with-files-and-directories.md
+74-15Lines changed: 74 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,10 +23,11 @@ Output
23
23
/Users/egrguric/desktop
24
24
~~~
25
25
26
-
If a directory of filename is too long to type you can use "tab" to autocomplete directory and file names quickly.
26
+
If a directory of filename is too long to type, you can type the first few letters to differentiate the directory and use "tab" to autocomplete directory and file names quickly.
27
27
28
28
## Creating a directory
29
-
Let's try creating a directory and moving in and out of it.
29
+
30
+
Let's try creating a directory and moving into it.
30
31
31
32
Input
32
33
{: .label .label-green}
@@ -47,7 +48,8 @@ Output
47
48
48
49

49
50
50
-
# Creating a file
51
+
## Creating a file
52
+
51
53
Now let's create a file. There are built-in text editors accessible through the shell. These editors are handy if you need to make a quick change from the shell or want to avoid having to open another program while you are working in it. Nano is a commonly used text editor.
52
54
53
55
Input
@@ -64,6 +66,12 @@ myfile
64
66
myfirstdirectory
65
67
~~~
66
68
69
+
Note: The command line interpreter might confuse the spaces used in the names of files and directories with spaces used to separate arguments. Thus, it is better to avoid them in names of files and directories and use `.`, `-`, or `_` instead. If you need to refer to names of files or directories that have spaces or other special characters, you should surround the name in quotes (`""`).
70
+
71
+
Note: `nano` is one of the least complex text editors in the Terminal. However, it is not popular among professional developer because it is not powerful and flexible enough. Since `nano` is installed by default on Unix shells, it is also a good choice for editing text files on remote machines. If you would like to know more about text editors, check out the [Introduction to Development Environment](https://ubc-library-rc.github.io/intro-development-environment/content/02.Editors.html) workshop.
72
+
73
+
The convention for naming files is `filename.extension`, where `extension` identifies the type of the file. For example, `.txt` signals a plain text file while `.png` signals a PNG image, and so on. Each filetype has its own encoding and should be openned by an application that decodes the format. Using the correct extension might cause the operating system to try and open it with the appropriate application.
74
+
67
75
## Moving, copying, and deleting files and directories
68
76
69
77
Now let's try moving our new file into our new directory to organize things a bit.
@@ -81,7 +89,7 @@ Output
81
89
myfile.txt
82
90
~~~
83
91
84
-
The mv function also renames files.
92
+
The first argument contains the file we are moving and the second one is where it is to go. To rename a file, you can use the new name as the destination in a `mv` command.
85
93
86
94
Input
87
95
{: .label .label-green}
@@ -95,14 +103,18 @@ Output
95
103
mynewfile.txt
96
104
~~~
97
105
98
-
The "cp" command lets you copy files, for example if you want to create a backup.
106
+
Note: Be careful when specifying the file name. `mv` silently overwrite any existing files with the same name and could cause data loss. With the option `-i`, you can set `mv` to ask for confirmation before writing.
107
+
108
+
The "cp" command lets you copy files, for example if you want to create a backup, and it works very much like `mv`.
99
109
100
110
Input
101
111
{: .label .label-green}
102
112
~~~
103
113
$ cp mynewfile.txt backup_mynewfile.txt
104
114
~~~
105
115
116
+
You can use the `-r` option to copy a directory and all its contents. `-r` specifies the recursive copying.
117
+
106
118
And you can delete files through the "rm" command which stands for remove.
107
119
108
120
Input
@@ -117,7 +129,9 @@ Output
117
129
mynewfile.txt
118
130
~~~
119
131
120
-
You can also delete directories but that requires an additional (and dangerous) flag. -r stands for "recursive" which essentially tells the command "rm" to keep going through all of the files that are inside the directory as well until there's nothing left.
132
+
You can also delete directories but that requires an additional (and dangerous) flag. `-r` stands for "recursive" which essentially tells the command "rm" to keep going through all of the files that are inside the directory as well until there's nothing left. Be careful when using this option, as the Unix shell does not have a trash bin and deleted files are unlinked from the file system. Tools for finding and recovering deleted files exists, but there's no guarantee they will work in any particular situation.
133
+
134
+
Note: with `-i` option, the shell prompt before removal and we have a chance to checkl that we are deleting only the files we want to remove.
121
135
122
136
Input
123
137
{: .label .label-green}
@@ -133,6 +147,16 @@ Output
133
147
134
148
~~~
135
149
150
+
### Using wildcards
151
+
152
+
`*` is a wildcard, which matches zero or more characters. When the shell sees a wildcard, it expands the wildcard and creates a list of matching filesnames before running the command. For example, if you want to copy all the PDF files in a directory, you can run the following command:
153
+
154
+
Input
155
+
{: .label .label-green}
156
+
~~~
157
+
$ cp firstdirectory/*.pdf seconddirectory/
158
+
~~~
159
+
136
160
## Reading files
137
161
138
162
Let's make a few files to work with for the next activity.
@@ -161,7 +185,7 @@ Input
161
185
$ ls -lh
162
186
~~~
163
187
164
-
But you can also quickly open the entirety of the file from the command line.
188
+
But you can also quickly open the entirety of the file from the command line. The `cat` command gets its name from 'concatenate' and prints the contents of files on the screen.
165
189
166
190
Input
167
191
{: .label .label-green}
@@ -197,6 +221,8 @@ Part 4
197
221
Part 5
198
222
~~~
199
223
224
+
If the file is large, you might want to get only the first few lines. WE can run another command called `head` for that. Using `-n 5` option with `head` tells it to print only the first 5 lines of the file. The command `head` defaults to showing the first ten lines in a file.
225
+
200
226
Input
201
227
{: .label .label-green}
202
228
~~~
@@ -218,9 +244,23 @@ Step 4
218
244
219
245
~~~
220
246
221
-
You can ask head to show more or less as needed.
247
+
You can use this method to quickly view all of a type of file in a directory.
248
+
249
+
Input
250
+
{: .label .label-green}
251
+
~~~
252
+
$ head *.txt
253
+
~~~
222
254
223
-
"Tail" works similarly
255
+
Or only the first line of all of the files which might be useful if there are a lot of them.
256
+
257
+
Input
258
+
{: .label .label-green}
259
+
~~~
260
+
$ head -n1 *.txt
261
+
~~~
262
+
263
+
`tail` works similarly and prints the end of your files.
224
264
225
265
Input
226
266
{: .label .label-green}
@@ -243,6 +283,8 @@ Step 6
243
283
World domination!
244
284
~~~
245
285
286
+
`less` can be used to read the content of a file **one screen** at a time.
287
+
246
288
Input
247
289
{: .label .label-green}
248
290
~~~
@@ -266,22 +308,39 @@ Step 5
266
308
:
267
309
~~~
268
310
269
-
Use the enter key to move through the file and "q" to "quit" or exit the file.
311
+
Use the enter key to move through the file and `q` to "quit" or exit the file.
270
312
271
-
The command "head" defaults to showing the first ten lines in a file.
313
+
## Finding things
272
314
273
-
You can use this method to quickly view all of a type of file in a directory.
315
+
`grep` is the name of a very useful command-line program that searches within files for a particular pattern. For example, to find lines that contain the number `3` in our file, we run:
274
316
275
317
Input
276
318
{: .label .label-green}
277
319
~~~
278
-
$ head *.txt
320
+
$ grep 3 file1.txt
321
+
~~~
322
+
Output
323
+
{: .label .label-yellow}
324
+
~~~
325
+
Step 3
279
326
~~~
280
327
281
-
Or only the first line of all of the files which might be useful if there are a lot of them.
328
+
To search for a phrase, we should put the phrase in quotes.
329
+
330
+
While `grep` finds lines in files, the `find` command find files within directories.
282
331
283
332
Input
284
333
{: .label .label-green}
285
334
~~~
286
-
$ head -n1 *.txt
335
+
$ find .
336
+
~~~
337
+
338
+
As `.` means the current working directory, `find`'s output is the names of all the files and directories under the current working directory. With `-name` option, we can specify conditions on the names of the files or directories:
0 commit comments