前端学习 linux —— 第一篇

前端学习 linux - 第一篇

本文主要介绍“linux 发行版本”、“cpu 架构”、“Linux 目录结构”、“vi 和 vim”、“用户管理”、“文件目录指令”、“docker 安装 ubuntu20.04”。

:笔者使用的是 ubuntu 20.04 桌面版。

linux 发行版本

linux 主要发行版有 ubuntu、redhat、centos等等,都是linux 内核,其他的可能就不同了。不同发行版的很多指令都类似,比如你学会了 ubuntu,在 centos 上也能快速上手。

unix 和 linux 关系

首先有 unix,但它的价格昂贵,不能运行于 x86 的 PC。

有个荷兰的教授写了一个叫 MINIX 的操作系统,向学生展示操作系统的内部原理,并将其代码开源。全世界学计算机的学生都通过钻研 MINIX 来操作系统,其中有一位芬兰大学学生,于 1991 年写出了 Linux 0.01。

Tip:更多介绍请看百度百科

cpu 架构

cpu 架构有 aarch64loongarch64mips64x86_64(也是AMD64

不同的架构可以简单理解为指令集不同。

Linux 目录结构

windows 分 C盘、D盘,而 linux 已经帮我们分好了,例如我们在 / 目录下查看文件信息:

pjl@pjl:/$ ls
bin  boot  cdrom  data  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  swapfile  sys  tmp  usr  var

每个目录的作用请看下文。

bin

/bin,存放最经常使用的命令。例如里面有 whoamisusudo等命令

pjl@pjl:/$ ll /bin
lrwxrwxrwx 1 root root 7 12月  7  2021 /bin -> usr/bin/

pjl@pjl:/$ ll usr/bin/ |grep 'whoami'
-rwxr-xr-x  1 root root       39256 9月   5  2019 whoami*

家目录

/home,存放普通用户的主目录。例如我增加一个用户 pjl,home目录下也会增加一个 pjl 的目录。请看示例:

pjl@pjl:/$ sudo adduser pjl
[sudo] pjl 的密码:
正在添加用户"pjl"...
正在添加新组"pjl" (1004)...
正在添加新用户"pjl" (1004) 到组"pjl"...
创建主目录"/home/pjl"...
正在从"/etc/skel"复制文件...
新的 密码:
重新输入新的 密码:
passwd:已成功更新密码
正在改变 pjl 的用户信息
请输入新值,或直接敲回车键以使用默认值
        全名 []:
        房间号码 []:
        工作电话 []:
        家庭电话 []:
        其它 []:

这些信息是否正确? [Y/n] pjl@pjl:/$

home 目录下增加用户 pjl 同名的家目录:

pjl@pjl:/$ ll /home
总用量 32
drwxr-xr-x  8 root     root     4096 6月   8 20:22 ./
drwxr-xr-x 23 root     root     4096 6月   8 10:48 ../
drwxr-xr-x  2 pjl      pjl      4096 6月   8 20:22 pjl/

root

/root,超级权限者的用户主目录。普通用户不能查看:

pjl@pjl:/$ ll /root
ls: 无法打开目录 '/root': 权限不够

切换 root 用户后查看:

pjl@pjl:/$ su
密码:
su: 认证失败
pjl@pjl:/$ su
密码:

root@pjl:/# ll /root
总用量 2012
drwx------ 28 root root    4096 6月   8 11:18 ./
drwxr-xr-x 23 root root    4096 6月   8 10:48 ../
drwxr-xr-x  2 root root    4096 6月   6 10:52 公共的/
drwxr-xr-x  2 root root    4096 6月   6 10:52 模板/
drwxr-xr-x  2 root root    4096 6月   6 10:52 视频/
drwxr-xr-x  2 root root    4096 6月   6 10:52 图片/
drwxr-xr-x  2 root root    4096 6月   6 10:52 文档/
drwxr-xr-x  2 root root    4096 6月   6 10:52 下载/
drwxr-xr-x  2 root root    4096 6月   6 10:52 音乐/
drwxr-xr-x  2 root root    4096 6月   6 10:52 桌面/
...

lib

/lib,系统开机所需要的最基本的动态链接共享库,类似 windows 中 DLL 文件。几乎所有应用程序都需要用到这些共享库

lost+found

/lost+found,通常为空,系统非法关机后,就存放一些文件

pjl@pjl:~$ ls /lost+found/
ls: 无法打开目录 '/lost+found/': 权限不够
pjl@pjl:~$ sudo ls /lost+found/
[sudo] pjl 的密码:

etc

/etc,系统管理所需的配置文件和子目录。例如 nginx、mysql、python、docker:

pjl@pjl:~$ ls /etc |grep -E 'mysql|nginx|docker|python'
docker
mysql
nginx
python2.7
python3
python3.8

nginx 的配置文件:

pjl@pjl:~$ ls /etc/nginx | grep '.conf'
fastcgi.conf
nginx.conf

usr

/usr,用户很多的应用程序和文件都在此,类似 windows 下的 Program Files:

pjl@pjl:~$ ls /usr
bin  games  include  lib  lib32  lib64  libexec  libx32  local  sbin  share  src

boot

/boot,存放启动 linux 时使用的一些核心文件。包括一些链接文件和镜像文件:

pjl@pjl:~$ ls /boot
config-5.13.0-41-generic  efi         initrd.img-5.13.0-41-generic  initrd.img.old  memtest86+_multiboot.bin      System.map-5.13.0-48-generic  vmlinuz-5.13.0-44-generic
config-5.13.0-44-generic  grub        initrd.img-5.13.0-44-generic  memtest86+.bin  System.map-5.13.0-41-generic  vmlinuz                       vmlinuz-5.13.0-48-generic
config-5.13.0-48-generic  initrd.img  initrd.img-5.13.0-48-generic  memtest86+.elf  System.map-5.13.0-44-generic  vmlinuz-5.13.0-41-generic     vmlinuz.old

proc

/proc,虚拟目录,它是系统内存的映射,访问这个目录获取系统信息

root@97342c74a502:/# ls /proc
1          cmdline    dma          ioports    kpagecgroup  modules       schedstat  thread-self  zoneinfo
10         config.gz  driver       irq        kpagecount   mounts        self       timer_list
160        consoles   execdomains  kallsyms   kpageflags   mtrr          softirqs   tty
acpi       cpuinfo    filesystems  kcore      loadavg      net           stat       uptime
buddyinfo  crypto     fs           key-users  locks        pagetypeinfo  swaps      version
bus        devices    interrupts   keys       meminfo      partitions    sys        vmallocinfo
cgroups    diskstats  iomem        kmsg       misc         sched_debug   sysvipc    vmstat

srv

/srv,service缩写,存放一些服务启动之后需要提取的数据

pjl@pjl:~$ ls /srv
ftp

sys

/sys,这是 linux 2.6 内核的一个很大的变化

pjl@pjl:~$ ls /sys
block  bus  class  dev  devices  firmware  fs  hypervisor  kernel  module  power

tmp

/tmp,存放一些临时文件

dev

/dev,类似 windows 的设备管理器,把所有硬件用文件存储。比如笔者 cpu 有 4 核

pjl@pjl:~$ ls /dev/cpu
0  1  2  3  microcode

media

/media,linux 会自动识别一些设备,例如 U 盘,识别后将会把该设备挂载到这个目录下。

mnt

/mnt,用于让用户临时挂载别的文件系统

opt

/opt,给主机额外安装软件的目录。例如安装数据库,默认为空。笔者安装了百度网盘

pjl@pjl:~$ ls /opt
baidunetdisk  google  sogoupinyin  teamviewer

usr/local

/usr/local,另一个给主机额外安装软件的目录。一般通过编译源码方式安装的程序

pjl@pjl:~$ ls /usr/local
bin  etc  games  include  lib  man  rvm  sbin  share  src

var

/var,存放不断扩充的东西,习惯将经常修改的目录放在这里,例如日志文件

pjl@pjl:~$ ls /var
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tmp  www

vi 和 vim

linux 会内置 vi 文本编辑器。vim 是增强版。

编辑器模式

编辑器有三种模式:

  • 正常模式
  • 插入模式
  • 命令行模式

Tip:通过 vim 默认进去的就是正常模式,退出(:q!)或保存退出(:wq)属于命令模式,通过输入 i 进入编辑模式。

模式切换:

  • 正常模式 输入 i 进入编辑模式
  • 编辑模式 输入 esc 进入正常模式
  • 正常模式 输入 :/ 进入命令模式
  • 命令模式 输入 esc 进入正常模式

Tip:退出有以下三种:

  • :wq 保存退出
  • :q 退出。有时退出失败,提示我们使用强制退出:E37: 已修改但尚未保存 (可用 ! 强制执行)
  • :q! 强制退出,不保存

快捷键

拷贝粘贴

复制一行 yy,复制 5 行 5yy,粘贴输入 p。需要在正常模式下进行。

在正常模式,光标在第一行,输入 yy,接着输入 p:

a
b
c
d
e

文件内容变为:

a
a
b
c
d
e
删除

删除当前行 dd,删除当前行向下3行,输入 3dd 即可。需在正常模式下进行。

文件中查找单词

正常模式输入 / 进入命令模式,输入要找的单词,例如 test,然后输入回车,接着输入 n 就能匹配下一个,到底了会提示“已查找到文件结尾,再从开头继续查找”,继续输入 n 就会从头开始。

如果需要找其他单词,输入 /,在输入其他单词例如 apple,如果要编辑找到的这个 apple,输入 i 进入编辑模式即可。

行号

命令行下输入 :set nu,显示行号。如下所示:

...
7 # the default umask is set in /etc/profile; for setting the umask
8 # for ssh logins, install and configure the libpam-umask package.
9 #umask 022

Tip:输入 :set nonu 隐藏行号

文件首行或末行

正常模式下,gg 首行,G 末行。

撤回

比如输入在某行输入 aa bb cc,想撤回,回到正常模式,输入 u 即可。对删除的某行(dd)等操作同样适用

切换到某行

一般模式下输入行号,例如 99,然后输入 shift+g 即可

用户管理

linux 是多用户,多任务的操作系统。

创建用户

通过 adduser 创建用户。请看示例:

root@pjl:/# adduser test11
正在添加用户"test11"...
正在添加新组"test11" (1007)...
正在添加新用户"test11" (1007) 到组"test11"...
创建主目录"/home/test11"...
正在从"/etc/skel"复制文件...
新的 密码:
重新输入新的 密码:
passwd:已成功更新密码
正在改变 test11 的用户信息
请输入新值,或直接敲回车键以使用默认值
        全名 []:
        房间号码 []:
        工作电话 []:
        家庭电话 []:
        其它 []:
这些信息是否正确? [Y/n]

家目录下已增加新增用户 test11:

root@pjl:/# ll /home
总用量 40
drwxr-xr-x 10 root     root     4096 6月  14 16:40 ./
drwxr-xr-x 23 root     root     4096 6月   8 10:48 ../
drwxr-xr-x  2 test11   test11   4096 6月  14 16:40 test11/

更改用户密码

通过 passwd 可以修改用户密码。请看示例:

test11@pjl:/$ passwd test11
更改 test11 的密码。
Current password:
新的 密码:
重新输入新的 密码:
passwd:已成功更新密码

Tip:远程工具通过 test11 登录成功后,默认会进入该用户的家目录:

test11@pjl:~$ pwd
/home/test11
test11@pjl:~$

切换用户

通过 su - 切换用户。例如切换到 root 用户:

test10@pjl:~$ su - root
密码:
root@pjl:~# su - test10
test10@pjl:~$ su - test11
密码:
su: 认证失败
test10@pjl:~$ su - test11
密码:

Tip:权限高的用户切换到权限低的无需输入密码。反之却需要输入密码。

删除用户

通过 userdel 可以删除用户。比如删除用户 test200,但保留家目录:

root@pjl:/home# userdel test200

root@pjl:/home# ls
test200 

通过 -r 删除用户,同时删除其家目录:

root@pjl:/home# ls
test201  vncuser

root@pjl:/home# userdel -r test201
userdel:test201 信件池 (/var/mail/test201) 未找到

test201 用户的家目录已删除:

root@pjl:/home# ls
vncuser

Tip:比如张三离职,一般情况保留其家目录,万一留有重要成果。更多介绍请通过 -h 查看:

root@pjl:/home# userdel -h
用法:userdel [选项] 登录名

选项:
  -f, --force                   即使不属于此用户,也强制删除文件
  -h, --help                    显示此帮助信息并退出
  -r, --remove                  删除主目录和信件池
  -R, --root CHROOT_DIR         chroot 到的目录
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
      --extrausers              Use the extra users database
  -Z, --selinux-user            为用户删除所有的 SELinux 用户映射

查询用户信息

通过 id 可查询用户信息。请看示例:

root@pjl:/home# id root
用户id=0(root) 组id=0(root) 组=0(root)
root@pjl:/home# id test10
用户id=1006(test10) 组id=1006(test10) 组=1006(test10)
root@pjl:/home# id test111
id: “test111”:无此用户

whoami,查询当前用户:

root@pjl:/home# whoami
root

Tip:通过 who am i 可以查看什么时间(2022-06-14 16:46),从哪台机器(192.168.1.225),什么用户(test11)登录这个 linux 终端。

root@pjl:/home# who am i
test11   pts/6        2022-06-14 16:46 (192.168.1.225)

用户组

用户组类似角色,方便管理。

增加组、删除组

增加组使用 groupadd,删除组使用 groupde。请看示例:

# 增加组 agroup
root@pjl:/home# groupadd agroup
# 删除组 agroup
root@pjl:/home# groupdel agroup
创建用户并指定所属组

创建用户 test30 并放入 agroup 组中:

root@pjl:/home# useradd -g agroup test30

test30 确实已经属于 agroup 组:

root@pjl:/home# id test30
用户id=1010(test30) 组id=1010(agroup) 组=1010(agroup)

:如果创建用户时不指定组,系统会创建一个与用户同名的组:

root@pjl:/home# adduser test40
root@pjl:/home# id test40
用户id=1011(test40) 组id=1011(test40) 组=1011(test40)
切换用户所属组

usermod 切换用户所属组。例如将 test40 放入 bgroup 组:

root@pjl:/home# groupadd bgroup
root@pjl:/home# usermod -g bgroup test40
root@pjl:/home# id test40
用户id=1011(test40) 组id=1012(bgroup) 组=1012(bgroup)
用户和组相关文件

用户和组相关的有三个文件:

  • 用户的配置文件
  • 用户的口令配置文件
  • 组的配置文件
/etc/passwd

用户的配置文件,记录用户的各种信息。请看示例:

test11@pjl:~$ cat /etc/passwd
// 用户名:口令:用户id:组id:注释性描述:家目录:使用的shell
test40:x:1011:1012:,,,:/home/test40:/bin/bash
...
/etc/shadow

口令配置文件:

root@pjl:/home/test11# cat /etc/shadow
// ! 这里是密码
// 19157 等信息都是处理过,看不懂的
test40:!:19157:0:99999:7:::

比如给 test40 设置密码(比如123456

root@pjl:/home/test11# passwd test40
新的 密码:
重新输入新的 密码:
passwd:已成功更新密码

再次查看 /etc/shadow,发现密码加密显示了:

test40:$6$JF.ed3d.S9RHucx.$13bXH9Ji3P8AGB2tsCnjRI8AFhp301F3HWzquzKCBTQWzS3..D6lvgKTsOLWuY1nwogtBcGdiZp5mGN69Ar7E.:19158:0:99999:7:::
/etc/group

组的配置文件:

root@pjl:/home/test11# cat /etc/group
bgroup:x:1012:

上面我们将 test40 用户放入 bgroup。这里的 1012 与用户配置文件中的组 id 相对应。

帮助指令

man

比如通过 man 查看 ls 命名。请看示例:

root@pjl:/home/test11# man ls
NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .
       ...

       -l     use a long listing format

点击空格能翻页,输入 q 就能退出:

Tip:笔者使用 mobaxterm 能通过鼠标向上向下滚动。

root@pjl:/home/test11#

ls 较常用的两个参数, a 显示所有文件文件。linux 中,隐藏文件以 . 开头:

root@pjl:/home/test11# ls
root@pjl:/home/test11# ls -a
.  ..  .bash_history  .bash_logout  .bashrc  .cache  .config  .local  .profile  .Xauthority

l 是以列表的形式显示信息。参数可以组合使用,不要求顺序:

root@pjl:/home/test11# ls -l
总用量 0

root@pjl:/home/test11# ls -la
总用量 40
drwxr-xr-x  5 test11 test11 4096 6月  15 18:55 .
drwxr-xr-x 12 root   root   4096 6月  14 17:21 ..
-rw-------  1 test11 test11   86 6月  14 17:27 .bash_history
-rw-r--r--  1 test11 test11  220 6月  14 16:40 .bash_logout
-rw-r--r--  1 test11 test11 3771 6月  14 16:40 .bashrc
drwx------  4 test11 test11 4096 6月  14 16:46 .cache
drwx------  4 test11 test11 4096 6月  14 16:46 .config
drwxr-xr-x  3 test11 test11 4096 6月  14 16:46 .local
-rw-r--r--  1 test11 test11  807 6月  14 16:40 .profile
-rw-------  1 test11 test11  108 6月  15 18:55 .Xauthority

help

help 能查看 shell 内置命令。例如 cd,请看示例:

root@pjl:/home/test11# help cd
cd: cd [-L|[-P [-e]] [-@]] [目录]
    改变 shell 工作目录。

    改变当前目录至 DIR 目录。默认的 DIR 目录是 shell 变量 HOME
    的值。

    变量 CDPATH 定义了含有 DIR 的目录的搜索路径,其中不同的目录名称由冒号 (:)分隔。
    一个空的目录名称表示当前目录。如果要切换到的 DIR 由斜杠 (/) 开头,则 CDPATH
    不会用上变量。

    如果路径找不到,并且 shell 选项 `cdable_vars' 被设定,则参数词被假定为一个
    变量名。如果该变量有值,则它的值被当作 DIR 目录。

    选项:
        -L      强制跟随符号链接: 在处理 `..' 之后解析 DIR 中的符号链接。
        -P      使用物理目录结构而不跟随符号链接: 在处理 `..' 之前解析 DIR 中的符号链接。
        -e      如果使用了 -P 参数,但不能成功确定当前工作目录时,返回非零的返回值。
        -@      在支持拓展属性的系统上,将一个有这些属性的文件当作有文件属性的目录。

    默认情况下跟随符号链接,如同指定 `-L'。
    `..' 使用移除向前相邻目录名成员直到 DIR 开始或一个斜杠的方式处理。

    退出状态:
    如果目录改变,或在使用 -P 选项时 $PWD 修改成功时返回 0,否则非零。

:man 查看 cd 失败;help 查看 ls 也失败。请看示例:

root@pjl:/home/test11# man cd
没有 cd 的手册页条目

root@pjl:/home/test11# help ls
bash: help: 没有与“ls”匹配的帮助主题。尝试使用“help help”、“man -k ls”或“info ls”。

pwd 用这两个帮助指令都可以。

info

如果觉得 ls --help 不详细,可以使用 info ls

root@97342c74a502:/# info ls

Next: dir invocation,  Up: Directory listing

10.1 'ls': List directory contents
==================================

The 'ls' program lists information about files (of any type, including
directories).  Options and file arguments can be intermixed arbitrarily,
as usual.
...

文件目录指令

pwd

pwd,查看当前目录所在的绝对路径。请看示例:

root@pjl:/home/test11# pwd
/home/test11

cd

cd ~ 回到家目录。请看示例

// 目前在 tmp
test11@pjl:~/tmp$ pwd
/home/test11/tmp

test11@pjl:~/tmp$ cd ~
// 已回到test11 用户的家目录

test11@pjl:~$ pwd
/home/test11

cd .. 返回上一层目录。

mkdir

mkdir,创建目录,默认创建一级目录,多级目录需要使用 -p 参数。例如创建 e、f、g 三个目录:

test11@pjl:/tmp$ mkdir -p e/f/g

test11@pjl:/tmp$ cd e/f/g/

test11@pjl:/tmp/e/f/g$

rmdir

rmdir,删除空目录。非空目录可以使用 rm -rf。请看示例:

test11@pjl:~/tmp$ rmdir e
rmdir: 删除 'e' 失败: 目录非空

test11@pjl:~/tmp$ rm -rf e

通过 rm --help 知道:r 表示递归,f 指强制删除

rm 也能删除文件,或同时删除非空目录 a 和 a.txt 文件。请看示例:

test11@pjl:~/tmp$ rm -rf a a.txt

touch

touch 创建一个空文件。

test11@pjl:~/tmp$ touch b.txt

test11@pjl:~/tmp$ ls
b.txt

cp

cp 拷贝文件或目录。例如将 b.txt 拷贝到 文件夹 c 中:

# 创建多级目录
test11@pjl:~/tmp$ mkdir -p a/b/c

# 将 b.txt 拷贝到文件夹 c 中
test11@pjl:~/tmp$ cp b.txt a/b/c

test11@pjl:~/tmp$ ls a/b/c
b.txt

通过参数 -r 将非空目录 a 整个拷贝到上层目录。请看示例:

# 拷贝非空目录失败
test11@pjl:~/tmp$ cp a ../
cp: 未指定 -r;略过目录'a'

test11@pjl:~/tmp$ cp a -r ../

test11@pjl:~/tmp$ ls ../
a  tmp

多次执行拷贝,未提示是否需要覆盖。

test11@pjl:~/tmp$ cp a -r ../

Tip:笔者是ubuntu20.04,并且也修改了 a/b/c/b.txt 这个文件。

mv

mv 移动目录或文件,也可以重命名。请看示例:

test11@pjl:~$ ls
a  tmp

# 将 a 重命名为 a2
test11@pjl:~$ mv a a2
test11@pjl:~$ ls
a2  tmp

移动并重新命名。将 b.txt 移动到上一层目录,并重命名:

test11@pjl:~/tmp$ mv b.txt ../b-rename.txt
test11@pjl:~/tmp$ ls ../
b-rename.txt  tmp

移动 a 文件夹,并重命名:

test11@pjl:~/tmp$ mv a ../a-rename
test11@pjl:~/tmp$ cd ../a-rename/b

# 检验:a中有b
test11@pjl:~/a-rename/b$

cat

cat 查看文件内容。只读,更安全。请看示例:

# n 显示行号
test11@pjl:~/a-rename/b$ cat -n /etc/profile
    ...
    10      fi
    11    else
    12      if [ "`id -u`" -eq 0 ]; then
    13        PS1='# '
    14      else
    15        PS1='$ '
    16      fi
    17    fi

可配合 more 使用:

test11@pjl:~/a-rename/b$ cat -n /etc/profile | more
     1  # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
     2  # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
     3
     4  if [ "${PS1-}" ]; then
     5    if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
     6      # The file bash.bashrc already sets the default PS1.
     7      # PS1='\h:\w\$ '
     8      if [ -f /etc/bash.bashrc ]; then
     9        . /etc/bash.bashrc
    10      fi
    11    else
    12      if [ "`id -u`" -eq 0 ]; then
    13        PS1='# '
    14      else
    15        PS1='$ '
    16      fi
    17    fi
    18  fi
    19
    20  if [ -d /etc/profile.d ]; then
    21    for i in /etc/profile.d/*.sh; do
    22      if [ -r $i ]; then
    23        . $i
--更多--

more

more,适合屏幕查看的文件阅读输出工具。基于 vi 编辑器的文本过滤器,以全屏幕的方式显示文件内容。可以配合管道使用,也可以单独使用。

例如查看文件:

test11@pjl:~/a-rename/b$ more  /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
--更多--(77%)

内置许多快捷键:

  • q - 退出 more
  • 空格 - 向下翻页
  • 回车 - 向下一行
  • ctrl + f - 向下一屏
  • ctrl + b - 向上一屏
  • = - 输出当前行号
  • :f - 输出文件名和当前行号

less

less,功能与 more 类似,功能比 more 更强大。less 显示文件内容时,不是一次将整个文件加载之后才显示,对显示大型文件有较好的效率。

内置许多快捷键:

  • q - 退出
  • 空格 - 向下翻页
  • page down - 向下翻页
  • page up - 向上翻页
  • /字符串 - 向下搜索字符串。n 向下查找;N 向上查找。

echo

echo,输出内容到控制台。请看示例:

# 输出字符串
root@97342c74a502:/# echo 'hello world'
hello world

# 输出内置变量
root@97342c74a502:/# echo $SHELL
/bin/bash

head,用于显示文件开头部分,默认前 10 行。请看示例:

test11@pjl:~/a-rename/b$ head /etc/profile
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi

假如只要显示前 5 行,可以使用参数 n。请看示例:

test11@pjl:~/a-rename/b$ head /etc/profile -n 5
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then

tail

tail, 与 head 对应,默认显示文件末尾 10 行。也能指定显示末尾 5 行。

test11@pjl:~/a-rename/b$ tail /etc/profile
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

export ANDROID_HOME=/home/pjl/software/android-studio-2021.1.1.22-linux/android-studio/bin
export PATH=$PATH:$ANDROID_HOME

tail -f filename ,随文件增长即时输出新增数据,也就是能监听文件。请看示例:

# 新建文件 a.txt
test11@pjl:~/a-rename/b$ touch a.txt

# 监听文件输出。此刻终端不会结束,一直监听着
test11@pjl:~/a-rename/b$ tail -f a.txt

另开一个终端,将字符串(apple)输出(或重定向)到 a.txt 文件中:

test11@pjl:~/a-rename/b$ echo 'apple'  > a.txt

立刻监听到并输出:

test11@pjl:~/a-rename/b$ tail -f a.txt
apple

> 和 >>

> 输出重定向。例如将 pwd 结果输出到 a.txt,这是一个覆盖操作:

test11@pjl:~/tmp$ touch a.txt
test11@pjl:~/tmp$ pwd > a.txt
test11@pjl:~/tmp$ cat a.txt
/home/test11/tmp
test11@pjl:~/tmp$ ls > a.txt
test11@pjl:~/tmp$ cat a.txt
a.txt

>> 则是追加。例如将 pwd 的结果追加到 a.txt 文件中:

test11@pjl:~/tmp$ pwd >> a.txt
test11@pjl:~/tmp$ cat a.txt
a.txt
/home/test11/tmp

ln

ln -s 创建软链接,类似 windows 下快捷键。

语法:ln -s 文件或目录 软连接名

例如给 a 创建一个软链接 ln-a:

test11@pjl:~/tmp$ ln -s a ln-a

test11@pjl:~/tmp$ ll
总用量 16
drwxrwxr-x 3 test11 test11 4096 6月  17 14:22 ./
drwxr-xr-x 8 test11 test11 4096 6月  17 14:13 ../
drwxrwxr-x 3 test11 test11 4096 6月  17 14:22 a/
lrwxrwxrwx 1 test11 test11    1 6月  17 14:22 ln-a -> a/

删除软连接可以使用 rm,类似删除一个文件:

test11@pjl:~/tmp$ ls
a  ln-a

# 删除软链接
test11@pjl:~/tmp$ rm ln-a

# 软链接已被删除
test11@pjl:~/tmp$ ls
a 

Tip:为什么要加 -s,通过 ln --help 有这么一段信息:-s, --symbolic 创建符号链接而非硬链接,也就是说加上 s 就是创建软链接。

history

history 查看已执行过的历史命令,也可以执行历史命令。

查看所有历史命令:

$ history
  ...
  170  ls
  171  cd ..
  172  ls
  173  rm ln-a
  174  ls
  175  history
  176  history |more
  177  ls
  178  history

查看最近 5 条历史命令:

test11@pjl:~/tmp$ history 5
  176  history |more
  177  ls
  178  history
  179  ls
  180  history 5

执行第 177(这里是 ls 命名) 条历史命令:

test11@pjl:~/tmp$ !177
ls
a  a.txt

运行级别

常用的运行级别有 35

  • 0 关机
  • 3 多用户有网络服务
  • 5 多用户图形
  • 6 系统重启

可以通过 init 来切换运行级别,例如 init 0 等于关机。

Tip:可以通过 --help 查看 init 命名。

root@pjl:/home/test11# init --help
init [OPTIONS...] COMMAND

Send control commands to the init daemon.

Commands:
  0              Power-off the machine
  6              Reboot the machine
  2, 3, 4, 5     Start runlevelX.target unit
  1, s, S        Enter rescue mode
  q, Q           Reload init daemon configuration
  u, U           Reexecute init daemon

Options:
     --help      Show this help
     --no-wall   Don't send wall message before halt/power-off/reboot

See the telinit(8) man page for details.

当前运行级别是图形化。请看示例:

root@pjl:/home/test11# systemctl get-default
graphical.target

比如将运行级别设置为 3:

root@pjl:/home/test11# systemctl set-default multi-user.target
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.
root@pjl:/home/test11# systemctl get-default
multi-user.target

:如果这么做,重启(reboot)后直接进入级别 3,能够节省资源。

docker 安装 ubuntu20.04

笔者不想安装虚拟机,为图方便,直接在笔记本中安装 ubuntu。

笔者已经在 win10 中安装 Docker Desktop。

Tip: 有关 docker 的介绍请看 初步认识 docker

下载镜像:

$ docker pull ubuntu:22.04

创建一个名为 ubuntu-test 的容器:

PS C:\Users\78614> docker run -itd --name ubuntu-test ubuntu:20.04

通过 exec 命令进入 ubuntu 容器,指定通过 bash 进行交互:

PS C:\Users\78614> docker exec -it ubuntu-test /bin/bash
root@5dbf1344541e:/# whoami
root

安装 vi

docker 下的 ubuntu 镜像没有 vi 命令。

root@5dbf1344541e:/# vi
bash: vi: command not found
root@5dbf1344541e:/# vim
bash: vim: command not found

如果需要 vi ,我们需要安装它。

据网友介绍:

  • apt-get/dpkg:适用于Debian、Ubuntu等平台
  • rpm/yum:适用于 Redhat、CentOS、Suse等平台
  • zypper:适合于Suse平台

笔者依次输入 rpmyumapt-get,发现 apt-get 生效:

root@9b385d9791f6:~# rpm
bash: rpm: command not found
root@9b385d9791f6:~# yum
bash: yum: command not found
root@9b385d9791f6:~# apt-get
apt 2.0.8 (amd64)
Usage: apt-get [options] command
       apt-get [options] install|remove pkg1 [pkg2 ...]
       apt-get [options] source pkg1 [pkg2 ...]

通过 --help 查看 apt-get

root@5dbf1344541e:/# apt-get --help
apt 2.0.8 (amd64)
Usage: apt-get [options] command
       apt-get [options] install|remove pkg1 [pkg2 ...]
       apt-get [options] source pkg1 [pkg2 ...]

apt-get is a command line interface for retrieval of packages
and information about them from authenticated sources and
for installation, upgrade and removal of packages together
with their dependencies.

Most used commands:
  update - Retrieve new lists of packages
  upgrade - Perform an upgrade
  install - Install new packages (pkg is libc6 not libc6.deb)
  reinstall - Reinstall packages (pkg is libc6 not libc6.deb)
  remove - Remove packages
  purge - Remove packages and config files
  autoremove - Remove automatically all unused packages
  dist-upgrade - Distribution upgrade, see apt-get(8)
  dselect-upgrade - Follow dselect selections
  build-dep - Configure build-dependencies for source packages
  satisfy - Satisfy dependency strings
  clean - Erase downloaded archive files
  autoclean - Erase old downloaded archive files
  check - Verify that there are no broken dependencies
  source - Download source archives
  download - Download the binary package into the current directory
  changelog - Download and display the changelog for the given package

See apt-get(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
                                        This APT has Super Cow Powers.

使用 apt-get 安装 vi,报错:

root@9b385d9791f6:~# apt-get install vi
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vi

通过apt-get update检索新的包列表:

root@9b385d9791f6:~# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [1324 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:8 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [30.3 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1404 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1170 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2407 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [54.2 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [27.1 kB]
Get:16 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1974 kB]
91% [16 Packages 710 kB/1974 kB 36%]
...
Reading package lists... 80%
Reading package lists... Done

再次通过 apt-get 安装 vi,依然失败:

root@9b385d9791f6:~# apt-get install vi
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vi

尝试安装 vim,成功:

root@9b385d9791f6:~# apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib
  libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim-common vim-runtime xxd xz-utils
Suggested packages:
  libasound2-plugins alsa-utils libcanberra-gtk0 libcanberra-pulse gpm readline-doc ctags vim-doc vim-scripts
The following NEW packages will be installed:
  alsa-topology-conf alsa-ucm-conf file libasound2 libasound2-data libcanberra0 libexpat1 libgpm2 libltdl7 libmagic-mgc libmagic1 libmpdec2 libogg0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib
  libreadline8 libsqlite3-0 libssl1.1 libtdb1 libvorbis0a libvorbisfile3 mime-support readline-common sound-theme-freedesktop vim vim-common vim-runtime xxd xz-utils
0 upgraded, 30 newly installed, 0 to remove and 7 not upgraded.
Need to get 14.9 MB of archives.
After this operation, 70.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libmagic-mgc amd64 1:5.38-4 [218 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libmagic1 amd64 1:5.38-4 [75.9 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 file amd64 1:5.38-4 [23.3 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libexpat1 amd64 2.2.9-1ubuntu0.4 [74.4 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 libmpdec2 amd64 2.4.2-3 [81.1 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libssl1.1 amd64 1.1.1f-1ubuntu2.13 [1321 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-minimal amd64 3.8.10-0ubuntu1~20.04.4 [717 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 mime-support all 3.64ubuntu1 [30.6 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 readline-common all 8.0-4 [53.5 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/main amd64 libreadline8 amd64 8.0-4 [131 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libsqlite3-0 amd64 3.31.1-4ubuntu0.3 [549 kB]
22% [11 libsqlite3-0 158 kB/549 kB 
...
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

命令行输入 vi 或 vim,发现 vi 和 vim 都已安装成功。

:vi 和 vim 只在这个容器中安装,如果通过 unbuntu 镜像运行另一个容器,需要重新安装 vi。我们可以通过停止和启动这个安装好 vi 的容器,反复使用它即可。

posted @ 2022-06-19 22:15  彭加李  阅读(694)  评论(0编辑  收藏  举报