【自用】MIT-Missing-Semester笔记
MIT-Missing-Semester
在csdiy上被安利的首个课程,虽然是全英文,而且整个课程比较硬核,但是确实是如标题所言,讲到了很多息息相关但是在课程里未曾涉及的知识,对我整个知识体系的完善有了很大帮。(还锻炼了点英语)
观看的时候,也不必强求理解所有东西,正如课程的一位教师所言,仅仅是知道有这样的东西存在,已经足够了。
另外,课后练习还是很推荐做的,有助于知识的巩固。
好啦,继续加油!
Lecture 1 Shell
1 |
|
distinguish between absolute path/relative path
ls -l
will return a string like
1 |
|
第一个rwx
表示root(user)用户具有read,write,和execute的权限;第二个r-x
表示其所在group owner能read和write;第三个r-x
表示everyone的权限。
对file来说,read、write、execute的意思很明显;对文件夹来说,read是表示能够浏览文件夹中的内容,write是表示能够重命名、创建,或删除文件;execute表示能否进入到这个文件夹中。没有execute,read和write无法进行。
ctrl+L 清空屏幕
$
表示你是user而非root,root对应的是#
在试图修改某些文件时(例如,修改亮度):
1 |
|
learn from practice:
touch
can change the information of files. If the file doesn’t exist,it will create one.```bash
usage of chmod
sudo chmod chmod ugo+r file1.txt # all can read
chmod a+r file1.txt # all can read
chmod 777 file # all can read,write,and execute it
chmod u=rw,go= file # user can write and read,group and other don’t have any power1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Lecture 2 Shell Tools
```bash
foo = bar # error,equal to execute "foo" with arguments like '=' and 'bar'
foo=bar # right.spaces are critical
echo $foo # print bar
echo 'value is $foo' # value is $foo
echo "value is $foo" # value is bar
vim mcd.sh
#mcd(){
# mkdir -p "$1" # the first argv
# cd "$1"
#}
source mcd.sh
mcd test # move into test folder
mkdir /mnt/new # error don't have permission
sudo !! # replace !! with mkdir /mnt/new
grep foobar mcd.sh
echo $? # error code 1
# the operator goes like what is in cpp
false || echo "opps fail"
true || echo "it won\'t be printed"
false && echo "it won\'t be printed"
foo=$(pwd)
echo "we are now in $(pwd)" # command substitution
cat <(ls) <(ls ..) # process substitution
# globbing
ls *.sh # all length is accepted
ls name? # ? only supports one length
# also brackets
convert image.{png,jpg}
# Will expand to
convert image.png image.jpg
cp /path/to/project/{foo,bar,baz}.sh /newpath
# Will expand to
cp /path/to/project/foo.sh /path/to/project/bar.sh /path/to/project/baz.sh /newpath
# Globbing techniques can also be combined
mv *{.py,.sh} folder
# Will move all *.py and *.sh files
# This creates files foo/a, foo/b, ... foo/h, bar/a, bar/b, ... bar/h
touch {foo,bar}/{a..h}
diff < folder1 < folder2
# below is a Python script
# the first line is a shebang which indicates that the kernel should use a Python interpreter instead of a shell command
# you can use command like #!/usr/bin/env python
# it takes advantage of the $PATH ,which will help to search the Python
#!/usr/local/bin/python
import sys
for arg in reversed(sys.argv[1:]):
print(arg)
tldr convert # tldr is a useful tool to give you practical commands example compared to manpage
# find
# Find all directories named src
find . -name src -type d
# Find all python files that have a folder named test in their path
find . -path '*/test/*.py' -type f
# Find all files modified in the last day
find . -mtime -1
# Find all zip files with size in range 500k to 10M
find . -size +500k -size -10M -name '*.tar.gz'
# you can also perform actions on the results of find
# Delete all files with .tmp extension
find . -name '*.tmp' -exec rm {} \;
# Find all PNG files and convert them to JPG
find . -name '*.png' -exec convert {} {}.jpg \;
# fd is a easier
fd ".*py"
# locate equals to find,but it uses inner database so it's faster but maybe not fresher than the find func.
updatedb # update database for locate
history
# grep search for the content in the files
grep -C 5 # Context.the result will print 5 lines before and after the match
grep -R # recursively find all files in the folders
# developed tools to take place of grep like ack ag rg is recommended
# Find all python files where I used the requests library
rg -t py 'import requests'
# Find all files (including hidden files) without a shebang line
rg -u --files-without-match "^#!"
# Find all matches of foo and print the following 5 lines
rg foo -A 5
# Print statistics of matches (# of matched lines and files )
rg --stats PATTERN
# fzf is a good tool combined with Ctrl+R to get the previous command you want to reuse
ls -R
# tree
# broot
these dollars:
name | func |
---|---|
$0 |
the name of the script that we’re running |
$1 to $9 |
the first to the ninth argument |
$# |
the num of arguments |
$$ |
the process ID of this command that is running |
$? |
return the code of the previous command |
$_ |
last argument from the last command |
$@ |
expand all arguments |
!! | entire last command(including arguments).A usual usage is like sudo !! |
Ctrl+shift+e
搜狗输入法切换英文自动联想()
ctrl+R
can perform backwards search
other multiple tools:
fish zsh fasd
autojump
tree
broot
Lecture 3 Editors(vim)
the Vim’s interface is a programming language
whenever you’re using your editor and you think “there must be a better way of doing this”, there probably is: look it up online.
1 |
|
Modal Editing
normal mode
h
leftj
downk
upl
rightw
b
e
- etc(below)
(
i
)insert mode(
r
)replace mode(
v
)visual mode- (
shift-v
) visual line - (
ctrl-v
) visual block
- (
(
:
)command line mode1
2
3
4
5
6
7
8
9:q # quit one window
:qa # quit all windows
:w # write
:wq # write and quit
:e {name of file} # open file for editing
:ls # show open buffers
:help {topic} # open help
:help :w # opens help for the :w command
:help w # opens help for the w movement
Movement
Basic movement:
hjkl
(left, down, up, right)Words:
w
(next word),b
(beginning of word),e
(end of word)Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line)Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen)Scroll:
Ctrl-u
(up),Ctrl-d
(down)File:
gg
(beginning of file),G
(end of file)Line numbers:
:{number}<CR>
or{number}G
(line {number})Misc:
%
(corresponding item)Find:
f{character}
t{character}
F{character}
T{character}
- find/to forward/backward {character} on the current line
,
/;
for navigating matchesSearch:
/{regex}
,n
/N
for navigating matchesctrl+g
:see the info of the file
Selection
Visual modes:
- Visual:
v
- Visual Line:
V
- Visual Block:
Ctrl-v
- Visual:
Edit
i
enter Insert mode
but for manipulating/deleting text, want to use something more than backspace
o
/O
insert line below / aboved{motion}
delete {motion}- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
c{motion}
change {motion}- e.g.
cw
is change word - like
d{motion}
followed byi
- e.g.
x
delete character (equal dodl
)s
substitute character (equal toxi
)Visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
u
to undo,<C-r>
to redoy
to copy / “yank” (some other commands liked
also copy)p
to pasteLots more to learn: e.g.
~
flips the case of a character
Count
3w
move 3 words forward5j
move 5 lines down7dw
delete 7 words
Modifiers
Some modifiers are
i
, which means “inner” or “inside”
anda
, which means “around”.ci(
change the contents inside the current pair of parenthesesci[
change the contents inside the current pair of square bracketsda'
delete a single-quoted string, including the surrounding single quotes
Lecture 4 Data Wrangling
regular expression(regexp)
1 |
|
the format always goes like s/REGEX/SUBSTITUTION/
some syntax:
.
means “any single character” except newline*
zero or more of the preceding match+
one or more of the preceding match[abc]
any one character ofa
,b
, andc
(RX1|RX2)
either something that matchesRX1
orRX2
^
the start of the line$
the end of the line
metacharacter | meaning |
---|---|
\d | digit |
\D | non-digit |
\w \W | is (or is not) digit nor an alphanumeric letter |
\s \S | is(or is not) a whitespace |
\t \T | is (or is not) a tab |
\b | the boundary between a word and a non-word character |
\0 | the whole captured group |
\1 .. \n | the sequential captured variables |
regular expression debugger
and multiple pipelines
这节课的知识量实在太大了..只能说,一个是学会正则表达式,另外一个是,明白linux中可以通过各种各样的指令实现很多数据提取。
Lecture 5 Command-line Environment
Job Control
signals
Ctrl-C
means sending SIGINT
to the process
Ctrl-\
means SIGQUIT
.
you can also use kill -TERM <PID>
to send SIGTERM
to kill this command.
Ctrl-Z
will send SIGTSTP
(signal terminal stop) ,which will pause the process. you can use bg
or fg
to continue the job in back/foreground.
jobs
1 |
|
terminal multiplexer(like tmux)
tmux:
Sessions
- window
- pane
Sessions
- a session is an independent workspace with one or more windows
tmux
starts a new session.tmux new -s NAME
starts it with that name.tmux ls
lists the current sessions- Within
tmux
typing<C-b> d
detaches the current session tmux a
attaches the last session. You can use-t
flag to specify which
Windows
- Equivalent to tabs in editors or browsers, they are visually separate parts of the same session
<C-b> c
Creates a new window. To close it you can just terminate the shells doing<C-d>
<C-b> N
Go to the N th window. Note they are numbered<C-b> p
Goes to the previous window<C-b> n
Goes to the next window<C-b> ,
Rename the current window<C-b> w
List current windows
Panes
- Like vim splits, panes let you have multiple shells in the same visual display.
<C-b> "
Split the current pane horizontally<C-b> %
Split the current pane vertically<C-b> <direction>
Move to the pane in the specified direction. Direction here means arrow keys.<C-b> z
Toggle zoom for the current pane<C-b> [
Start scrollback. You can then press<space>
to start a selection and<enter>
to copy that selection.<C-b> <space>
Cycle through pane arrangements.
https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/#fnref:2
https://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/
alias(useful for mapping)
1 |
|
1 |
|
dotfiles&configuration
(you can also search dotfiles
on github to get configurations)
there also other tools called symlink,whose concept means that you can git all your dotfiles in a folder, and the commands will point to those dotfiles like a trick.in this way, you make it possible to keep every dotfiles neat.
remote machines
SSH
SSH keys
Frameworks
Lecture 6 Version Control(git)
before i learn this lesson,i master how to switch between tabs in chrome,using ctrl 1
orctrl tab
.
tree blobs
merge conflicts
notes from class notes
Snapshots trees blobs
1 |
|
Commit merge branch
1 |
|
Data Model
1 |
|
Object, and content-addressing(using SHA-1 hash)
1 |
|
References
1 |
|
then, repository is the combination of objects and references.
Basics
git help <command>
: get help for a git commandgit init
: creates a new git repo, with data stored in the.git
directorygit status
: tells you what’s going ongit add <filename>
: adds files to staging areagit commit
: creates a new commit
- Write good commit messages!
- Even more reasons to write good commit messages!
git commit -a
:automatically add untracked files and commitgit log
: shows a flattened log of historygit log --all --graph --decorate
: visualizes history as a DAGgit diff <filename>
: show changes you made relative to the staging areagit diff <revision> <filename>
: shows differences in a file between snapshotsgit checkout <revision>
: updates HEAD and current branchgit mv file_from file_to
: rename files in git
Branching and merging
git branch
: shows branchesgit branch <name>
: creates a branch```plaintext
git checkout -b1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
: creates a branch and switches to it
- same as `git branch <name>; git checkout <name>`
- `git merge <revision>`: merges into current branch
- `git mergetool`: use a fancy tool to help resolve merge conflicts
- `git rebase`: rebase set of patches onto a new base
## Remotes
- `git remote`: list remotes
- `git remote add <name> <url>`: add a remote
- `git push <remote> <local branch>:<remote branch>`: send objects to remote, and update remote reference
- `git branch --set-upstream-to=<remote>/<remote branch>`: set up correspondence between local and remote branch
- `git fetch`: retrieve objects/references from a remote
- `git pull`: same as `git fetch; git merge`
- `git clone`: download repository from remote
## Undo
- `git commit --amend`: edit a commit’s contents/message
- `git reset HEAD <file>`: unstage a file
- `git checkout -- <file>`: discard changes
## Advanced Git
- `git config`: Git is [highly customizable](https://git-scm.com/docs/git-config)
- `git clone --depth=1`: shallow clone, without entire version history
- `git add -p`: interactive staging
- `git rebase -i`: interactive rebasing
- `git blame`: show who last edited which line
- `git stash`: temporarily remove modifications to working directory
- `git bisect`: binary search history (e.g. for regressions)
- `.gitignore`: [specify](https://git-scm.com/docs/gitignore) intentionally untracked files to ignore
## .gitignore
```text
# #表示注释 所有#和空行都会忽略
# 遵循简单的globing原则
# 忽略所有的 .a 文件
*.a
# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
# !表示否
!lib.a
# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
/TODO
# 忽略任何目录下名为 build 的文件夹
build/
# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt
# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf
除此以外,可以在GitHub上找到别的gitignore文件。
Lecture 7 Debugging and Profiling
https://askubuntu.com/questions/29284/how-do-i-mount-shared-folders-in-ubuntu-using-vmware-tools
1 |
|
logging
debugging
python -m ipdb myprogram.py
different static analyzer
flame graph
you don’t need to understand everything here, and even only be aware of these tools is useful enough.
这节课实在是太难了..挑了些自己觉得现阶段能搞定的。
- shell check
- pdb调试器 for python
- 静态检查工具/自动补全
Lecture 8 Metaprogramming
a build system
make(how to use make from .dat to .png and finally to .pdf)
1 |
|
repository
semantic versioning(avoiding the version conflicts.for example,the function in older versions may be deleted in newer ones.)
8.1.7
major version.minor version.patch version.
incompatible change.compatible adds or changes.bug fixes
(ex.Python2 vs Python 3.5 vs Python 3.8)
therefore,the lower the version is, maybe the better the compatibility is.
lock file
(lock some key files to avoid conflicts)
A lock file is simply a file that lists the exact version you are currently depending on of each dependency. Usually, you need to explicitly run an update program to upgrade to newer versions of your dependencies. There are many reasons for this, such as avoiding unnecessary recompiles, having reproducible builds, or not automatically updating to the latest version (which may be broken). An extreme version of this kind of dependency locking is vendoring, which is where you copy all the code of your dependencies into your own project. That gives you total control over any changes to it, and lets you introduce your own changes to it, but also means you have to explicitly pull in any updates from the upstream maintainers over time.
continuous intergration
(有点像自动化?)
(example: github pages(also where my blog from))
mocking
从作业中学到的:
xargs:
1
echo hello | xargs echo # hellp transfer from input to arguments
git ls-files --others
# files apart from the repositoryPhony: not really a target need to construct,but a command or a request. Examples are like
clean
:1
2
3.PHONY: clean
clean:
rm *.o tempOnce this is done, ‘make clean’ will run the recipe regardless of whether there is a file named clean.
Lecture 9 Security and Cryptography
太累了..看不动了..
entropy
useful when determining the strength of a password.
entropy = log_2(# of possibilities)
hash function
sha1(bytes)->160 bits
1 |
|
some demands:
- non-inverted: hard to get input from output
- collision resistant: hard to find two inputs having the same output
- Target collision resistant: given an input
m_1
, it’s hard to find a different inputm_2
such thathash(m_1) = hash(m_2)
. - Collision resistant: it’s hard to find two inputs
m_1
andm_2
such thathash(m_1) = hash(m_2)
(note that this is a strictly stronger property than target collision resistance).
applications
sha1 can also be used to test the integrity of large files downloaded from the Internet.
key derivation function (KDF)
it should be slow,for fear of brute-force attacks.
encryption decryption
symmetric cryptography
1 |
|
asymmetric cryptography
1 |
|
https://missing.csail.mit.edu/2020/security/
就这样吧,以后用到了再看。
Lecture 10 Potpourri
some miscellaneous items
keyboard remapping
the core concept is using keyboard mostly and with the optimal usage.
(for example,CapsLock
is a rarely used key,and you can use it to remap)
Some software resources to get started on the topic:
- macOS - karabiner-elements, skhd or BetterTouchTool
- Linux - xmodmap or Autokey
- Windows - Builtin in Control Panel, AutoHotkey or SharpKeys
- QMK - If your keyboard supports custom firmware you can use QMK to configure the hardware device itself so the remaps works for any machine you use the keyboard with.
daemon(programs running in the background)
(sshd->ssh daemon)
(systemd systemctl)
(cron->a daemon your system already runs to perform scheduled tasks.)
file system
FUSE
Some interesting examples of FUSE filesystems are:
- sshfs - Open locally remote files/folder through an SSH connection.
- rclone - Mount cloud storage services like Dropbox, GDrive, Amazon S3 or Google Cloud Storage and open data locally.
- gocryptfs - Encrypted overlay system. Files are stored encrypted but once the FS is mounted they appear as plaintext in the mountpoint.
- kbfs - Distributed filesystem with end-to-end encryption. You can have private, shared and public folders.
- borgbackup - Mount your deduplicated, compressed and encrypted backups for ease of browsing.
encrypted system
backups
google drive/RAID is not really backup(sync is not backup)
offline hardware
APIs
curl
some useful common arguments
--help
--version
--verbose
->print more detailed info
--silent
->only print when having errors
-i
->interactive mode,in which the shell will inform you every time you have a dry run(where the command couldn’t undo)
-r
->recursive flag,especially useful in destructive commands like rm
or cp
however,sometimes we meet with the situation,where the filenames are the same as the arguments, like -i
.the solution is, using --
:
1 |
|
In many tools, -
in place of a file name means “standard input” or “standard output”, depending on the argument.
windows manager
using keyboard instead of mouse
VPN
danger: being tracked,or don’t encrypted
Markdown
good for learning
hammerspoon
(a powerful desktop automation framework for MacOS)
boot up in a live USB
virtual machine/Docker
notebook programming environment
https://www.wolfram.com/mathematica/
适用于交互性的开发,比如有利于小的函数的debug和测试
Github
issue & pull request
Lecture 11 Q&A
using keyboard more ,using mouse less.
reduce repetitive operations