【Linux】基本指令(二)

目录

man 指令

cp 指令

mv 指令

1、剪切功能

2、重命名功能

3、剪切+重命名功能

echo 指令

cat 指令

wc 指令

more 指令

less 指令

head 指令

tail 指令

管道符 |

date 指令

时间戳

cal 指令

sort 指令

uniq 指令

搜索指令

find 指令

which 指令

whereis 指令

grep 指令

top 指令

zip/unzip 指令


本篇文章我们来进行 Linux 第二部分基本指令的学习。

man 指令

语法:man [选项] [命令]

功能:查看联机手册

man 指令本身也可以通过 man 指令查询到:

[root@iZuf69tfiox41j76yf0416Z ~]# man man

 如图所示,man 手册分为 8 个章节和 1 个内核例程:

1 是普通的命令
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
3 是库函数,如printf,fread

4 是特殊文件,也就是/dev下的各种设备文件
5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义
6 是给游戏留的,由各个游戏自己定义
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
8 是系统管理用的命令,这些命令只能由root使用,如ifconfig

9 是内核例程(非标准)

 按 q 键退出查阅。

在Linux中查询手册时,默认是从 1 号手册开始查找,如果 1 号手册中没有相关指令,再按照顺序依次向下查找。

比如我想要查找 printf 指令,可以直接输入 man printf:

[root@iZuf69tfiox41j76yf0416Z ~]# man printf

 默认查找到的是 1 号手册中的 Linux 内的 printf 指令。

如果我们想要查找 C语言库函数中的 printf 指令,就需要在 3 号文件中查找:

[root@iZuf69tfiox41j76yf0416Z ~]# man 3 printf

cp 指令

语法:cp [选项] [源文件或目录] [目标文件或目录]

功能:复制文件或目录

说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息


常用选项:
-f 或 --force 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 或 --interactive 覆盖文件之前先询问用户
-r 递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
-R 或 --recursive 递归处理,将指定目录下的文件及子目录一并处理

如果 cp 指令不加命令选项,则默认只能拷贝普通文件,无法拷贝目录。 

[root@iZuf69tfiox41j76yf0416Z newdir]# pwd
/root/lll/newdir
[root@iZuf69tfiox41j76yf0416Z newdir]# ls
dir  text1.c  tou1
[root@iZuf69tfiox41j76yf0416Z newdir]# cp dir ..
cp: omitting directory ‘dir’
[root@iZuf69tfiox41j76yf0416Z newdir]# 

如果想要拷贝目录,需要加命令选项 -r 进行递归拷贝:

