Lecture 1 Course Overview

课程网址:https://missing-semester-cn.github.io/2020/course-shell/

内容概览

  • Linux和shell基本知识

    • Linux文件系统(目录文件)
    • shell与环境变量
  • Linux常用指令

    • echo
    • man command 查看指令command的操作手册
    • control + L 清空终端并且回到顶部
    • 目录操作:
      cd
      ls 查看目录内容
      pwd 打印当前绝对路径(从环境变量查找)
      cp 复制文件或目录
      mv 移动文件
      rm 删除文件
      mkdir 创建目录
      rmdir 删除目录
    • 文件操作:
      cat 从头打印文件内容
      tail 只打印尾几行
      head
      tac 从最后一行开始打印
    • 输入输出重定向
      command > file 输出重定向到file
      command < file 输入重定向到file
      command >> file 将输出以追加方式重定向到file
      pipe:command1 | command2 以左侧的输出作为右侧的输入
      
  • 关于读写权限

    sudo
    the super user
    sudo su
    xdg-open 文件名.后缀 打开文件

一、Linux 和 shell 基本知识

很多都是以终端为中心的应用程序,例如:…,这些都存储在你的文件系统,而shell 可以利用一种搜索方式去帮助确定这些程序在哪里,它通过一个叫做环境变量的方式来做这件事 (it does this through something called an invariant environment variable)。

因此 shell 特别是 bourne-again shell 事实上是一种编程语言,用于提示你在某个位置是否可以运行一个程序。

Particular environment variable 特定环境变量

环境变量是启动 shell 程序时,不需要关注的一些变量,包括主目录(home directory)、用户名(username)、路径变量(path variable)等等。

二、Linux常用指令

echo 命令

显示机器上所有的 shell 将用于搜索程序的路径列表:

1
$ echo $PATH

当我们要运行一个程序,比如说显示当前的日期 date,shell 会遍历所有这些目录直到找到一个文件或程序名字为 date 或echo。

1
$ echo `date`

补充:清空文件内容的指令

1
2
3
4
$ > filename
$ echo /dev/null > filename
$ cp /dev/null filename
$ echo > filename

https://linux.cn/article-8024-1.html

如果我们想知道我们运行的是什么程序,有一个命令 called “which“ lets us do。

1
$ which echo

路径就是:a way to name the location of files on your computer.

/:top of the file system

windows: 反斜杠 \,mac OS、Linux: 斜杠 /


1. cd 命令

1.1 相对路径(relative path)

相对路径是相对于当前工作目录。打印当前工作目录(present working director)

1
$ pwd

相对路径是相对于当前工作目录,因此也可以改变当前工作目录:

1
$ cd /... 

1.2 几个特殊的目录

1.2.1 dot dot: $ cd .. 转到当前目录的父目录
1.2.1 dot :$cd ./ 转到当前目录的子目录

$ cd . /Cwill cd into the home directory under the current directory

because there is no home directory under the current directory I am now on which I change by doing the right cd.

1.3 去到 home 目录 to home directory

1
$ cd ~ 

注意此指令: cd ~回到/home/jinstoz目录下!!!

也可以使用相对路径:

1.4 去到刚刚的目录 back to the previous directory

1
$ cd -

所以这个命令经常用于在两个不同的目录之间来回切换。

2. ls 命令

2.1 显示当前目录下的所有文件 files in the current directory

1
$ ls

2.2 显示上一级目录的所有文件

1
$ ls .. 

2.3 显示帮助文档

1
$ ls -- help

2.4 显示具体文件列表

1
$ ls -l

前缀带 Dd 代表这是一个目录而不是文件 (D at the beginning of some of the these entries indicate that something is a directory):

无法cd或者打开指定文件或者目录是因为权限的缘故。

  • the first group of the three characters: the owner of these file (jinstoz)

  • the second group of the three characters: the permissions for the group that owns this file (jinstoz group)

  • the final group of the three characters: the permissions for everyone else so anyone who’s not a user owner or a group owner

3. mv 命令(移动文件,兼具修改文件名)

(move 移动文件,给出现在路径和目标路径,可以改变location and its name) 可参考:https://www.runoob.com/linux/linux-comm-mv.html

1
$ mv textfile.txt fool.txt	# 给 textfile.txt 重命名为 fool.txt

4. cp 命令(复制文件,兼具修改文件名)

(two arguments:复制的路径和复制去的路径)

1
$ cp textfile.txt ../foo.txt	# 将 textfile.txt 复制到父目录并且重命名为 foo

5. rm 命令(删除文件remove)

rm 只能删除文件,因为不具有递归删除目录下文件的功能(所以不能删除目录)。

in Linux, default not recursive:Linux下默认不是递归的

