Linux的学习(Linux learning)

第一章 Linux是什么与如何学习

uname -r
#查看Linux核心版本

第四章 首次登陆与线上求助

修改语系为英文:

[root@bogon ~]# LANG=en_US.utf8
[root@bogon ~]# export LC_ALL=en_US.utf8
#LANG只与输出讯息有关,若需要更改其他不同的讯息,要同步更新LC_ALL才行。

[Tab]接在一串指令的第一个字后面,则为【命令补齐】;

[root@bogon ~]# ca[Tab][Tab]
cacertdir_ehash cache_repair caller cancel.cups cas-admin
cache_check cache_restore callgrind_annotate capsh case
cache_dump cal callgrind_control captoinfo cat
cache_metadata_size ca-legacy cancel cas catchsegv

[Tab]接在一串指令的第二个字后面,则为【档案补齐】。

[root@bogon ~]# date --[Tab][Tab]
#试用无反应
\[Ctrl\]+C
#中断目前程序,比如执行\[find\]后系统跑不停

\[Ctrl\]+D
#相当于exit

\[shift\]+\[pgup\](↑)/\[pgdn\](↓)
#用于上下翻页(在Xshell中相当于鼠标滚轴的功能)
[root@bogon ~]# g[Tab][Tab]
Display all 109 possibilities? (y or n)
#查看Linux共有多少以“g”开头的指令
[root@bogon ~]# reboot    #重启
[root@bogon ~]# halt      #系统停止,屏幕可能会保留系统已停止的讯息
[root@bogon ~]# poweroff  #系统开机,所以没有提供额外的电力,屏幕空白

第五章 Linux文件权限与目录配置

[root@bogon ~]# ls -al
#ls是“list”的意思,重点在显示档案的名字与相关属性。而选项“-al”则表示里出所有的档案详细的权限与属性(包含隐藏档,就是名字第一个字符为“.”的档案)。
  • -a:全部的档案,连同隐藏档(开头为.的档案)一起列出来(常用)
  • -A:全部的档案,连同隐藏档,但不包括.与..这两个目录
  • -d:仅列出目录本身,而不列出目录内的档案资料(常用)
  • -f:直接列出结果,而不进行排序(ls预设会以档案名称排序)
  • -F:根据档案、目录等资讯,给予附加资料结构,例如:

    [*]:代表可执行档;[/]:代表目录;[=]:代表socket档案;[|]:代表FIFO档案;

  • [*]:代表可执行档;[/]:代表目录;[=]:代表socket档案;[|]:代表FIFO档案;
  • -h:将档案容量以人类较易读的方式(例如GB,KB等等)列出来;
  • -i:列出inode号码(inode:记录文件的属性,一个文件占用一个inode,同时记录此文件的资料所在的block号码。block:实际记录文件的内容,若文件太大时,会占用多个block。)
  • -l:长资料串列出,包含档案的属性与权限等等资料(常用)
  • -n:列出UID与GID而非使用与群组的名称(UID与GID详见账号管理)
  • -r:将排序结果反向输出,例如:原本档名由小到大,反向则为由大到小
  • -R:连同子目录内容一起列出来,等于该目录下的所有档案都会显示出来
  • -S:以档案容量大小排序,而不是用档名排序
  • -t:依时间排序,而不是用档名
  • –color=never:不要依据档案特性基于颜色显示
  • –color=always:显示颜色
  • –color=auto:让系统自行依据设定来判断是否给予颜色
  • –full-time:以完整时间模式(包含年、月、日、时、分)输出
  • –time={atime,ctime}:输出access时间或改变权限属性时间(ctime),而非内容变更时间(modification time)

各权限的分数对照:r-4,w-2,x-1

第六章 Linux文件与目录管理

. #代表此层目录
.. #代表上一层目录
– #代表前一个工作目录
~ #代表目前使用者所在的家目录
~acount #代表acount这个使用者的家目录(acount是个账号名称)

cd:变换目录
pwd:显示当前的目录,加上[-P]可以显示出正确的完整路径
mkdir:创建一个新的目录
mkdir -p:帮助你将所需要的目录(包含上级目录)逐级建立起来
mkdir -m:建立目录的同时直接设定档案的权限
rmdir:删除一个空的目录
rmdir -p:连同上层空的目录也一起删除
rm -r:将所有目录下的东西都删除(不建议)

cp:复制文件或目录

  • -a:相当于-dr –preserve=all的意思(dr介绍见后文)(常用)
  • -d:若来源档为连结档的属性(link file),则复制连结档属性而非档案本身
  • -f:为强制(force)的意思,若目标档案已经存在且无法开启,则移除后再尝试一次
  • -i:若目标档(destination)已经存在,在覆盖时会先询问动作的进行(常用)
  • -l:进行硬式连结(hard link)的连结档建立,而非复制档案本身
  • -p:连同档案的属性(权限、用户、时间)一起复制过去,而非使用预设属性(备份常用)
  • -r:按层次持续复制,用于目录的复制行为(常用)
  • -s:复制成为符号连结档(symbolic link),亦即[捷径]档案
  • -u:目标档比源档旧才更新目标档,或目标档不存在的情况下才复制
  • –preserve=all:除了-p的权限相关参数外,还加入SELinux的属性,links,xattr等也复制了
    最后需要注意,如果来源档有两个以上,则最后一个目的档一定要是[目录]才行

