MIT-The-Missing-Semester-Studynote

课程总结#

前后总共花费了半个月的时间(或许更久一些,但总之是先看了几节他人编写的本地化内容)上完了这门总课时大约 13 小时的课。

参考资料:

大约先回顾一下这门课程讲了什么内容:

  1. 对 Shell 做了一些介绍,了解了 Shell 的基本指令。
  2. 了解了一些 Shell Tools and Scrpting,脚本逻辑云云。
  3. 对 Vim 文本编辑器的介绍。
  4. Shell 中的数据处理、管道化输出。
  5. 介绍 Linux 中一些有关提高效率的工具。
  6. 介绍 Git 版本控制工具的使用方法。
  7. 调试和事件分析,分别以 Python 和 perf 举例。
  8. 构建、版本号逻辑、代码自动化发布。
  9. 对密码学、Hash 函数、常用加密方案的介绍。
  10. 杂项,主要是键盘映射、备份、VPN、Markdown、VMs、GitHub。
  11. Q&A,对前面章节的问题解答。

很遗憾,因为比较懒,这门课程的 Lecture 4 之后的作业都没有完成,只是看了中文版笔记和讲座视频。课程中的一些东西是我以前便有使用过的,比如在 Python 中使用 printf 调试、使用 Git 进行版本控制、Hash 函数、Markdown、GitHub,不过或多或少在之前的基础上有了一些深入的了解。

  • 比如以前使用 Git 时只会对 VS Code 自带的插件狠狠地傻瓜式操作,现在高低了解一些指令了;
  • 再比如以前写 Markdown 的时候格式总是不大规范,也因为这个问题被一些框架前端的渲染逻辑坑过好几次。在被 @岚 同学介绍了 markdown-lint 插件之后写得更规范了,不过使得我现在看到黄色划线就难受,看到以前写的 Markdown 文档中满屏的黄色划线就想直接重构。