[root@iZuf69tfiox41j76yf0416Z lll]# pwd
/root/lll
[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir  newdir2  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# ls newdir
dir  text1.c  tou1
[root@iZuf69tfiox41j76yf0416Z lll]# cp -r newdir/dir .
[root@iZuf69tfiox41j76yf0416Z lll]# ls
dir  newdir  newdir2  tou2

mv 指令

mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。

语法:mv [选项] [源文件或目录] [目标文件或目录]

功能:

  1. 当第二个参数类型是 文件 时, mv命令完成 文件重命名 ,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。
  2. 当第二个参数是 已存在的目录名称 时,mv命令完成 文件转移 ,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。

 常用选项:
-f : force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖

1、剪切功能

语法:mv src(文件或者目录) dst(一定是目录)

剪切文件:把 dir 目录中的普通文件 123 剪切到上级目录中:

[root@iZuf69tfiox41j76yf0416Z dir]# pwd
/root/lll/dir
[root@iZuf69tfiox41j76yf0416Z dir]# ls
123  456  diii
[root@iZuf69tfiox41j76yf0416Z dir]# mv 123 ..
[root@iZuf69tfiox41j76yf0416Z dir]# ls
456  diii
[root@iZuf69tfiox41j76yf0416Z dir]# ls ..
123  dir  newdir  newdir2  tou2

剪切目录:把 lll 目录中的 dir 目录剪切到 newdir2 目录中:

[root@iZuf69tfiox41j76yf0416Z lll]# ls
123  dir  newdir  newdir2  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# mv dir newdir2
[root@iZuf69tfiox41j76yf0416Z lll]# ls
123  newdir  newdir2  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# ls newdir2
dir  test.c

2、重命名功能

语法:mv src(文件或者目录) newname(新的文件名或目录名)

 文件重命名:把 lll 目录中的文件 123 重命名为 tou1:

[root@iZuf69tfiox41j76yf0416Z lll]# ls
123  newdir  newdir2  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# mv 123 tou1
[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir  newdir2  tou1  tou2

目录重命名:把 lll 目录中的目录 newdir 重命名为 newname:

[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir  newdir2  tou1  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# mv newdir newname
[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir2  newname  tou1  tou2

3、剪切+重命名功能

语法:mv src(文件或者目录) dst(一定是目录)/newname(新的文件名或目录名)

 把 lll 目录中的文件 tou2 剪切到目录 newname 中,并重命名为 test.c:

[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir2  newname  tou1  tou2
[root@iZuf69tfiox41j76yf0416Z lll]# ls newname
dir  text1.c  tou1
[root@iZuf69tfiox41j76yf0416Z lll]# mv tou2 newname/test.c
[root@iZuf69tfiox41j76yf0416Z lll]# ls
newdir2  newname  tou1
[root@iZuf69tfiox41j76yf0416Z lll]# ls newname
dir  test.c  text1.c  tou1

echo 指令

语法:echo "字符串"

功能:把字符串打印出来

[root@iZuf69tfiox41j76yf0416Z lll]# echo "aaa bbb ccc"
aaa bbb ccc

echo 指令在打印字符串的时候,默认是在屏幕上打印的。如果在命令行中加一个 “>” 符号,会发现字符串不会再被打印在屏幕上。

这个 ">" 符号被称为 输出重定向 

[root@iZuf69tfiox41j76yf0416Z lll]# echo "aaa bbb ccc"
aaa bbb ccc
[root@iZuf69tfiox41j76yf0416Z lll]# echo "aaa bbb ccc" > mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# 

那么我们输入的字符串到底在哪里呢?这串字符被输入到了 ">" 符号之后的文件 mylog.txt 中,如果该指定文件不存在,那么系统会自动新建一个文件,并且为之命名。

 输出重定向的概念:

把本来应该写入到 “A文件” 中的文本写入到 “B文件” 之中。

就向上面的操作,echo 指令默认是把字符串在显示器上打印出来,但是因为被输出重定向,把字符串写入到了指定文件之中。

在显示器上打印,可以理解为向显示器写入,可以把显示器也看成一种 “文件” 。在我们写C程序的时候,获取数据 scanf,cin,都是从键盘上获取到的,我们的键盘也可以看成一种 “文件” 。

  读 与 写 都是文件的操作,所以我们可以把显示器与键盘都看成文件,实际上,我们可以得出结论:Linux 下一切皆文件

了解了输出重定向的概念之后,它的具体操作是什么样的呢?如果我们进行多次输出重定向会有什么样的结果呢?

[root@iZuf69tfiox41j76yf0416Z lll]# ls
mylog.txt  newdir2  newname  tou1
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
aaa bbb ccc
[root@iZuf69tfiox41j76yf0416Z lll]# echo "hello world" > mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
hello world
[root@iZuf69tfiox41j76yf0416Z lll]# echo "hahahaha" > mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
hahahaha
[root@iZuf69tfiox41j76yf0416Z lll]# echo "adgasdg" > mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
[root@iZuf69tfiox41j76yf0416Z lll]# 

可以看到,随着我们不断通过 输出重定向 向 mylog.txt 中写入数据,mylog.txt 中的内容不断被更新,所以 输出重定向 是一种 覆盖式写入 。更准确的说 输出重定向在写入之前,会先清空文件。


 接下来我们再学习另一种符号 ">>"

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
[root@iZuf69tfiox41j76yf0416Z lll]# echo "hello world" >> mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
hello world
[root@iZuf69tfiox41j76yf0416Z lll]# echo "adgasdg" >> mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
hello world
adgasdg
[root@iZuf69tfiox41j76yf0416Z lll]# echo "hahahaha" >> mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
hello world
adgasdg
hahahaha
[root@iZuf69tfiox41j76yf0416Z lll]# 

可以看到使用 ">>" 符号的输出重定向在写入时,不会清空文件。我们称 ">>" 符号为 追加重定向。不断的向目标文件新增内容。

cat 指令

语法: cat [选项] [文件]

功能:查看目标文件的内容

常用选项:
-b 对非空输出行编号
-n 对输出的所有行编号
-s 不输出多行空行

 使用 cat 指令可以直接把文本文件的内容打印出来。详细用法见上面 echo 指令中的操作。

接下来我们通过 cat 指令学习一种新的符号: "<" 符号。我们称之为 输入重定向

我们说 cat 指令在读取文件内容的时候,必须要在 cat 指令后跟上指定文件名,如果我们在 cat 指令后什么都不跟,那么就默认读取 “键盘文件” 上的内容,即我们输入什么就打印什么:

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt
adgasdg
hello world
adgasdg
hahahaha
[root@iZuf69tfiox41j76yf0416Z lll]# cat
asd
asd
hirrer
hirrer
holl
holl
whatever
whatever
^C
[root@iZuf69tfiox41j76yf0416Z lll]# 

Ctrl + c 停止。

而如果我们使用 输入重定向 "<" ,就会更改 cat 指令读取文本的对象:

[root@iZuf69tfiox41j76yf0416Z lll]# cat < mylog.txt
adgasdg
hello world
adgasdg
hahahaha
[root@iZuf69tfiox41j76yf0416Z lll]# 

cat 指令改为从 "<" 符号后面的文件中读取文本内容。

要注意 cat < mylog.txt cat mylog.txt 的输出结果相同,但是意义不同,cat mylog.txt 是直接读取mylog.txt 文件中的文本内容,而cat < mylog.txt 是把原本应该从 "键盘文件" 中读取文本内容 重定向为 从 mylog.txt 文件中读取文本内容。

cat -n [文件名]:对输出的所有行编号

[root@iZuf69tfiox41j76yf0416Z lll]# cat -n mylog.txt
     1	adgasdg
     2	hello world
     3	adgasdg
     4	hahahaha
[root@iZuf69tfiox41j76yf0416Z lll]# 

wc 指令

语法:wc [命令选项] [文件名]

功能:统计文件内容

常用选项:

-l   统计行数

-w 统计字数(英文单词)

-m 统计字符数量

[root@iZuf69tfiox41j76yf0416Z lll]# wc -l mylog.txt
4 mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# wc -l < mylog.txt
4
[root@iZuf69tfiox41j76yf0416Z lll]# 

more 指令

语法:more [选项][文件]

功能:more命令,功能类似 cat

常用选项:
-n 对输出的所有行编号
q 退出more

 当文件文本较短时,我们可以直接使用 cat 指令来打印文本内容,但是如果文件文本量较大,用 cat 指令打印就有可能出现刷屏的现象。所以我们需要使用 more 命令来查看文本内容。

首先,我们先使用 shell 脚本创建一个大文本文件:

[root@iZuf69tfiox41j76yf0416Z lll]# cnt=0; while [ $cnt -le 1000 ]; do echo "hello $cnt"; let cnt++; done > mylog.txt

 使用 more 指令来查看文本内容,会默认根据 屏幕大小 来显示文本内容:

[root@iZuf69tfiox41j76yf0416Z lll]# more mylog.txt

more 指令会从文本的第 0 行开始显示文本内容,显示到屏幕满了就会自动停止,再按 enter 键就可以 往下 逐行翻阅文本。

需要注意的是,more 指令只能向下翻阅,无法向上翻阅。

less 指令

说明:

  • less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。
  • less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup][pagedown] 等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!
  • 除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜。

语法:less [参数] [文件]

功能:less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载整个文件。

常用选项:

-i:  忽略搜索时的大小写
-N:显示每行的行号
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n: 重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
q:   quit

 less 指令除了比 more 指令多了向上翻阅的功能之外,还增加了搜索功能,在翻阅见面输入 /"字符串" 可以从当前位置 向下搜索 指定字符串,输入 ?"字符串" 则是 向上搜索 指定字符串。输入 n、N 则可以 重复 或 反向重复 前一个搜索。

例如,在文本中搜素 666 :

 输入 /666 向下查找 666。

 自动找到了 666 的位置。

在使用 less 指令翻阅文本时, 可以按 "↑"  "↓" 向上 或 向下 翻阅。

"q" 键退出翻阅。

head 指令

说明:

head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块, head 用来显示档案的开头至标准输出中,而 tail 想当然就是看档案的结尾。

语法:head [参数] [文件]

功能:head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。

常用选项:
-n<行数> 显示的行数

[root@iZuf69tfiox41j76yf0416Z lll]# head mylog.txt
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[root@iZuf69tfiox41j76yf0416Z lll]# 

默认显示前 10 行,我们可以增加命令选项 "-n" 来修改显示的行数:比如显示文本的前 5 行:

[root@iZuf69tfiox41j76yf0416Z lll]# head -5 mylog.txt
hello 0
hello 1
hello 2
hello 3
hello 4
[root@iZuf69tfiox41j76yf0416Z lll]# 

tail 指令

说明:

tail 命令从指定点开始将文件写到标准输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新,使你看到最新的文件内容。

语法:tail[必要参数][选择参数][文件]

功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

常用选项:

-f 循环读取
-n<行数> 显示行数

[root@iZuf69tfiox41j76yf0416Z lll]# tail mylog.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
[root@iZuf69tfiox41j76yf0416Z lll]# 

默认显示后 10 行,我们可以增加命令选项 "-n" 来修改显示的行数:比如显示文本的后 5 行:

[root@iZuf69tfiox41j76yf0416Z lll]# tail -5 mylog.txt
hello 996
hello 997
hello 998
hello 999
hello 1000
[root@iZuf69tfiox41j76yf0416Z lll]# 

管道符 |

如果我们想统计 mylog.txt 文件中的文本行数,我们有以下几种方法:

1、直接使用 wc -l 指令:

[root@iZuf69tfiox41j76yf0416Z lll]# wc -l mylog.txt
1001 mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# 

2、使用 cat 指令直接打印文件内容,来判断文本行数:

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt

 3、使用 管道符号 " | " 来显示文本行数:

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt | wc -l
1001
[root@iZuf69tfiox41j76yf0416Z lll]# 

那么 管道 到底是什么呢?

" | "是一个特殊符号,在Linux当中表示 管道 ,管道,顾名思义,是用来 传输资源 的,有入口也有出口。而我们计算机中最重要的资源是 数据 ,因此 管道 " | "可以从解释成 连接两条指令,用来传输数据的指令

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt | wc -l

以上面的命令行为例:cat mylog.txt 是一个指令,中间加了一个 "|" " | " 可以被看成一个文件,它会把 cat 指令 输出的所有的数据全部写入 管道 里,而后面的 wc -l 指令原本需要在指令后跟上文件名,来读取该文件以统计其行数,现在变成了从 管道 中把数据读取出来,再统计行数。

基本理解了管道之后,我们再来练习一下:

如果我们想要提取文件 mylog.txt 中的第 500 到第 520 行的文本内容,该如何操作呢?

首先第一个方法:我们先创建一个 临时文件 ,把 mylog.txt 中前 520 行文本放入临时文件中,再读取这个临时文件的后 20 行内容:

[root@iZuf69tfiox41j76yf0416Z lll]# ls
mylog.txt
[root@iZuf69tfiox41j76yf0416Z lll]# head -520 mylog.txt > tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# ls
mylog.txt  tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# tail -20 tmp.txt
hello 500
hello 501
hello 502
hello 503
hello 504
hello 505
hello 506
hello 507
hello 508
hello 509
hello 510
hello 511
hello 512
hello 513
hello 514
hello 515
hello 516
hello 517
hello 518
hello 519
[root@iZuf69tfiox41j76yf0416Z lll]# 

任务可以完成,但是需要创建临时文件。

第二个方法:使用 管道 来操作:

[root@iZuf69tfiox41j76yf0416Z lll]# head -520 mylog.txt | tail -20 
hello 500
hello 501
hello 502
hello 503
hello 504
hello 505
hello 506
hello 507
hello 508
hello 509
hello 510
hello 511
hello 512
hello 513
hello 514
hello 515
hello 516
hello 517
hello 518
hello 519
[root@iZuf69tfiox41j76yf0416Z lll]# ls

使用管道来操作,就不需要创建临时文件,所以管道最核心的意义在于:可以级联多条命令,让命令与命令组合,来完成批量化文本处理的任务

相信经过讲解,同学们对于管道应该有了较深的理解,那么同学们可以自行来理解一下以下指令:

[root@iZuf69tfiox41j76yf0416Z lll]# cat mylog.txt | head -520 | tail -20 | wc -l
20
[root@iZuf69tfiox41j76yf0416Z lll]# 

date 指令

date 语法: date [选项] [+FORMAT]

date 直接显示时间:date

date 指定格式显示时间:date  +%Y:%m:%d

在显示方面,使用者可以设定 欲显示 的格式,格式设定为 一个加号 后接 数个标记,其中常用的标记列表如下

%H : 小时(00~23)
%M : 分钟(00~59)
%S : 秒(00~61)
%X : 相当于 %H:%M:%S
%d : 日 (01~31)
%m : 月份 (01~12)
%Y : 完整年份 (0000~9999)
%F : 相当于 %Y-%m-%d

[root@iZuf69tfiox41j76yf0416Z lll]# date
Tue Dec 27 16:19:01 CST 2022
[root@iZuf69tfiox41j76yf0416Z lll]# date +%Y:%m-%d_%H:%M=%S
2022:12-27_16:22=49
[root@iZuf69tfiox41j76yf0416Z lll]# 

标记与标记之间的分隔符号是可以随便写的,比如我在 年 与 月 之间用的是 ":" 号,在 月 与 日 之间用的是 "-" 号,在 分 与 秒 之间用的是 "=" 号。

但是"+" 号 与 "%" 必不可少也不能变

时间戳

概念:

Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

 时间戳与时间的相互转化:

时间->时间戳: date +%s

时间戳->时间: date -d @时间戳

[root@iZuf69tfiox41j76yf0416Z lll]# date +%s
1672131132
[root@iZuf69tfiox41j76yf0416Z lll]# date -d @1672131132
Tue Dec 27 16:52:12 CST 2022
[root@iZuf69tfiox41j76yf0416Z lll]# 

扩展内容:

通过查找 3号手册,我们可以找到一个 time 函数,可以直接调用 time 函数:

[root@iZuf69tfiox41j76yf0416Z lll]# man 3 time

 注:nano 指令的用法我在之前的文章《Linux基本指令(一)》中有过讲解,这里不再赘述。

 我们 nano 一个文件,在文本中输入:

#include <stdio.h>

int main()
{ 
        printf("time: %u\n", (unsigned int)time(NULL));
        return 0;
}

保存退出,使用 gcc 指令编译该文件会生成一个名为 a.out 的可执行文件,我们运行该文件:

[root@iZuf69tfiox41j76yf0416Z lll]# ./a.out
time: 1672132436
[root@iZuf69tfiox41j76yf0416Z lll]# ./a.out
time: 1672132437
[root@iZuf69tfiox41j76yf0416Z lll]# ./a.out
time: 1672132439
[root@iZuf69tfiox41j76yf0416Z lll]# 

 可以打印当前的时间戳,我们在写C语言程序时,种下的随机数种子其实就是时间戳。

cal 指令

说明:

cal命令可以用来显示公历(阳历)日历。公历是现在国际通用的历法,又称格列历,通称阳历。 “阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”。

语法:cal [参数][月份][年份]
功能:用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份

常用选项:
-3 显示系统前一个月,当前月,下一个月的月历
-j  显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
-y 显示当前年份的日历

cal -j 指令: 

以今天 2022 年 12 月 27 日 为例:

[root@iZuf69tfiox41j76yf0416Z lll]# cal -j

 cal -j 指令显示出今天是2022年的第 361 天。

sort 指令

语法:sort [命令选项][文件]

功能:对文件文本内容按照为单位进行排序

排序规则:从左向右按照每一行的第一个字母(按ascll码值作比较)排序,谁的ascll码值小,(先比较每一行的第一个字母,如果相等,再比较第二个字母)就放在前面,默认是升序排列。

我们 nano 一个文件,命名为 my.txt,随便输入以下内容:

[root@iZuf69tfiox41j76yf0416Z lll]# cat my.txt
11111
222222
11111
222222
333333
444444
66666
77777
666666666

999999

8888888888
88888888
7777777
444444444444
33333333333
[root@iZuf69tfiox41j76yf0416Z lll]# 

sort my.txt :对文件 my.txt 的文本内容升序排序

[root@iZuf69tfiox41j76yf0416Z lll]# sort my.txt


11111
11111
222222
222222
333333
33333333333
444444
444444444444
66666
666666666
77777
7777777
88888888
8888888888
999999
[root@iZuf69tfiox41j76yf0416Z lll]# 

sort -r my.txt :对文件 my.txt 的文本内容降序排序

[root@iZuf69tfiox41j76yf0416Z lll]# sort -r my.txt
999999
8888888888
88888888
7777777
77777
666666666
66666
444444444444
444444
33333333333
333333
222222
222222
11111
11111


[root@iZuf69tfiox41j76yf0416Z lll]# 

uniq 指令

去重指令:可把相邻的、一样的文本行进行压缩,变为一行(删除重复的,只留下一行)

[root@iZuf69tfiox41j76yf0416Z lll]# sort my.txt | uniq

11111
222222
333333
33333333333
444444
444444444444
66666
666666666
77777
7777777
88888888
8888888888
999999
[root@iZuf69tfiox41j76yf0416Z lll]# 

搜索指令

find 指令

语法:find [路径] [命令选项] [查找目标]

功能:用于在文件树中查找文件,并作出相应的处理(可能访问磁盘)

常用选项:
-name 按照文件名查找文件

 例如:我在家目录中搜索一个名为 test.c 的文件:

[root@iZuf69tfiox41j76yf0416Z lll]# find ~ -name test.c
/root/lll/newname/test.c
/root/lll/newdir2/test.c
/root/lll/test.c
[root@iZuf69tfiox41j76yf0416Z lll]# 

find 指令会把 指定目录下 所有符合条件的 文件 或 目录 全部找到。

which 指令

语法:which [指令]

功能:搜索 "指令" 。

例如:

搜索 pwd 指令:

[root@iZuf69tfiox41j76yf0416Z lll]# which pwd
/usr/bin/pwd
[root@iZuf69tfiox41j76yf0416Z lll]#

搜索 ls 指令:

[root@iZuf69tfiox41j76yf0416Z lll]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@iZuf69tfiox41j76yf0416Z lll]# 

 补充内容:

alias 指令:起别名。

例如:

给 指令 ls -a -l -i -n 起别名叫做 myls:

[root@iZuf69tfiox41j76yf0416Z lll]# ls -a -l -i -n
total 48
1057415 drwxr-xr-x  5 0 0 4096 Dec 27 17:30 .
 917505 dr-xr-x---. 6 0 0 4096 Dec 26 15:33 ..
1057423 drwxr-xr-x  2 0 0 4096 Dec 18 16:41 .hide
1057296 -rw-r--r--  1 0 0 9901 Dec 26 17:56 mylog.txt
1057322 -rw-r--r--  1 0 0  124 Dec 27 17:36 my.txt
1057226 drwxr-xr-x  3 0 0 4096 Dec 26 16:04 newdir2
1057416 drwxr-xr-x  3 0 0 4096 Dec 26 16:18 newname
1057556 -rw-r--r--  1 0 0   95 Dec 27 17:09 test.c
1057320 -rw-r--r--  1 0 0 5090 Dec 27 15:35 tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# alias myls='ls -a -l -i -n'
[root@iZuf69tfiox41j76yf0416Z lll]# myls
total 48
1057415 drwxr-xr-x  5 0 0 4096 Dec 27 17:30 .
 917505 dr-xr-x---. 6 0 0 4096 Dec 26 15:33 ..
1057423 drwxr-xr-x  2 0 0 4096 Dec 18 16:41 .hide
1057296 -rw-r--r--  1 0 0 9901 Dec 26 17:56 mylog.txt
1057322 -rw-r--r--  1 0 0  124 Dec 27 17:36 my.txt
1057226 drwxr-xr-x  3 0 0 4096 Dec 26 16:04 newdir2
1057416 drwxr-xr-x  3 0 0 4096 Dec 26 16:18 newname
1057556 -rw-r--r--  1 0 0   95 Dec 27 17:09 test.c
1057320 -rw-r--r--  1 0 0 5090 Dec 27 15:35 tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# 

whereis 指令

语法:whereis [搜索目标]

功能:用来在系统默认路径下,搜索指定名称的文件、程序或者归档文件(比如压缩包)

例如:

搜索 my:

[root@iZuf69tfiox41j76yf0416Z lll]# whereis my
my: /etc/my.cnf
[root@iZuf69tfiox41j76yf0416Z lll]# 

搜索 ls:

[root@iZuf69tfiox41j76yf0416Z lll]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@iZuf69tfiox41j76yf0416Z lll]# 

不光搜索到了 ls 指令所在的位置,还搜索到了该程序所配套的 man 手册对应的内容。

补充内容:

指令就是文件,指令是C语言代码编译好之后,形成的可执行文件。

grep 指令

语法: grep [选项] [字符串] [文件]

功能: 在文件中搜索字符串,将找到的行打印出来

常用选项:
-i : 忽略大小写的不同,所以大小写视为相同
-n :顺便输出行号
-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行

例如:把 mylog.txt 文件中所有包含字符串 '88' 的行打印出来:

[root@iZuf69tfiox41j76yf0416Z lll]# grep -n '88' mylog.txt
89:hello 88
189:hello 188
289:hello 288
389:hello 388
489:hello 488
589:hello 588
689:hello 688
789:hello 788
881:hello 880
882:hello 881
883:hello 882
884:hello 883
885:hello 884
886:hello 885
887:hello 886
888:hello 887
889:hello 888
890:hello 889
989:hello 988
[root@iZuf69tfiox41j76yf0416Z lll]# 

top 指令

相当于 Windows 系统里的任务管理器:

[root@iZuf69tfiox41j76yf0416Z lll]# top

zip/unzip 指令

语法:zip [压缩文件.zip] [目录或文件]

功能: 将目录或文件压缩成zip格式

常用选项:
-r  递归处理,将指定目录下的所有文件和子目录一并处理

例如:

压缩打包 newname 目录,并把压缩文件命名为 newname.zip :

[root@iZuf69tfiox41j76yf0416Z lll]# ll
total 32
-rw-r--r-- 1 root root 9901 Dec 26 17:56 mylog.txt
-rw-r--r-- 1 root root  124 Dec 27 17:36 my.txt
drwxr-xr-x 3 root root 4096 Dec 26 16:18 newname
-rw-r--r-- 1 root root   95 Dec 27 17:09 test.c
-rw-r--r-- 1 root root 5090 Dec 27 15:35 tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# zip newname.zip newname
  adding: newname/ (stored 0%)
[root@iZuf69tfiox41j76yf0416Z lll]# ll
total 36
-rw-r--r-- 1 root root 9901 Dec 26 17:56 mylog.txt
-rw-r--r-- 1 root root  124 Dec 27 17:36 my.txt
drwxr-xr-x 3 root root 4096 Dec 26 16:18 newname
-rw-r--r-- 1 root root  166 Dec 27 19:31 newname.zip
-rw-r--r-- 1 root root   95 Dec 27 17:09 test.c
-rw-r--r-- 1 root root 5090 Dec 27 15:35 tmp.txt
[root@iZuf69tfiox41j76yf0416Z lll]# 

把 newname.zip 文件剪切到上层目录,再使用 unzip 命令解压:

[root@iZuf69tfiox41j76yf0416Z lll]# mv newname.zip ..
[root@iZuf69tfiox41j76yf0416Z lll]# cd ..
[root@iZuf69tfiox41j76yf0416Z ~]# ll
total 8
drwxr-xr-x 4 root root 4096 Dec 27 19:35 lll
-rw-r--r-- 1 root root  166 Dec 27 19:31 newname.zip
[root@iZuf69tfiox41j76yf0416Z ~]# unzip newname.zip
Archive:  newname.zip
   creating: newname/
[root@iZuf69tfiox41j76yf0416Z ~]# ll
total 12
drwxr-xr-x 4 root root 4096 Dec 27 19:35 lll
drwxr-xr-x 2 root root 4096 Dec 26 16:18 newname
-rw-r--r-- 1 root root  166 Dec 27 19:31 newname.zip
[root@iZuf69tfiox41j76yf0416Z ~]# tree newname
newname

0 directories, 0 files
[root@iZuf69tfiox41j76yf0416Z ~]# 

可以看到我们解压出来的 newname 目录是空目录,里面什么都没有。这是因为我们打包时使用的指令是 zip 指令,没有加命令选项 -r 。默认只打包了空目录,没有打包内容。


下面我们把刚刚打包、转移、解包操作产生的文件都删掉,使用 zip -r 指令再操作一次:

[root@iZuf69tfiox41j76yf0416Z lll]# zip -r newname.zip newname
  adding: newname/ (stored 0%)
  adding: newname/dir/ (stored 0%)
  adding: newname/dir/456 (stored 0%)
  adding: newname/dir/123 (stored 0%)
  adding: newname/tou1 (stored 0%)
  adding: newname/text1.c (stored 0%)
  adding: newname/test.c (stored 0%)
[root@iZuf69tfiox41j76yf0416Z lll]# mv newname.zip ..
[root@iZuf69tfiox41j76yf0416Z lll]# cd ..
[root@iZuf69tfiox41j76yf0416Z ~]# unzip newname.zip
Archive:  newname.zip
   creating: newname/
   creating: newname/dir/
 extracting: newname/dir/456         
 extracting: newname/dir/123         
 extracting: newname/tou1            
 extracting: newname/text1.c         
 extracting: newname/test.c          
[root@iZuf69tfiox41j76yf0416Z ~]# tree newname
newname
├── dir
│   ├── 123
│   └── 456
├── test.c
├── text1.c
└── tou1

1 directory, 5 files
[root@iZuf69tfiox41j76yf0416Z ~]# 

可以看到,这一次 newname 目录里的所有目录与文件都被转移过来了。


我们使用 unzip 指令对压缩包进行解压的时候,默认是解压到当前目录,如果我们想要解压到指定目录,需要添加命令选项 -d

unzip [压缩文件.zip] [-d] [指定目录]

 例如:

把 newname.zip 解压到新建的目录 mydir 里:

[root@iZuf69tfiox41j76yf0416Z ~]# unzip newname -d mydir
[root@iZuf69tfiox41j76yf0416Z ~]# ll
total 12
drwxr-xr-x 4 root root 4096 Dec 27 19:41 lll
drwxr-xr-x 3 root root 4096 Dec 26 16:18 newname
-rw-r--r-- 1 root root 1100 Dec 27 19:41 newname.zip
[root@iZuf69tfiox41j76yf0416Z ~]# mkdir mydir
[root@iZuf69tfiox41j76yf0416Z ~]# unzip newname -d mydir
Archive:  newname.zip
   creating: mydir/newname/
   creating: mydir/newname/dir/
 extracting: mydir/newname/dir/456   
 extracting: mydir/newname/dir/123   
 extracting: mydir/newname/tou1      
 extracting: mydir/newname/text1.c   
 extracting: mydir/newname/test.c    
[root@iZuf69tfiox41j76yf0416Z ~]# tree mydir
mydir
└── newname
    ├── dir
    │   ├── 123
    │   └── 456
    ├── test.c
    ├── text1.c
    └── tou1

2 directories, 5 files
[root@iZuf69tfiox41j76yf0416Z ~]# 

以上就是本篇文章的全部内容,内容比较多,希望大家多多支持,如果有不妥的地方欢迎大佬指正!

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世间是否此山最高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值