rm:移除档案或目录

  • -f:就是force的意思,忽略不存在的档案,不会出现警告讯息
  • -i:互动模式,在删除前会询问使用者是否动作
  • -r:按层次删除,最常用在目录的删除,是非常危险的选项
  • mv:移动档案与目录(可用于针对单一档案的名称变更)
  • -f:force强制的意思,如果目标档案已经存在,不会询问而直接覆盖
  • -i:若目标档案已经存在,会询问是否覆盖
  • -u:若目标档案已经存在、且源档案比较新,才会更新

文件内容查阅

  • cat:由第一行开始显示文件内容(加上-n或者-b也能显示行号)
  • tac:由最后一行开始显示文件内容
  • nl:显示文件内容同时展示行号(还可以在前面补0)
  • more:一页一页的展示文件内容
  • less:和more相似,但可以往前翻页
  • head:只看头几行
  • tail:只看末尾几行
  • od:以二进制的方式读取文件内容

touch:修改文件时间或新建文件

  • -a:仅修订access time
  • -c:仅修改档案的时间,若该文件不存在则不建立新文件
  • -d:后面可以接想要修订的日期而不用当前的日期,也可以使用–date=”日期或时间“
  • -m:仅修改mtime
  • -t:后面可以接想要修订的日期而不用当前的日期,格式为[YYYYMMDDhhmm]

第八章 文件与文件系统的压缩、打包与备份

Linux上常见的压缩指令集就是gzip,bzip2以及最新的xz。
最通用的是tar,可以将很多文件打包成为一个文件

最常用的解压指令:

[root@bogon ~]# tar xvf *.tar
[root@bogon ~]# tar zxvf *.tar.gz

第九章 vim程序编辑器

vim程序编辑器(vi 的进阶版本,vim 可以依据文件的副档名或者是文件內的开头咨询,用颜色或底线等方式来显示一些特殊的信息)
vi的三种模式:

  • (一般)指令模式
    用vi打开一个文件就进入了(一般)指令模式,可以上下左右移动游标,也可以使用删除单字、删除整行来处理文件内容;或者复制、粘贴来处理文件资料。(但是不能编辑文件内容哦!)
  • 编辑模式
    指令模式下可以进行删除、复制、粘贴等操作,但却不能编辑文件内容。按下【i,l,o,O,a,A,r,R】任意一个按钮进入编辑模式,界面下方会出现【INSERT】或【REPLACE】字样,必须按【Esc】键才能退出编辑模式,回到(一般)指令模式。
  • 指令行命令模式
    一般按下【?】中任意一个按键进入指令行命令模式,游标移动到最底下那一行。这个模式下,可以提供你搜寻资料的动作。读取、存档、大量取代字元、离开vi、显示行号等等的动作都是在这个模式下完成的。也是按下【Esc】退出编辑模式,回到(一般)指令模式。

vi 后面一定要加文件名,不管该文件存在与否。

(一般)指令模式可用的按钮
0/[Home]:移动到这一行的最前面字元处
$/[End]:移动到这一行的最后面字元处
G:移动到这个文件的最后一行
gg:移动到这个文件的第一行(相当于1G)
n<space>:移动到第n行
n<Enter>:向下移动n行
:1,$s/word1/word2/g:从第一行到最后一行寻找word1字符串,并将该字符串更改为word2。
:1,$s/word1/word2/gc:从第一行到最后一行寻找word1字符串,并将该字符串更改为word2。(在替换前会进行确认)
x,X:在同一行中,x为向后删除一个字元,X为向前删除一个字元
dd:删除游标所在那一行
yy:复制游标所在那一行
nyy:复制游标所在的向下n行
p,P:p为将复制的资料粘贴在游标下一行,P则为粘贴在游标上一行。(舉例來說,我目前游標在第 20 列,且已經複製了 10 列資料。則按下 p 後, 那 10 列資料會貼在原本的 20 列之後,亦即由 21 列開始貼。但如果是按下 P 呢? 那麼原本的第 20 列會被推到變成 30 列。)
u:复制前一个动作
[Ctrl]+r:重做上一个动作
.:重复前一个动作

在我们使用vim编辑器时,vim会在被编辑文件的目录下在新建一个名为 .filename.swp 的文件,我们对文件所作的动作都会被记录在这个文件中。

如果我们的系统因为某些原因断线了,导致我们编辑的文件还没有储存,这个时候 .filename.swp 就能发挥救援的功能了。

系统中断后重新编辑文件会出现警告,如果之前的vim处理动作尚未储存,此时你应该按下[R]键,(R)ecover项目。此时vim会载入swp的内容,让我们自行决定要不要储存,这样就能就会之前未储存的工作。但是swp文件不会在结束vim后自动删除,所以离开vim后要自行删除swp文件以避免每次打开这个文件都出现警告。

第十章 认识与学习bash