同时也对一些以前不熟悉的东西有了一些大致的了解。

  • 比如 Vim,以前使用 Linux 服务器时基本不懂得这个怎么使用,于是遇到一些什么问题都去搜百度。到后面也就记得个 i 和 :wq,当时觉得这个工具非常难用,不知道怎么用得下去的。现在对 Vim 有了一些了解,学会了 hlkj 之类基本的操作。虽然感觉还是很难用(上手难度太高了..,日常使用不习惯。
  • 再比如键盘映射。感觉一个什么东西不好操作、比较麻烦时先去找找有没有对应的快捷键,这是一个好习惯。不过目前我还没养成。希望之后可以慢慢习惯吧。(说起来刚刚在想怎么在 VS Code 中快速跳转到行尾,Google 一搜发现是 End 键,但是用的 75 配列的键盘没有 End。好在键盘厂商提供的软件可以重载。
  • 版本号逻辑。以前写点自娱自乐的东西,版本号都是随便加的,经过这门课才了解到一些区别。aa.bb.cc,在正常迭代时 cc++ ,在更新一些功能但依然可以向下兼容时 bb++,在更新一些没办法向下兼容的功能时 aa++。所以 Python3 可能在近十来年内都不会更新到 Python4。

其实这门课要说真正系统化地讲了些什么嘛倒也没有,但是学到了很多之前没有接触过的、别样的内容。不过应该短时间内我也不会真正将 Linux 作为自己日用的系统了,之后主要就是先掌握一些切实提高自己的效率的快捷键吧。

总之,完结撒花。

Lecture 1 - The Shell#

  • cd ~: change work directory to users’ home.
  • cd -: change work directory to last work directory.
  • pwd: print work directory, print the work directory now.
  • ls -l: show file list and info in work directory, “rwx” means read, right, execute (for owner, group and others), “t/T” is sticky bit only shows in others, which can stop user from removing or renaming the file.
  • mv [origin_filename] [new_filename]: move file or rename file.
  • Ctrl+L: clear screen.
  • cat: means concatenate
    • cat [filename]: print file’s text to screen
    • cat [filename1] [filename2]: concatenate file’s text
  • <: input text
  • >: output text
  • <<: add text
  • |: pipe. set a command’s stdout as another command’s stdin.
  • tail: show the end of file.
    • tail -f: follow the file’s changes.
    • tail -n [num]: show the endly num lines of file.
  • $ dollar means user, # means root.
  • tee: read bytes from stdin and output to stdout.
    • example: ls -l | tee output.log

Lecture 2 - Shell Tools and Scripting#

  • Shell just like a programming language. Blankspace is important in shell.
  • $foo = bar: set a variable.
  • “”: string in here will be formatted. “Value is $foo”
  • mcd (){
    mkdir -p “$1”
    }

  • 判断逻辑与 Python 相似。
  • “$(pwd)” in string: execute the “pwd” as command, then print the output in string.
  • “$@” in string: use all parameters which sent to script.

Lecture 3 - Editors (vim)#

Different modes

  • normal mode(esc, view things)
  • insert mode(i, edit things)
  • replace mode(r key)
  • visual mode(v key, move cursor and select things as block)
  • command line mode(: key)

start vim

  • vim
  • vim filename

keys

  • h(move left), l(move right), j(move down), k(move up)
  • ^ [times] before here: do the same thing times repeatly.
  • e: move to the end of this word
  • 0: move to the begin of a line.
  • $: move to the end of a line.
  • U: goes up.
  • D: goes down.
  • G: move to the end of the file.
  • gg: move to the begin of the file.
  • L: move the cursor to the end line that’s shown on the screen.
  • M: move the cursor to the middle line that’s shown on the screen.
  • H: move the cursor to the middle line that’s shown on the screen.
  • f[char]: jump to next first char of the line.
  • F[char]: jump to last first char of the line.
  • r[char]: replace the char on cursor to char.
  • o: open a new line on cursor and turn into insert mode.
  • O: open a new line on cursor’s last line and turn into insert mode.
  • x: delete the char.
  • d(e,1,2,3,..)w: delete x words.
  • c(e,1,2,3,..)w: delete x words and turn into insert mode.
  • DD: delete the line.
  • CC: delete the line and turn into insert mode.
  • q(in command line mode): quit now tab. if no tab exists, quit vim.
  • w(in command line mode): save file.
  • wq(in command line mode): save file and quit vim.
  • help [key](in command line mode): open help manual about the key.
  • tabnew(in command line mode): open a new tab.
  • u: undo last change.
  • r: redo last change.
  • y: copy.
  • yw: copy the word on cursor.
  • yy: copy the line.
  • p: paste.
  • /[word] + Enter: search.
  • .:repear last command.

buffers, tabs and windows

Lecture 4 - Data Wrangling#

  • regular expressions
  • | grep [text]: filter something.
  • uniq -c: 去除相邻的重复行。
  • sort [-r]: 以行为单位排序输出结果(默认依照 ASCII 码排序)。
  • wc: word count. 统计指定文件中的字节数。
  • tail: 查看文件的末尾部分(默认显示后十行)。
  • awk: 处理结构化文本。
  • paste: 将多个文件的内容按列合并在一起。
  • bc: basic calculator 数学计算器。
  • gnuplot: 命令行绘图程序。
  • xargs: 将标准输入转换成命令行参数, 然后传递给另一个命令执行。
  • sed: stream editor. 逐行读取文本流,以给定规则对每一行进行编辑,输出编辑后的结果。 s(substitute)/要查找的内容/要替换成的内容/g(global)

Lecture 5 - Command-line Environment#

  • Ctrl+C: send a sigint to program.
  • Ctrl+: send a sigquit to program.
  • nohup: 程序在后台开始运行。
  • jobs: 查看当前进行的工作。
  • kill %[num]: 停止指定进程。

tmux

  • tmux a: 重连最后分离的会话
  • tmux a -t [name]: 连接指定会话
  • tmux new -s [name]: 命名新会话
  • tmux: 启动新会话
  • Ctrl+B D: 分离当前会话

  • Ctrl+B 0-9: 跳转到第n个窗口
  • Ctrl+B ,: 重命名当前窗口
  • Ctrl+B c: 创建新窗口
  • Ctrl+B &: 关闭当前窗口

  • Ctrl+B %/“: 垂直/水平分割窗格
  • Ctrl+B 方向键: 移动到指定方向窗格
  • Ctrl+B x: 关闭当前窗格
  • Ctrl+B [: 进入复制模式(类似vim)
  • Ctrl+B ]: 粘贴复制的内容

alias

  • alias alias_name=”command_to_alias arg1 arg2”
  • 临时忽略一个别名 \ls
  • unalias alias_name: 完全禁用一个别名
  • alias alias_name: 查看别名定义
  • alias: 列出已定义的别名
  • 若需要别名持久化,需要将其添加到Shell的启动配置文件中

ssh

  • ssh username@hostname
  • ssh username@hostname command
  • ssh端口转发: ssh -L 9999:localhost:8888 username@hostname

Lecture 6 - Version Control#

Git

  • git init: 在当前目录创建一个新的 Git 仓库。
  • git status: 显示当前工作目录和暂存区的状态。
  • git add [filename]: 将指定文件的当前更改添加到暂存区。
  • git commit: 根据暂存区的内容创建一个新的提交。
  • git log: 显示当前分支的提交历史记录。
  • git diff (hash) (branch) [filename]: 显示当前目录中相对于暂存区的更改。
  • git checkout [branch]: 更新 HEAD 和工作目录以匹配指定的提交或分支。
  • git cat-file -p [hash]: 查看提交信息/目录树/文件内容

Git 的分支与合并

  • git branch: 列出所有本地分支。(-a 参数表示包括远程分支)
  • git branch (branch_name): 创建一个新分支。
  • git checkout -b [name]: 创建新分支并立即切换到该分支。
  • git merge [name]: 将指定分支合并到当前所在分支。
  • git rebase [base_branch]: 将当前分支上自从与 base_branch 分叉之后的所有提交在 base_branch 的最新提交之上重新应用一遍。

Git 远程操作

  • git remote: 列出当前配置的所有远程仓库的简称(-v 参数表示同时显示URL)。
  • git remote add [name] [url]: 添加一个新的远程仓库。
  • git push [remote] [local branch]:[remote branch]: 将本地的对象和引用推送到指定远程仓库。(-u 参数表示建立跟踪关系)
  • git clone [url]: 从指定 URL 下载一个完整的远程仓库副本到本地。
  • git fetch [remote_name]: 从指定的远程仓库下载所有本地没有的对象和引用。
  • git pull: 拉取远程更改并尝试合并到本地当前分支。

Git 撤销操作

  • git commit –amend: 修改上一次提交。
  • git reset HEAD [file]: 将文件从暂存区移除,但保留工作目录中的更改。
  • git checkout – [file]: 丢弃工作目录中对指定文件的未暂存更改。
  • git revert [commit_hash]: 创建一个新的逆提交(适用于撤销已经推送到公共分支的提交)。

Git 高级技巧

  • git config: 配置Git。
  • git clone –depth=1 [url]: 浅克隆,只下载最新代码。

Lecture 7 - Debugging and Profiling#

  • printf 调试
  • logging 记录(Python模块)
  • debuggers
    • 断点
    • 步进
    • 检查变量
    • 调用栈
    • 监视表达式
  • pdb 是 Python 内置的命令行调试器。(现代调用:在Python中输入 breakpoint();
  • ipdb: 较pdb更现代化, import ipdb;ipdb.set_trace();

linux 事件分析

  • perf: 自带的系统级性能分析工具

Lecture 8 - Metaprogramming#

  • Make
  • CMake、Gradle、Maven 等

Dependency Management

  • SemVer: 版本格式:主版本号.次版本号.修订号 (MAJOR.MINOR.PATCH)
    • 修订号 (PATCH): 做了向后兼容的 Bug 修复。
    • 次版本号 (MINOR): 添加了向后兼容的新功能。
    • 主版本号 (MAJOR): 做了不向后兼容的 API 修改。

Lock Files: 锁文件,记录项目当前使用的每一个依赖的确切版本号。

Continuous Integration: 持续集成,自动化代码变更后的流程(通过触发预定义的 workflow),如 GitHub Actions。

Lecture 9 - Security and Cryptography#

熵(Entropy)

Hash Functions

  • 理想的密码学哈希函数特性
    • 确定性 (Deterministic): 相同输入永远产生相同输出。
    • 抗原像攻击 (Preimage Resistant): 给定哈希值 h,难以找到输入 m 使得 hash(m) = h。(单向性)
    • 抗第二原像攻击 (Second Preimage Resistant): 给定输入 m1,难以找到另一个不同输入 m2 使得 hash(m1) = hash(m2)
    • 抗碰撞攻击 (Collision Resistant): 难以找到任意两个不同的输入 m1m2 使得 hash(m1) = hash(m2)。(这是最强的要求)
  • 对称加密: AES
  • 非对称加密: RSA

Lecture 10 - 杂项#

  • Keyboard Remapping
  • Daemons (eg. ssh, nginx)
  • FUSE
  • Backups 3-2-1
  • API - REST
  • Window Managers
  • VPN
  • Markdown (like this)
  • BIOS and Live USB
  • VMs
  • Notebook
  • GitHub

Lecture 11 - Q&A#

  • 问:有哪些工具是你认为应该优先学习的?

  • 答:以下是一些我们认为回报率很高的主题和工具:

    • 更高效地使用键盘: 学习快捷键、自定义环境、使用命令行工具,减少对鼠标的依赖。
    • 熟练掌握你的代码编辑器: 深入学习 Vim, Emacs, VS Code 或你选择的编辑器的功能、快捷键和插件。
    • 自动化重复性任务: 学习编写脚本、使用别名和任务运行器来简化工作流程。
    • 掌握版本控制 (Git) 与协作平台 (GitHub 等)。
  • 问:什么时候我应该用 Python,什么时候用 Bash 脚本,什么时候用其他语言?

  • 答:原则: 根据任务复杂度、性能要求、可维护性、团队熟悉度和生态系统支持来选择。

  • 问:source script.sh 和 ./script.sh 有什么区别?

  • 主要区别在于执行脚本命令的 Shell 会话环境不同:

    • source script.sh (或简写 . script.sh): 命令在当前 Shell 会话中执行。脚本对环境所做的任何更改(如设置变量、定义函数、更改目录)在脚本执行完毕后会保留在当前会话中。
    • ./script.sh (需要脚本有执行权限 chmod +x script.sh): 当前 Shell 会启动一个新的子 Shell 实例来执行脚本中的命令。子 Shell 对环境所做的更改(如 cd 或导出变量)在子 Shell 退出后不会影响父 Shell。