Skip to content

Commit 976a823

Browse files
committed
Add permission and pipelines
1 parent faffc11 commit 976a823

10 files changed

+234
-40
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Gemfile.lock
2+
.jekyll-cache/
3+
_site/

Feedback.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
nav_exclude: true
3+
---
4+
5+
### What to keep:
6+
7+
- Hands-on experience
8+
- Reference to the website
9+
10+
### What to improve:
11+
12+
- Drop loops and introduce permissions, ownership, and process management

Gemfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
git_source(:github) {|repo_name| "https://github.com/ubc-library-rc/intro-shell/" }
6+
7+
# gem "rails"
8+
9+
gem "jekyll"
10+
gem 'github-pages', group: :jekyll_plugins
11+
File renamed without changes.

content/01-what-is-the-shell.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010

1111
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.
1212

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+
1317
## Why should I use it?
1418

1519
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.
1620

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.
1822

1923
## How do I get to it?
2024

2125
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.
2226

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.

content/02-navigating-the-file-system.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
---
77
# Navigating the file system
88

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.
1010

1111
~~~
1212
$
@@ -15,7 +15,8 @@ $
1515
You are communicating with your computer by giving it commands and having it react to those commands.
1616

1717
## 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.
1920

2021
Input
2122
{: .label .label-green}
@@ -28,6 +29,8 @@ Output
2829
/Users/egrguric
2930
~~~
3031

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+
3134
Input
3235
{: .label .label-green}
3336
~~~
@@ -36,6 +39,10 @@ $ open .
3639

3740
![Finder window](finder.png)
3841

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+
3946
Input
4047
{: .label .label-green}
4148
~~~
@@ -50,6 +57,8 @@ Desktop Movies Vagrants
5057
Documents Music
5158
~~~
5259

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.
61+
5362
Input
5463
{: .label .label-green}
5564
~~~
@@ -70,6 +79,8 @@ drwx------+ 4 egrguric staff 128 25 Jul 13:56 Pictures
7079
drwxr-xr-x+ 4 egrguric staff 128 18 Jul 2019 Public
7180
~~~
7281

82+
By adding `-h` option, the file size becomes human readable.
83+
7384
Input
7485
{: .label .label-green}
7586
~~~
@@ -91,8 +102,32 @@ drwxr-xr-x+ 4 egrguric staff 128B 18 Jul 2019 Public
91102
92103
~~~
93104

105+
## Permissions and ownership
106+
107+
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+
94128
## 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`
96131

97132
Input
98133
{: .label .label-green}
@@ -129,11 +164,19 @@ DESCRIPTION
129164
~~~
130165
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".
131166

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.
133176

134177
## Moving around
135178

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:
137180

138181
Input
139182
{: .label .label-green}
@@ -149,7 +192,7 @@ Output
149192

150193
When typing a directory name, the case of the directory doesn't matter on Mac or PC but does on Linux.
151194

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).
153196

154197
Input
155198
{: .label .label-green}

content/03-working-with-files-and-directories.md

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ Output
2323
/Users/egrguric/desktop
2424
~~~
2525

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.
2727

2828
## 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.
3031

3132
Input
3233
{: .label .label-green}
@@ -47,7 +48,8 @@ Output
4748

4849
![Finder window with empty directory](myfirstdir.png)
4950

50-
# Creating a file
51+
## Creating a file
52+
5153
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.
5254

5355
Input
@@ -64,6 +66,12 @@ myfile
6466
myfirstdirectory
6567
~~~
6668

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+
6775
## Moving, copying, and deleting files and directories
6876

6977
Now let's try moving our new file into our new directory to organize things a bit.
@@ -81,7 +89,7 @@ Output
8189
myfile.txt
8290
~~~
8391

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.
8593

8694
Input
8795
{: .label .label-green}
@@ -95,14 +103,18 @@ Output
95103
mynewfile.txt
96104
~~~
97105

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`.
99109

100110
Input
101111
{: .label .label-green}
102112
~~~
103113
$ cp mynewfile.txt backup_mynewfile.txt
104114
~~~
105115

116+
You can use the `-r` option to copy a directory and all its contents. `-r` specifies the recursive copying.
117+
106118
And you can delete files through the "rm" command which stands for remove.
107119

108120
Input
@@ -117,7 +129,9 @@ Output
117129
mynewfile.txt
118130
~~~
119131

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.
121135

122136
Input
123137
{: .label .label-green}
@@ -133,6 +147,16 @@ Output
133147
134148
~~~
135149

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+
136160
## Reading files
137161

138162
Let's make a few files to work with for the next activity.
@@ -161,7 +185,7 @@ Input
161185
$ ls -lh
162186
~~~
163187

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.
165189

166190
Input
167191
{: .label .label-green}
@@ -197,6 +221,8 @@ Part 4
197221
Part 5
198222
~~~
199223

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+
200226
Input
201227
{: .label .label-green}
202228
~~~
@@ -218,9 +244,23 @@ Step 4
218244
219245
~~~
220246

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+
~~~
222254

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.
224264

225265
Input
226266
{: .label .label-green}
@@ -243,6 +283,8 @@ Step 6
243283
World domination!
244284
~~~
245285

286+
`less` can be used to read the content of a file **one screen** at a time.
287+
246288
Input
247289
{: .label .label-green}
248290
~~~
@@ -266,22 +308,39 @@ Step 5
266308
:
267309
~~~
268310

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.
270312

271-
The command "head" defaults to showing the first ten lines in a file.
313+
## Finding things
272314

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:
274316

275317
Input
276318
{: .label .label-green}
277319
~~~
278-
$ head *.txt
320+
$ grep 3 file1.txt
321+
~~~
322+
Output
323+
{: .label .label-yellow}
324+
~~~
325+
Step 3
279326
~~~
280327

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.
282331

283332
Input
284333
{: .label .label-green}
285334
~~~
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:
339+
340+
Input
341+
{: .label .label-green}
287342
~~~
343+
$ find . -name *.txt
344+
~~~
345+
346+

0 commit comments

Comments
 (0)