6. rmdir 命令(删除空目录

要求空目录的原因:empty:to be a safety mechanism 这是一种安全机制,防止误删。

7. mkdir 命令(创建新目录)

对于 Linux 命令,要多注意字符串中的空格:

1
$ mkdir My Photos 	# 这样会创建两个目录,one My,one Photos
1
$ mkdir "My Photos" 	# 这样就对了,转义空格或引用字符串

8. man 命令

give the manual page of another programme,举例:

1
$ man ls	# 可以获得类似ls -help的结果,但是更加清晰,方便阅读,而且末尾通常有范例。

9. ctrl + L

清除终端并且返回顶部。


Things above are talked about isolated programs. The followings are about chaining files together.

Input stream:keyboard (default)

Output stream:terminal (default)


10. < > 输入输出重定向

The shell gives a way to rewire these streams, to change where the input or output programs are pointed by using the bracket signs < and >.

1
2
3
4
$ # 输入输出流重定向
$ < file
$ > file
$ # 左尖括号表示重新连接输入流到 file,右尖括号表示重新连接输出到此文件 Rewire the output of the preceding program into this file

举例:

1
2
$ echo hello > hello.txt 
$ # (这里使用的是相对路径,将会在当前目录下创建一个txt文件,文件名为 hello 并且文件内容为hello,此时输出被重新连接到hello.txt文件,故终端(default)上不会有输出)

11. cat 命令(打印文件内容)

cat 也可以输入输出重定向:

11.1 cat < hello.txt

11.2 cat < hello.txt > hello2.txt

输入输出流双向 rewired:shell 通过 cat <hello.txt 获取文本内容并且通过 > 将内容打印到 hello2.txt 中。(a copy of the original file)

12. >> 追加

将一个文件的内容追加写入另外一个文件,举例:

1
$ cat < hello.txt > hello2.txt	# 覆盖
1
$ cat < hello.txt >> hello2.txt	 # 追加

13. | (pipe)

使得程序从左侧的输出,作为右侧的输入。举例:

1
$ ls -l /	 # 显示根目录下的所有文件

如果只是想打印输出的最后一行:

14. tail

打印输出的最后一行

1
$ tail -n1

举例:tail 和 | (pipe) 结合起来就是:

1
$  ls -l / | tail -n1	# ls 的输出作为 tail 的输入并且将 tail 的结果输出到 terminal(default)

注意:需要进行输入输出重定向的两个程序本身并不知道互相之间的关系(tail did not know about ls, ls did not know about tail),是 shell 设置了输入输出的重定向。

重定向 tail 的输出之后:

1
$ ls -l / | tail -n1 > ls.txt

| pipe 也可以用于图像、视频(chromecast)

三、读写权限

l . 文件 file 和目录 directory 的权限

1.1 文件权限

三种权限:read write execute(r-w-x)

    r-x:表示已经读和运行,但是并没有权限只有读的权限没有写的权限(https://programskills.blog.csdn.net/article/details/105023305)

执行: $ ls -l /usr/bin

结果:大多数都 have the execute bit set even for people who are not the owner of the file,因为例如 echo 这样的文件,系统希望所有人可以运行(尽管不能读或写)。

1.2 目录权限

  • 读(Read):能否看到目录下文件的权限。

  • 写(Write)
    whether allowed to rename(重命名) create(创建) or remove(移动) files in that directory。(也就是说,如果对于文件有write right,但是对于文件所在目录没有write right,那么只能清空这个文件的内容,而不能删除它——因为这需要对目录本身做写操作)

  • 执行(execute)
    就是所谓的搜索。如果需要对某一个文件有读、写、运行的权限,也就是cd into a directory,那么必须拥有这个文件所在目录的所有父目录的execute(执行)权限,以及这个目录本身的execute permission。

举例:
比如要运行 echo 程序(/root/usr/bin),那么必须有 /root/usr/bin 每层目录的全部运行权限,否则 will not be able to enter the directory along the way。

2. sudo 和 the super user

This is something about how to use terminal more powerful~~.

2.1 notion:the root user

  • Just like the administrator users on Windows and its user ID is 0.

  • Allow the root user do anything on the system.

但是大多数情况下,我们一般使用其他身份(比如我以jinstoz的身份),因为如果一旦不小心运行了错误的指令或程序,可能会毁了这个系统。

但总有需要root user的操作,于是有了 sudo(do the following things as the super user)。

2.2 sudo 命令

1
$ cd /sys	# 内核参数

这些文件实际上不是我们电脑上的文件,这些是各种各样的内核参数(kernel parameters),只是以文件系统的形式展现出来。

下面看看class里的东西:$ cd class

这些是可以互动的设备类型(types of devices that i can interact with),或可以访问的所有线索等等。

再次注意:

需要进行输入输出重定向的两个程序本身并不知道互相之间的关系(tail did not know about ls, ls did not know about tail),是 shell 设置了输入输出的重定向。

所以:

1
2
$ sudo echo 500 > brightness
$ # 这里只是 shell 以 root 权限使用 echo 程序(参数是 echo 和 500,并且将其输出送到 brightness),但是,是 echo 程序打开 brightness 并且将 500 写入而不是 sudo 程序。所以这里是我(user:jinstoz)尝试打开 brightness 并且写入,这里会出现权限下降permission down error的问题。

2.3 sudo su 可以让 shell 成为 the super user

变化:jinstoz -> root, $ -> #

1
2
# sudo echo 500 > brightness  # 此时这条指令不会出现权限下降
# exit # 退出 root 权限

不过不一定要获取 root 权限(因为使用root权限比较危险),还可以使用:

1
$ echo 1060 | sudo tee brightness

补充:

xdg-open 文件名

打开目录下的任意格式文件

1
$ xdg-open 'Missing Semester'.docx

这里打开的文件名有空格,要用单引号括起来。

https://blog.csdn.net/liweiminlining/article/details/51497866

https://blog.csdn.net/u010853580/article/details/43406005

附:Bash quoting手册:

https://www.gnu.org/software/bash/manual/html_node/Quoting.html