输入指令后,通过 kernel (核心) 的控制工作让硬件知道我下达的指令。也就是說,我们必须通过『 Shell 』将我们输入的指令与 Kernel 沟通,好让 Kernel 可以控制硬件来准确无误的工作。Linux 使用的这一版本的Shell就称为『 Bourne Again SHell (简称 bash) 』
bash的优点:

  • 命令编修能力
    可以记忆使用过的命令,只要在指令行上通过【上下键】就能找到前/后一个输入的指令,存储数量可以达到1000个。(记录在家目录内的 .bash_history 里面,不过 ~./bash_history 记录的是上一次登录所执行过的指令)
  • 命令与文件补全功能([Tab]按键的好处)
    见上文
  • 命令别名设定功能(alias)[root @bogon ~] # alias lm=‘ls -al’
    #使用上述命令为指令‘ls -al’设定别名‘lm’,使指令更加精简

  • 工作控制,前景、背景控制
    前景、背景控制能让工作更加顺利,工作控制能将工作丢到背景中执行。(后面会讲
  • 程序化脚本(shell scripts)
    可以将你平时管理系统常需要下达的连续指令写成一个文件,该文件可以通过对谈互助方式来进行主机的勘测工作,也可以借由shell提供的环境变量及相关指令来进行设计。
  • 万用字元(Wildcard)
    想要知道/usr/bin地下有多少以X开头的文件吗?[root @bogon ~] # ls -l /usr/bin/X*
    #使用上述指令就可以知道

查询指令是否为Bash shell的内建指令,type

[root @bogon ~] # type [-tpa] name
选项与参数:
    :不加任何选项与参数时,type 會显示出 name 是外部指令还是 bash 内建指令
-t  :当加入 -t 参数时,type 会将 name 以底下這些字眼显示出他的意义:
      file    :表示为外部指令;
      alias   :表示该指令为命令別名所设定的名称;
      builtin :表示该指令为 bash 内建的指令功能;
-p  :如果后面接的 name 为外部指令時,才会显示完整文件名;
-a  :会由 PATH 变量定义的路径中,将所有含 name 的指令都列出來,包含 alias

type主要是找执行文件而不是一般文件,有时也可以起到跟witch一样的作用:查找指令

指令的下达与快速编辑按钮

#如果指令串太长的話,如何使用两行來输出?
[root@bogon ~] # cp /var/spool/mail/root /etc/crontab \
> /etc/fstab /root

[Ctrl]+u:向前删除指令串
[Ctrl]+k:向后删除指令串
[Ctrl]+a:游标移动到整个指令串的最前面
[Ctrl]+e:游标移动到整个指令串的最后面

[root @bogon ~] # echo ${变量}
#变量的取用(也可以不加花括号)

变量的设定规则:

  • 变量与变量内容以等号连接
    如:name=vm176
  • 等号两边不能有空格
    反例:name = vm176;name=vm 176
  • 变量名称只能是英文+数字,但开头不能是数字
    反例:2name=vm176
  • 变量内若有空白字元可用双引号[“”]或单引号[‘’]将变量内容括起来

    双引号内的特殊符号如 $ 等,可以保持原本的属性,如下所示:
    『var=”lang is $LANG”』则『echo $var』可得『lang is zh_TW.UTF-8』
    单引号内的特殊字元则仅为一般字元 (纯文字),如下所示:
    『var=’lang is $LANG’』則『echo $var』可得『lang is $LANG』

  • 双引号内的特殊符号如 $ 等,可以保持原本的属性,如下所示:
    『var=”lang is $LANG”』则『echo $var』可得『lang is zh_TW.UTF-8』
  • 单引号内的特殊字元则仅为一般字元 (纯文字),如下所示:
    『var=’lang is $LANG’』則『echo $var』可得『lang is $LANG』
  • 可用跳脱字元 [ \ ] 将特殊符号(如 [Enter],$,\,空格,’ 变成一般字元
    如:name=vm\ 176
  • 在一串指令的执行中,还需要藉由其他额外的指令所提供的资讯时,可以使用反单引号[`指令`]或[$(指令)]。
    例如想要取得核心版本的设定:
    『version=$(uname -r)』再『echo $version』可得『3.10.0-229.el7.x86_64』
  • 若该变量要在其他子程序执行,则需要以export来使变量变成环境变量:
    export PATH
  • 通常大写字元为系统预设变量,自行设定变量可用小写字元,方便判断。
  • 取消变量用unset:[unset 变量名称]
    如:unset name

标准输入   (stdin) :代码为 0 ,使用 < 或 << ;
标准输出   (stdout):代码为 1 ,使用 > 或 >> ;
标准错误输出 (stderr):代码为 2 ,使用 2> 或 2>> ;
1> :以覆盖的方法将『正确的资料』输出到指定的文件或装置上;
1>>:以累加的方法将『正确的资料』输出到指定的文件或装置上;
2> :以覆盖的方法将『错误的资料』输出到指定的文件或装置上;
2>>:以累加的方法将『错误的资料』输出到指定的文件或装置上;

cmd1 && cmd2

  • 若 cmd1 执行完毕且为正确执行($?=0),则开始执行 cmd2。
  • 若 cmd1 执行完毕且为错误 ($?≠0),则 cmd2 不执行。

cmd1 || cmd2

  • 若 cmd1 执行完毕且正确执行 ($?=0),则 cmd2 不执行。
  • 若 cmd1 执行完毕且为错误 ($?≠0),则开始执行 cmd2。

第十一章 正则表达式与文件格式化处理

日常使用 vim 作文书处理或程式撰写时使用到的『搜寻/删除/取代』等等的功能, 这些举动要做的漂亮,就得要配合正则表达式来处理。
正则表达式就是处理字串的方式,以行为单位进行字串的处理。

grep的一些进阶选项
[root@bogon ~]# grep [-A][-B][–color=auto] ‘搜寻字串’ filename
选项与参数:
-A:后面可加数字,为after的意思,除了列出该行外,后续的n行也列出来;
-B:后面可加数字,为before的意思,除了列出该行外,前面的n行也列出来。
在CentOS7当中,预设已经将–color=auto加入alias当中了,可以直接使用grep达到关键字有颜色提示的效果

[root@bogon ~]# grep -n ‘xxx’ filename.filetype
#显示行号
[root@bogon ~]# grep -vn ‘xxx’ filename.filetype
#反向选择
[root@bogon ~]# grep -in ‘xxx’ filename.filetype
#不计较xxx的大小写
[root@bogon ~]# grep -n ‘[^g]xxx’ filename.filetype
#不想xxx前面有g出现,^为集合字元的反向选择

当我们在书写一组字元时,如果它们是连续的(例如大写字母、小写字母、数字等),那我们就可以采取以下方式书写:[a-z]、[A-Z]、[0-9]。
如果要求字符串是数字与英文,那么就把它们全部写在一起:[a-zA-Z0-9]。
字符“^”在括号[]内外的定义是不同的,在括号内表示反向选择,在括号外表示将字符定位在行首。

RE字符
意义与范例
^word 意义:待搜寻的字符串(word)在行首
范例:搜寻行首为#开始的那一行,并列出行号
grep -n ‘^#’ regular_express.txt
word$ 意义:待搜寻的字符串(word)在行尾
范例:搜寻以!结尾的那一行,并列出行号
grep -n ‘!$’ regular_express.txt
. 意义:代表一定有一个任意字符的字符串
范例:搜寻的字串可以是(eve)(eae)(eee)(e e),但不能是(ee)。也就是两个e之间一定要有字符,即便是空白字符。
grep -n ‘e.e’ regular_express.txt
\ 意义:shell的跳脱字符,将字符的特殊意义去除
范例:搜寻含有单引号的那一行
grep -n \’ regular_express.txt
* 意义:找出含有(es)(ess)(esss)等等的字符串,*可以是零。*为重复前一个RE字符,所以要紧挨着前一个字符,例如任意字符则为’.*’
范例:搜寻以!结尾的那一行,并列出行号
grep -n ‘ess*’ regular_express.txt
[list] 意义:字符集合,里面列出想要的字符
范例:搜寻含有(gl)或(gd)的那一行。需要特别留意的是,在[]当中仅代表一个待搜寻的字符,例如 ‘ a[alf]y ‘ 代表搜寻的字串可以是aay,afy,aly,即[alf]代表的是a或l或f。
grep -n ‘g[ld]’ regular_express.txt
[n1-n2] 意义:字符集合,里面列出想要的字符范围
范例:搜寻含有任意数字的那一行,-代表两个字符之间的所有字符,与ASCII码顺序有关
grep -n ‘[0-9]’ regular_express.txt
[^list] 意义:字符集合,里面列出不要的字符或范围
范例:搜寻的字符可以是(oog)(ood)但不能是(oot)
grep -n ‘oo[^t]’ regular_express.txt
\{n,m\} 意义:连续n到m个的前一个RE字符
意义:若为\{n\}则是连续n个的前一个RE字符
意义:若为\{n,\}则是连续n个以上的前一个RE字符
范例:在g与g之间有两到三个o存在的字符串
grep -n ‘go\{2,3\}g’ regular_express.txt

sed:一个管线命令,可以分析 standard input ,还可以将资料进行替换、删除、新增、撷取特定行等功能。

[dmtsai@study ~]$ sed [-nefr] [动作]
选项与参数:
-n  :使用安靜(silent)模式。在一般 sed 的用法中,所有来自STDIN 的资料一般都会被列出到屏幕上。
      但如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出來。
-e  :直接在指令列模式上进行 sed 的动作编辑;
-f  :直接将 sed 的动作写在一个文件内, -f filename 则可以执行 filename 内的 sed 动作;
-r  :sed 的动作支援的是延伸型正则表达式的语法。(预设是基础正则表达式语法)
-i  :直接修改读取的文件内容,而不是由屏幕输出。

动作说明:  [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』

function 有底下这些选项:
a   :新增, a 的后面可以接字符串,而这些字符串会在新的一行出现(目前的下一行)~
c   :取代, c 的后面可以接字符串,这些字符串可以取代 n1,n2 之间的行!
d   :删除,因为是删除啊,所以 d 后面通常不接任何东西;
i   :插入, i 的后面可以接字符串,而这些字符串会在新的一行出现(目前的上一行);
p   :列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s   :替换,可以直接进行替换的工作。通常这个 s 的动作可以搭配正则表达式!
      例如 1,20s/old/new/g 就是啦!
  • sed 后面接的动作,请务必以 ” 两个单引号括住

部分资料的搜寻与替换功能:

sed 's/要被取代的字串/新的字串/g'

格式化输出printf的使用
资料处理工具awk的使用
文件比对工具:diff,cmp,patch
文件打印准备:pr

第十二章 学习Shell Scripts

————————

Chapter 1 what is Linux and how to learn it

uname -r
#查看Linux核心版本

Chapter IV first landing and online help

Revised language family to English:

[root@bogon ~]# LANG=en_US.utf8
[root@bogon ~]# export LC_ALL=en_US.utf8
#LANG只与输出讯息有关,若需要更改其他不同的讯息,要同步更新LC_ALL才行。

[tab] followed by the first word of a series of instructions is [command complement];

[root@bogon ~]# ca[Tab][Tab]
cacertdir_ehash cache_repair caller cancel.cups cas-admin
cache_check cache_restore callgrind_annotate capsh case
cache_dump cal callgrind_control captoinfo cat
cache_metadata_size ca-legacy cancel cas catchsegv

If [tab] is followed by the second word of a series of instructions, it is [file supplement].

[root@bogon ~]# date --[Tab][Tab]
#试用无反应
\[Ctrl\]+C
#中断目前程序,比如执行\[find\]后系统跑不停

\[Ctrl\]+D
#相当于exit

\[shift\]+\[pgup\](↑)/\[pgdn\](↓)
#用于上下翻页(在Xshell中相当于鼠标滚轴的功能)
[root@bogon ~]# g[Tab][Tab]
Display all 109 possibilities? (y or n)
#查看Linux共有多少以“g”开头的指令
[root@bogon ~]# reboot    #重启
[root@bogon ~]# halt      #系统停止,屏幕可能会保留系统已停止的讯息
[root@bogon ~]# poweroff  #系统开机,所以没有提供额外的电力,屏幕空白

Chapter V Linux file permissions and directory configuration

[root@bogon ~]# ls -al
#ls是“list”的意思,重点在显示档案的名字与相关属性。而选项“-al”则表示里出所有的档案详细的权限与属性(包含隐藏档,就是名字第一个字符为“.”的档案)。
  • -a: All files are listed together with hidden files (files starting with). Commonly used)
  • -A: All files, together with hidden files, but excluding the two directories of. And
  • -d: List only the directory itself, not the file data in the directory (common)
  • -f: List the results directly without sorting (LS default sorting by file name)
  • -F: Give additional data structures according to files, directories and other information, such as:
    [*]: represents executable file; [/]: represents the directory; []: represents socket file; [|]: represents FIFO file;
  • [*]: represents executable file; [/]: represents the directory; []: represents socket file; [|]: represents FIFO file;
  • -h: List the file capacity in a human readable way (such as GB, KB, etc.);
  • -i: List inode numbers (inode: records the attributes of a file. One inode is occupied by a file, and the block number of the data of the file is recorded at the same time. Block: actually records the contents of the file. If the file is too large, it will occupy multiple blocks.)
  • -l: Long data list, including file attributes, permissions and other data (common)
  • -n: List uid and GID instead of the name of the user and group (see account management for uid and GID)
  • -r: Reverse the sorting results. For example, the original file name is from small to large, and the reverse is from large to small
  • -R: It is listed together with the contents of the subdirectory, which means that all files under the directory will be displayed
  • -S: Sort by file size, not by file name
  • -t: Sort by time, not by file name
  • –Color = never: do not display based on color according to file properties
  • –color=always:显示颜色
  • –Color = Auto: let the system judge whether to give color according to the setting
  • –Full time: output in full time mode (including year, month, day, hour and minute)
  • –time={atime,ctime}:输出access时间或改变权限属性时间(ctime),而非内容变更时间(modification time)

Score comparison of each authority: R-4, W-2, X-1

Chapter 6 Linux file and directory management

. # represents the directory of this layer
.. # represents the upper level directory
-# represents the previous working directory
~# represents the home directory of the current user
~Account # represents the user’s home directory (account is an account name)

CD: Transform directory
PWD: displays the current directory, and [- P] can display the correct full path
MKDIR: create a new directory
MKDIR – P: help you set up the required directory (including the parent directory) level by level
MKDIR – M: set file permissions directly while creating a directory
Rmdir: delete an empty directory
Rmdir – P: delete it together with the empty directory on the upper layer
RM – R: delete everything in the directory (not recommended)

< strong > CP < / strong >: copy a file or directory

  • -a: Equivalent to – Dr — preserve = all (see the following text for Dr introduction) (commonly used)
  • -d: If the source file is a link file attribute, copy the link file attribute instead of the file itself
  • -f: To force, if the target file already exists and cannot be opened, remove it and try again
  • -i: If the destination file already exists, you will first inquire about the progress of the action (common) when overwriting
  • -l: Create a hard link link file instead of copying the file itself
  • -p: Copy the past along with the attributes of the file (permission, user, time), instead of using the default attributes (backup common)
  • -r: Continuous replication by hierarchy, used for directory replication behavior (common)
  • -s: Copy to a symbolic link, or shortcut file
  • -u: The target file is updated only when the target file is older than the source file, or copied only when the target file does not exist
  • –Preserve = all: in addition to the permission related parameters of – P, SELinux attributes are added, and links, xattr, etc. are also copied
    Finally, it should be noted that if there are more than two source files, the last destination file must be [directory]

< strong > RM < / strong >: remove a file or directory

  • -f: Force means to ignore nonexistent files and no warning message will appear
  • -i: In interactive mode, the user will be asked whether to move before deletion
  • -r: Delete by hierarchy, which is most commonly used in directory deletion, is a very dangerous option
  • MV: move files and directories (can be used to change the name of a single file)
  • -f: Force force means that if the target file already exists, it will not be queried but directly overwritten
  • -i: If the target file already exists, you will be asked whether to overwrite it
  • -u: If the target file already exists and the source file is relatively new, it will be updated

< strong > file content reference < / strong >

  • Cat: display the file content from the first line (plus – N or – B can also display the line number)
  • TAC: displays the contents of the file from the last line
  • Nl: displays the file content and line number (you can also fill 0 in front)
  • More: show the contents of the document page by page
  • Less: similar to more, but you can turn the page forward
  • Head: just look at the first few lines
  • Tail: just look at the last few lines
  • Od: read the contents of the file in binary mode

< strong > touch < / strong >: modify the file time or create a new file

  • -a:仅修订access time
  • -c: Only modify the time of the file. If the file does not exist, no new file will be created
  • -d: You can follow the date you want to revise instead of the current date, or you can use — date = “date or time“
  • -m:仅修改mtime
  • -t: It can be followed by the date you want to revise instead of the current date. The format is [yyyymmddhhmm]

Chapter VIII compression, packaging and backup of files and file systems

The common compression instruction sets on Linux are gzip, bzip2 and the latest XZ.
The most common is tar, which can package many files into one file

The most commonly used decompression instructions:

[root@bogon ~]# tar xvf *.tar
[root@bogon ~]# tar zxvf *.tar.gz

Chapter IX VIM program editor

< strong > VIM program editor < / strong > (for the advanced version of VI, VIM can display some special information by means of color or bottom line according to the file extension name or the beginning of the file)
Three modes of VI:

  • (general) command mode
    When you open a file with VI, you enter the (general) instruction mode. You can move the cursor up, down, left and right, or delete words and delete the whole line to process the file content; Or copy and paste to process documents. (but you can’t edit the contents of the file!)
  • Edit mode
    In instruction mode, you can delete, copy, paste and other operations, but you cannot edit the file content. Press any button of [I, l, O, O, a, a, R, R] to enter the editing mode, and the words [insert] or [replace] will appear at the bottom of the interface. You must press [ESC] to exit the editing mode and return to the (general) command mode.
  • Command line command mode
    Generally, press any key in [?] to enter the command line command mode, and the cursor moves to the bottom line. In this mode, you can search for data. Reading, archiving, replacing a large number of characters, leaving VI, displaying line numbers, and so on are all done in this mode. Also press [ESC] to exit the editing mode and return to the (general) command mode.

VI must be followed by the file name, whether the file exists or not.

< strong > (general) buttons available in command mode < / strong >:
0 / [home]: move to the first character of this line
$/ [End]: move to the last character of this line
G: Move to the last line of this file
GG: move to the first line of this file (equivalent to 1g)
n< space>: Move to line n
n< Enter>: Move n rows down
: 1, $s / word1 / word2 / G: find the word1 string from the first line to the last line and change the string to word2.
: 1, $s / word1 / word2 / GC: look for the word1 string from the first line to the last line and change the string to word2. (to be confirmed before replacement)
x. X: in the same line, X means to delete a character backward and X means to delete a character forward
DD: delete the row where the cursor is located
YY: copy the row where the cursor is located
NYY: copy the down n line where the cursor is located
p. P: P is to paste the copied data to the next row of the cursor, and P is to paste to the previous row of the cursor. (for example, I currently have the cursor in column 20 and have copied 10 columns of data. If I press P, the 10 columns of data will be pasted after the original 20 columns, that is, from column 21. But if I press P, the original column 20 will be pushed to column 30.)
u: Copy previous action
[Ctrl] + R: redo the previous action
.: repeat the previous action

When we use VIM editor, VIM will create a new file named. Filename.swp in the directory of the edited file, and all our actions on the file will be recorded in this file.

If our system is disconnected for some reason and the file we edited has not been saved, then. Filename.swp can play the rescue function.

After the system is interrupted, a warning will appear when editing the file again. If the previous VIM processing action has not been saved, you should press the [R] key and (R) recover item. At this time, VIM will load the contents of SWP and let us decide whether to save or not, so that we can save the work that has not been saved before. However, SWP files will not be deleted automatically after VIM, so you should delete SWP files after leaving VIM to avoid warnings every time you open this file.

Chapter 10 understanding and learning Bash

After inputting the instructions, let the hardware know the instructions I issued through the control of the kernel. In other words, we must communicate the instructions we input with the kernel through the “shell”, so that the kernel can control the hardware to work accurately. This version of shell used by Linux is called “Bourne again shell (bash for short)”
Bash’s advantages:

  • Command editing capability
    The used commands can be memorized. As long as [up and down] on the command line, the previous / next input commands can be found, and the storage quantity can reach 1000. (it is recorded in. Bash_history in the home directory, but ~. / bash_history records the instructions executed during the last login)
  • Command and file completion function ([tab] key benefits)
    See above
  • Command alias setting function (alias) [root @ Bogon ~] # alias LM = ‘LS – Al’
    #Use the above command to set the alias’ LM ‘for the instruction’ LS – Al ‘to make the instruction more compact
  • Work control, foreground and background control
    Foreground and background control can make the work more smooth, and work control can throw the work into the background for execution. (I’ll talk about it later
  • Shell scripts
    You can write the continuous instructions often issued by your management system into a file. The file can carry out the survey work of the host through conversation and mutual assistance, and can also be designed by using the environment variables and relevant instructions provided by the shell.
  • Wildcard
    Want to know how many files start with X in / usr / bin? [root @bogon ~] # ls -l /usr/bin/X*
    #You can know by using the above instructions

Query whether the instruction is a built-in instruction of bash shell, < strong > type < / strong >

[root @bogon ~] # type [-tpa] name
选项与参数:
    :不加任何选项与参数时,type 會显示出 name 是外部指令还是 bash 内建指令
-t  :当加入 -t 参数时,type 会将 name 以底下這些字眼显示出他的意义:
      file    :表示为外部指令;
      alias   :表示该指令为命令別名所设定的名称;
      builtin :表示该指令为 bash 内建的指令功能;
-p  :如果后面接的 name 为外部指令時,才会显示完整文件名;
-a  :会由 PATH 变量定义的路径中,将所有含 name 的指令都列出來,包含 alias

Type is mainly used to find execution files rather than general files. Sometimes it can also play the same role as switch: finding instructions

< strong > command release and Quick Edit buttons < / strong >

#如果指令串太长的話,如何使用两行來输出?
[root@bogon ~] # cp /var/spool/mail/root /etc/crontab \
> /etc/fstab /root

[Ctrl] + U: delete instruction string forward
[Ctrl] + K: delete instruction string backward
[Ctrl] + A: the cursor moves to the front of the entire instruction string
[Ctrl] + e: the cursor moves to the end of the entire instruction string

[root @bogon ~] # echo ${变量}
#变量的取用(也可以不加花括号)

Variable setting rules:

  • Variables and variable contents are connected by an equal sign
    For example: name = vm176
  • There must be no spaces on either side of the equal sign
    Counterexample: name = vm176; name=vm 176
  • Variable names can only be English + numbers, but cannot start with numbers
    Counterexample: 2Name = vm176
  • If there are blank characters in a variable, you can use double quotation marks [“”] or single quotation marks [”] to enclose the variable content
    Special symbols in double quotation marks, such as $, can maintain the original attributes, as shown below:
    If “var =” Lang is $Lang “, then” echo $var “will get” Lang is zh_tw.utf-8 ”
    Special characters in single quotation marks are only general characters (plain text), as shown below:
    If “var =’lang is $Lang ‘,” echo $var “will get” Lang is $Lang “
  • Special symbols in double quotation marks, such as $, can maintain the original attributes, as shown below:
    If “var =” Lang is $Lang “, then” echo $var “will get” Lang is zh_tw.utf-8 “
  • Special characters in single quotation marks are only general characters (plain text), as shown below:
    If “var =’lang is $Lang ‘,” echo $var “will get” Lang is $Lang “
  • Special symbols such as [Enter], $, \, space, ‘can be changed into general characters with the skip character [\]
    For example: name = VM \ 176
  • When the information provided by other additional instructions is needed during the execution of a series of instructions, you can use anti single quotation marks [` instruction `] or [$(instruction)].
    For example, to get the core version settings:
    “Version = $(uname – R)” and “echo $version” to get “3.10.0-229. EL7. X86_64”
  • If the variable is to be executed in other subroutines, you need to use export to change the variable into an environment variable:
    export PATH
  • Generally, upper case characters are system default variables, and lower case characters can be used for self setting variables to facilitate judgment.
  • 取消变量用unset:[unset 变量名称]
    如:unset name

Standard input (stdin): code is 0, use & lt; Or & lt& lt; ;
Standard output (stdout): Code 1, use & gt; Or & gt& gt; ;
Standard error output (stderr): Code 2, use 2 & gt; Or 2 & gt& gt; ;
1> : Output the “correct data” to the specified file or device by overwriting;
1>& gt;: Output the “correct data” to the specified file or device by cumulative method;
2> : Output the “wrong data” to the specified file or device by overwriting;
2>& gt;: Output the “wrong data” to the specified file or device by accumulation;

cmd1 && cmd2

  • If CMD1 is executed correctly ($? = 0), start to execute cmd2.
  • If CMD1 is executed and it is an error ($? ≠ 0), cmd2 will not be executed.

cmd1 || cmd2

  • If CMD1 is completed and executed correctly ($? = 0), cmd2 will not execute.
  • If the execution of CMD1 is completed and it is an error ($? ≠ 0), start the execution of cmd2.

Chapter 11 regular expression and file format processing

The “search / delete / replace” and other functions used in VIM composition processing or program writing are used in daily life. If these actions want to be beautiful, they must be processed with regular expressions.
Regular expressions are the way to process strings, which are processed in behavioral units.

< strong > some advanced options of grep < / strong >:
[ root@bogon ~]#Grep [- A] [- b] [– color = auto] ‘search string’ filename
Options and parameters:
-A: Numbers can be added after, which means after. In addition to listing this line, the subsequent n lines are also listed;
-B: A number can be added after it, which means before. In addition to listing this line, the previous n lines are also listed.
In centos7, by default — color = auto has been added to alias. You can directly use grep to achieve the effect of color prompt for keywords

[root@bogon ~]# grep -n ‘xxx’ filename.filetype
#显示行号
[root@bogon ~]# grep -vn ‘xxx’ filename.filetype
#反向选择
[root@bogon ~]# grep -in ‘xxx’ filename.filetype
#不计较xxx的大小写
[root@bogon ~]# grep -n ‘[^g]xxx’ filename.filetype
#不想xxx前面有g出现,^为集合字元的反向选择

When we write a group of characters, if they are continuous (such as uppercase letters, lowercase letters, numbers, etc.), we can write in the following ways: [A-Z], [A-Z], [0-9].
If the string is required to be numeric and English, write them all together: [a-za-z0-9].
The definition of the character “^” inside and outside brackets [] is different. Inside brackets means reverse selection, and outside brackets means positioning the character at the beginning of the line.

RE字符
意义与范例
^word 意义:待搜寻的字符串(word)在行首
范例:搜寻行首为#开始的那一行,并列出行号
grep -n ‘^#’ regular_express.txt
word$ 意义:待搜寻的字符串(word)在行尾
范例:搜寻以!结尾的那一行,并列出行号
grep -n ‘!$’ regular_express.txt
. 意义:代表一定有一个任意字符的字符串
范例:搜寻的字串可以是(eve)(eae)(eee)(e e),但不能是(ee)。也就是两个e之间一定要有字符,即便是空白字符。
grep -n ‘e.e’ regular_express.txt
\ 意义:shell的跳脱字符,将字符的特殊意义去除
范例:搜寻含有单引号的那一行
grep -n \’ regular_express.txt
* 意义:找出含有(es)(ess)(esss)等等的字符串,*可以是零。*为重复前一个RE字符,所以要紧挨着前一个字符,例如任意字符则为’.*’
范例:搜寻以!结尾的那一行,并列出行号
grep -n ‘ess*’ regular_express.txt
[list] 意义:字符集合,里面列出想要的字符
范例:搜寻含有(gl)或(gd)的那一行。需要特别留意的是,在[]当中仅代表一个待搜寻的字符,例如 ‘ a[alf]y ‘ 代表搜寻的字串可以是aay,afy,aly,即[alf]代表的是a或l或f。
grep -n ‘g[ld]’ regular_express.txt
[n1-n2] 意义:字符集合,里面列出想要的字符范围
范例:搜寻含有任意数字的那一行,-代表两个字符之间的所有字符,与ASCII码顺序有关
grep -n ‘[0-9]’ regular_express.txt
[^list] 意义:字符集合,里面列出不要的字符或范围
范例:搜寻的字符可以是(oog)(ood)但不能是(oot)
grep -n ‘oo[^t]’ regular_express.txt
\{n,m\} 意义:连续n到m个的前一个RE字符
意义:若为\{n\}则是连续n个的前一个RE字符
意义:若为\{n,\}则是连续n个以上的前一个RE字符
范例:在g与g之间有两到三个o存在的字符串
grep -n ‘go\{2,3\}g’ regular_express.txt

< strong > sed: a pipeline command that can analyze standard input, replace, delete, add, and retrieve specific lines of data

[dmtsai@study ~]$ sed [-nefr] [动作]
选项与参数:
-n  :使用安靜(silent)模式。在一般 sed 的用法中,所有来自STDIN 的资料一般都会被列出到屏幕上。
      但如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出來。
-e  :直接在指令列模式上进行 sed 的动作编辑;
-f  :直接将 sed 的动作写在一个文件内, -f filename 则可以执行 filename 内的 sed 动作;
-r  :sed 的动作支援的是延伸型正则表达式的语法。(预设是基础正则表达式语法)
-i  :直接修改读取的文件内容,而不是由屏幕输出。

动作说明:  [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』

function 有底下这些选项:
a   :新增, a 的后面可以接字符串,而这些字符串会在新的一行出现(目前的下一行)~
c   :取代, c 的后面可以接字符串,这些字符串可以取代 n1,n2 之间的行!
d   :删除,因为是删除啊,所以 d 后面通常不接任何东西;
i   :插入, i 的后面可以接字符串,而这些字符串会在新的一行出现(目前的上一行);
p   :列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s   :替换,可以直接进行替换的工作。通常这个 s 的动作可以搭配正则表达式!
      例如 1,20s/old/new/g 就是啦!
  • Please be sure to enclose the actions followed by sed in two single quotation marks

Search and replacement of some data:

sed 's/要被取代的字串/新的字串/g'

Use of formatted output printf
Use of data processing tool awk
File comparison tools: diff, CMP, patch
Document printing preparation: PR

第十二章 学习Shell Scripts