前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux系统 (二)- 指令学习2

Linux系统 (二)- 指令学习2

作者头像
怠惰的未禾
发布2023-04-27 21:54:15
2.4K0
发布2023-04-27 21:54:15
举报
文章被收录于专栏:Linux之越战越勇Linux之越战越勇

前言

本节继续基本指令的学习!


mkdir

基本语法

代码语言:javascript
复制
mkdir [选项] dirname

功能

在当前目录创建一个名为"dirname"的目录;

创建目录举例

代码语言:javascript
复制
[root@VM-4-17-centos dir]# pwd
/root/dir
[root@VM-4-17-centos dir]# ls
test1.txt  test2.txt  test3.txt
[root@VM-4-17-centos dir]# mkdir dir1
[root@VM-4-17-centos dir]# mkdir dir2
[root@VM-4-17-centos dir]# mkdir dir3
[root@VM-4-17-centos dir]# ls
dir1  dir2  dir3  test1.txt  test2.txt  test3.txt
image.png
image.png

选项 -p

功能

即一次可以建立多个目录 前一个目录是后一个目录的父目录,不是同级关系

代码语言:javascript
复制
[root@VM-4-17-centos dir]# pwd
/root/dir
[root@VM-4-17-centos dir]# ls
test1.txt  test2.txt  test3.txt
[root@VM-4-17-centos dir]# mkdir -p dir1/dir2/dir3/dir4
[root@VM-4-17-centos dir]# ls
dir1  test1.txt  test2.txt  test3.txt
[root@VM-4-17-centos dir]# tree
.
|-- dir1
|   `-- dir2
|       `-- dir3
|           `-- dir4
|-- test1.txt
|-- test2.txt
`-- test3.txt

4 directories, 3 files
image.png
image.png

rmdir

基本语法

代码语言:javascript
复制
rmdir [选项] dirname

功能

删除空目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# mkdir dir1
[root@VM-4-17-centos dir]# mkdir -p dir2/dir3
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
|   `-- dir3
|-- test1.txt
|-- test2.txt
`-- test3.txt

3 directories, 3 files
[root@VM-4-17-centos dir]# rmdir dir1
[root@VM-4-17-centos dir]# tree
.
|-- dir2
|   `-- dir3
|-- test1.txt
|-- test2.txt
`-- test3.txt

2 directories, 3 files
image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree
.
|-- dir2
|   `-- dir3
|-- test1.txt
|-- test2.txt
`-- test3.txt

2 directories, 3 files
[root@VM-4-17-centos dir]# rmdir dir2
rmdir: failed to remove ‘dir2’: Directory not empty
image.png
image.png

-p选项

**删除空目录,当子目录被删除后如果父目录也变成空目录,带父目录也一起删除 **

代码语言:javascript
复制
[root@VM-4-17-centos dir]# mkdir -p a/b/c
[root@VM-4-17-centos dir]# tree .
.
|-- a
|   `-- b
|       `-- c
|-- test1.txt
|-- test2.txt
`-- test3.txt

3 directories, 3 files
[root@VM-4-17-centos dir]# rmdir -p a/b/c
[root@VM-4-17-centos dir]# tree .
.
|-- test1.txt
|-- test2.txt
`-- test3.txt

0 directories, 3 files
image.png
image.png

rm

基本语法

代码语言:javascript
复制
rm [选项] name

功能

默认删除一个普通文件,在删除之前系统会询问我们释放删除Y(y)/N(n)

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
|   `-- dir3
|-- test1.txt
|-- test2.txt
`-- test3.txt

3 directories, 3 files
[root@VM-4-17-centos dir]# rm test1.txt 
rm: remove regular empty file ‘test1.txt’? y
[root@VM-4-17-centos dir]# rm test2.txt 
rm: remove regular empty file ‘test2.txt’? y	
[root@VM-4-17-centos dir]# rm test3.txt
rm: remove regular empty file ‘test3.txt’? y
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
`-- dir2
    `-- dir3

3 directories, 0 files
image.png
image.png

选项 -r

递归删除所选目录及其所有子目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree
.
|-- dir1
`-- dir2
    `-- dir3
        `-- dir4

4 directories, 0 files
[root@VM-4-17-centos dir]# rm -r dir1
rm: remove directory ‘dir1’? y
[root@VM-4-17-centos dir]# rm -r dir2
rm: descend into directory ‘dir2’? y
rm: descend into directory ‘dir2/dir3’? y
rm: remove directory ‘dir2/dir3/dir4’? y
rm: remove directory ‘dir2/dir3’? y
rm: remove directory ‘dir2’? y
[root@VM-4-17-centos dir]# tree .
.

0 directories, 0 files
image.png
image.png

选项 -f

删除时不让系统询问我,而是直接删除

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree
.
|-- dir1
|   `-- dir2
|       `-- dir3
|-- dir4
`-- test.txt

4 directories, 1 file
[root@VM-4-17-centos dir]# rm -f test.txt
[root@VM-4-17-centos dir]# rm -rf dir1
[root@VM-4-17-centos dir]# tree .
.
`-- dir4

1 directory, 0 files
[root@VM-4-17-centos dir]# rm -rf dir4
[root@VM-4-17-centos dir]# tree
.

0 directories, 0 files
image.png
image.png

选项 -i

删除普通文件或目录时询问用户是否删除,由用户再次确认是否删除

防止用户的误操作导致某些文件或目录被删除

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
|   `-- dir3
|       `-- dir4
`-- test.txt

4 directories, 1 file
[root@VM-4-17-centos dir]# rm -ri test.txt 
rm: remove regular empty file ‘test.txt’? y
[root@VM-4-17-centos dir]# rm -r -i dir1
rm: remove directory ‘dir1’? y
[root@VM-4-17-centos dir]# tree
.
`-- dir2
    `-- dir3
        `-- dir4

3 directories, 0 files
image.png
image.png

stat

stat filename 列出文件三种时间属性

image.png
image.png

man

基本语法

代码语言:javascript
复制
man [选项] 命令

功能

访问Linux在线手册查找对应命令的文档

查询C语言printf函数

代码语言:javascript
复制
[root@VM-4-17-centos ~]# man printf
image.png
image.png

在线手册介绍

查询man命令

代码语言:javascript
复制
[root@VM-4-17-centos dir]# man man

在线手册分为9章,每一章包含不同功能的文档; 1号手册包含可执行程序和命令 2号手册包含系统调用 3号手册包含库函数,是C语言的库函数 4号手册包含特别文件 5号手册文件格式 6号手册游戏相关 7号手册杂项,包括附件和一些变量 8号手册系统管理相关 9号手册内核例程

man 查找命令默认从1号手册开始,顺序查找所有手册直到找到为止;

image.png
image.png

选项 number

指定在number手册中查找命令

已知pwd在1号手册,那么在其他手册中将找不到pwd命令

image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# man pwd
[root@VM-4-17-centos dir]# man 1 pwd
[root@VM-4-17-centos dir]# man 2 pwd
No manual entry for pwd in section 2
[root@VM-4-17-centos dir]# man 3 pwd
No manual entry for pwd in section 3
image.png
image.png

选项 -k

根据关键字(命令)搜索联机帮助

代码语言:javascript
复制
[root@VM-4-17-centos dir]# man -k pwd
lckpwdf (3)          - get shadow password file entry
pwd (1)              - print name of current/working directory
pwd (1p)             - return working directory name
pwd (n)              - Return the absolute path of the current working directory
pwd.h (0p)           - password structure
pwdx (1)             - report current working directory of a process
ulckpwdf (3)         - get shadow password file entry
unix_chkpwd (8)      - Helper binary that verifies the password of the current user
image.png
image.png

选项 -a

将所有章节显示出来,man将搜索所有章节而不是找到命令就停止搜索,按下q推出文档时未到末尾还会继续搜索

代码语言:javascript
复制
[root@VM-4-17-centos dir]# man -a printf
--Man-- next: printf(1p) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
image.png
image.png

cp

基本语法

代码语言:javascript
复制
cp [选项] 源文件/目录 目标文件/目录

功能

复制源文件或目录到目标文件或目录中

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
`-- test.txt

2 directories, 1 file
[root@VM-4-17-centos dir]# cp test.txt ./dir1
[root@VM-4-17-centos dir]# cp test.txt ./dir2
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|   `-- test.txt
|-- dir2
|   `-- test.txt
`-- test.txt
image.png
image.png

选项 - i

覆盖文件之前先询问用户

代码语言:javascript
复制
[root@VM-4-17-centos dir]# ls
dir1  dir2  text1.txt  text2.txt  text3.txt
[root@VM-4-17-centos dir]# cp -i text1.txt text2.txt 
cp: overwrite ‘text2.txt’? y
image.png
image.png

选项 -f/–force

复制文件或目录到目标文件或目录,不论目标文件或目录是否已经存在 目标文件或目录不存在就新建一个文件或目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cp -f text1.txt cp1.txt
[root@VM-4-17-centos dir]# ls
cp1.txt  dir1  dir2  text1.txt  text2.txt  text3.txt
image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# cp --force text1.txt cp2.txt
[root@VM-4-17-centos dir]# ls
cp1.txt  cp2.txt  dir1  dir2  text1.txt  text2.txt  text3.txt
image.png
image.png

选项 -r/-R/–recursive

递归处理指定目录下的文件与子目录,若源文件或目录的形态不属于目录或符号链接,就当做普通文件处理;

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cp -r dir1 dircp
[root@VM-4-17-centos dir]# ls
cp1.txt  cp2.txt  dir1  dir2  dircp  text1.txt  text2.txt  text3.txt
image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# cp -R dir1 dircp2
[root@VM-4-17-centos dir]# ls
dir1  dir2  dircp  dircp2  text1.txt  text2.txt  text3.txt
image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# cp --recursive dir1 dircp3
[root@VM-4-17-centos dir]# ls
dir1  dir2  dircp  dircp2  dircp3  text1.txt  text2.txt  text3.txt
image.png
image.png


mv

基本语法

代码语言:javascript
复制
mv [选项] 源文件/目录 目标文件/目录

功能

mv命令中第二个参数是文件时,完成对源文件/目录的重命名; mv命令中第二个参数是已存在的目录时,mv把源文件/目录 移动到目标目录中,等价于windows中剪切+复制

重命名

代码语言:javascript
复制
[root@VM-4-17-centos dir]# ls
dir1  dir2  text1.txt  text2.txt  text3.txt
[root@VM-4-17-centos dir]# mv text1.txt name.txt
[root@VM-4-17-centos dir]# ls
dir1  dir2  name.txt  text2.txt  text3.txt
[root@VM-4-17-centos dir]# mv dir1 newdir1
[root@VM-4-17-centos dir]# ls
dir2  name.txt  newdir1  text2.txt  text3.txt
image.png
image.png

移动文件或目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# ls
dir1  dir2  text1.txt  text2.txt  text3.txt
[root@VM-4-17-centos dir]# mv text1.txt text2.txt 
mv: overwrite ‘text2.txt’? y
[root@VM-4-17-centos dir]# ls
dir1  dir2  text2.txt  text3.txt
image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# ls
dir1  dir2  text1.txt  text2.txt  text3.txt
[root@VM-4-17-centos dir]# mv text1.txt dir1
[root@VM-4-17-centos dir]# tree
.
|-- dir1
|   `-- text1.txt
|-- dir2
|-- text2.txt
`-- text3.txt

2 directories, 3 file
image.png
image.png

选项 -f

如果目标文件已经存在,不再询问而是直接覆盖目标文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
|-- text1.txt
|-- text2.txt
`-- text3.txt

2 directories, 3 files
[root@VM-4-17-centos dir]# mv -f text1.txt text2.txt 
[root@VM-4-17-centos dir]# ls
dir1  dir2  text2.txt  text3.txt

选项 -i

如果目标文件已经存在就询问用户是否覆盖目标文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tree .
.
|-- dir1
|-- dir2
|-- text2.txt
`-- text3.txt

2 directories, 2 files
[root@VM-4-17-centos dir]# mv -i text2.txt text3.txt 
mv: overwrite ‘text3.txt’? y
[root@VM-4-17-centos dir]# ls
dir1  dir2  text3.txt

cat

基本语法

代码语言:javascript
复制
cat [选项] 文件

功能

查看文件的内容 cat会把文件所有内容都显示出来,不适合看大文本

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat text1.txt 
hello world!
hello world!
image.png
image.png

选项 -n

对输出的所有行进行编号

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat -n text1.txt 
     1	hello world!
     2	hello world!
     3	hello world!
     4	
     5	hello world!
     6	hello world!
     7	hello world!
     8	
     9	
    10	hello world!
    11	hello world!
    12	
    13	hello world!
    14	hello world!
    15	hello world!
    16	hello world!
    17	hello

选项 -b

对非空行进行编号

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat -b text1.txt 
     1	hello world!
     2	hello world!
     3	hello world!

     4	hello world!
     5	hello world!
     6	hello world!


     7	hello world!
     8	hello world!

     9	hello world!
    10	hello world!
    11	hello world!
    12	hello world!
    13	hello

选项 -s

不输出多行相邻的空行,即空行不连续出现

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat -s text1.txt 
hello world!
hello world!
hello world!

hello world!
hello world!
hello world!

hello world!
hello world!

hello world!
hello world!
hello world!
hello world!
hello

tac

基本语法

代码语言:javascript
复制
tac [选项] 文件

功能

倒着显示文件内容,与cat指令正好相反

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tac text1.txt 
hello 
hello world!
hello world!
hello world!
hello world!

hello world!
hello world!


hello world!
hello world!
hello world!

hello world!
hello world!
hello world!

more

基本语法

代码语言:javascript
复制
more [选项] 文件

功能

显示文件内容,按q退出或文件内容查看完自动退出

代码语言:javascript
复制
[root@VM-4-17-centos dir]# more text1.txt

按enter键可以向下翻阅文件内容,但是不能向上翻阅,还是不太方便

image.png
image.png

在查看文件内容时还可以搜索某一内容以/开始的文本内容

代码语言:javascript
复制
hello world!
hello world!
hello world!
hello world!
/weihe
image.png
image.png

选项 -n

输出前n行内容

代码语言:javascript
复制
[root@VM-4-17-centos dir]# more -5 text1.txt 
hello world!
hello world!

hello world!
hello world!
--More--(6%)
image.png
image.png

less

基本语法

代码语言:javascript
复制
less [选项] 文件

功能

对文件或其他输出分页显示 与more类似,但比more好用,less在查看之前不会加载整个文件; less工具在Linux中重要且强大,支持向前向后翻阅文件快捷键pageup/pagedown或四个方向键; 支持向前向后搜索以/或?开始的文本内容**;**

代码语言:javascript
复制
[root@VM-4-17-centos dir]# less text1.txt
image.png
image.png

选项 -N

显示所有行的行号

代码语言:javascript
复制
[root@VM-4-17-centos dir]# less -N text1.txt 

选项 -i

忽略搜索时的大小写

代码语言:javascript
复制
[root@VM-4-17-centos dir]# less -i text1.txt
image.png
image.png

选项 /字符串

向前搜索

image.png
image.png

选项 ?字符串

向后搜索

image.png
image.png

n

重复前一个搜索

N

反向重复前一个搜索


head

基本语法

代码语言:javascript
复制
head [选项] 文件

功能

显示文件内容的开头至标准输出stdout默认输出文件开始的前十行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# head text1.txt 
hello world!
HELLO WORLD!
HELLO WORLD!

hello world!
hello world!
hello world!
hello world!
haha
HAHA

选项 -n

指定输出文件的前n行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# head -5 text1.txt 
hello world!
HELLO WORLD!
HELLO WORLD!

hello world!

tail

基本语法

代码语言:javascript
复制
tail [选项] 文件

功能

显示指定文件末尾内容到标准输出stdout,不指定文件时,作为输入信息进行处理。常用查看日志文件 默认输出后十行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tail text1.txt 
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!

选项 -n<行数>

指定输出文件的前n行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tail -n5 text1.txt 
hello world!
hello world!
hello world!
hello world!
hello world!

选项 -f

输出文件末尾内容,并不断刷新,即循环输出

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tail -f text1.txt 
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!

重定向

输出重定向 >

基本语法

代码语言:javascript
复制
命令 > 文件

功能

将输出重定向到文件,文件存在就覆盖,文件不存在就创建文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# echo "you can see me" >  newfile.txt
image.png
image.png

输入重定向 <

基本语法

代码语言:javascript
复制
命令 < 文件

功能

将输入重定向到文件,是覆盖式的写入已存在的文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# echo "you can not see me" < newfile.txt
you can not see me

追加 >>

基本语法

代码语言:javascript
复制
命令 >> 文件

功能

将输出以追加的方式重定向到文件中

代码语言:javascript
复制
[root@VM-4-17-centos dir]# echo hello world! >> newfile.txt 
[root@VM-4-17-centos dir]# cat newfile.txt 
you can see me
hello world!

标准输入/标准输出/标准错误

Linux运行时会默认打开三个文件:

标准输入 stdin,程序默认向stdin读取数据; 标准输出 stdout默认向stdout输出数据; 标准错误 stderr,向stderr写入错误信息;

>stdout重定向到文件,<stdin重定向到文件;


管道 |

功能

管道用于数据传输,将|前面命令的输出作为|后面的输入

对于文件中的内容输出前五行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat text1.txt | head -5
hello world!
HELLO WORLD!
HELLO WORLD!

hello world!

date

基本语法

代码语言:javascript
复制
date [选项] [+格式]

功能

显示时间

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date
Sun Oct 23 12:36:06 CST 2022

指定格式显示时间

%H : 小时(00~23) %M : 分钟 %S : 秒 %X : 相当于 %H:%M:%S %d : 日 %m : 月份 %Y : 完整年份 **%F : 相当于 %Y-%m-%d **

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date +%Y/%m/%d-%H:%M:%S
2022/10/23-12:40:21
代码语言:javascript
复制
[root@VM-4-17-centos dir]# date +%F--%X
2022-10-23--12:41:22 PM

时间戳

%s 时间戳指的是从格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数;

获取时间戳

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date +%s
1666500176

时间戳转换为具体日期 -d

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date -d @1666500176
Sun Oct 23 12:42:56 CST 2022

时间戳转换指定格式

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date -d @1666500176 +%Y/%m/%d-%H:%M:%S
2022/10/23-12:42:56

设置时间 -s

root权限才能设置

代码语言:javascript
复制
[root@VM-4-17-centos dir]# date -s"2022-10-24 12:56:01"
Mon Oct 24 12:56:01 CST 2022

cal

基本语法

代码语言:javascript
复制
cal [选项][月份][年份]

功能

查看日历等时间信息,只有一个参数表示年份;两个参数表示月份和年数 表示的否是公历,又称格列历和阳历。

代码语言:javascript
复制
[root@VM-4-17-centos ~]# cal
    October 2022    
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
代码语言:javascript
复制
[root@VM-4-17-centos ~]# cal 2022
                               2022                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31
代码语言:javascript
复制
[root@VM-4-17-centos ~]# cal 10 2022
    October 2022    
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

选项 -3

显示系统当前月、前一个月和后一个月;

代码语言:javascript
复制
[root@VM-4-17-centos ~]# cal -3
   September 2022         October 2022          November 2022   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
             1  2  3                     1         1  2  3  4  5
 4  5  6  7  8  9 10   2  3  4  5  6  7  8   6  7  8  9 10 11 12
11 12 13 14 15 16 17   9 10 11 12 13 14 15  13 14 15 16 17 18 19
18 19 20 21 22 23 24  16 17 18 19 20 21 22  20 21 22 23 24 25 26
25 26 27 28 29 30     23 24 25 26 27 28 29  27 28 29 30         
                      30 31

选项 -j

显式一年中的第几天

代码语言:javascript
复制
[root@VM-4-17-centos ~]# cal -j
        October 2022       
Sun Mon Tue Wed Thu Fri Sat
                        274
275 276 277 278 279 280 281
282 283 284 285 286 287 288
289 290 291 292 293 294 295
296 297 298 299 300 301 302
303 304

选项 -y

显式年份的12个月的日历

代码语言:javascript
复制
[root@VM-4-17-centos ~]#cal -y 2022

find

基本语法

代码语言:javascript
复制
find 文件名 选项

功能

在文件树中查找文件,并做出相应操作

代码语言:javascript
复制
[root@VM-4-17-centos dir]# find -name test.txt 
./test.txt
./dir2/test.txt
./dir1/test.txt
代码语言:javascript
复制
[root@VM-4-17-centos dir]# find /root/dir/ -name test.txt 
/root/dir/test.txt
/root/dir/dir2/test.txt
/root/dir/dir1/test.txt

find也有许多其他的操作


whereis

基本语法

代码语言:javascript
复制
whereis [选项] 指令名或文件名

功能

在特定路径下查找指定的文件名对应的指令或文档所在的位置

代码语言:javascript
复制
[root@VM-4-17-centos dir]# whereis test.txt
test: /usr/bin/test /usr/share/man/man1/test.1.gz /usr/share/man/man1p/test.1p.gz

wc

基本语法

代码语言:javascript
复制
wc [选项] 文件名

功能

统计行数

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat test.txt 
1111
22221111
1111
1111
hello
HELLO
heLLo
代码语言:javascript
复制
[root@VM-4-17-centos dir]# wc -l test.txt 
7 test.txt

grep

基本语法

代码语言:javascript
复制
grep [选项] 某个字符串 待查找文件 

功能

在文件中搜索字符串,并将找到的字符串打印出来;找不到就什么也不做

代码语言:javascript
复制
[root@VM-4-17-centos dir]# grep '1111' test.txt 
1111
1111
1111
1111
1111
1111
1111
1111

选项 -i

忽略字母大小写

代码语言:javascript
复制
[root@VM-4-17-centos dir]# grep -i 'hello' test.txt 
hello
HELLO
heLLo

选项 -n

输出行号

代码语言:javascript
复制
[root@VM-4-17-centos dir]# grep -i -n 'hello' test.txt 
21:hello
22:HELLO
23:heLLo

选项 -v

反向选择

代码语言:javascript
复制
[root@VM-4-17-centos dir]# grep -v -n 'hello' test.txt 
1:1111
2:22221111
3:1111
4:1111
6:HELLO
7:heLLo

与 | 结合使用

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat test.txt 
1111
22221111
1111
1111
hello
HELLO
heLLo
代码语言:javascript
复制
[root@VM-4-17-centos dir]# grep '1111' test.txt | wc -l
4

#uniq

基本语法

代码语言:javascript
复制
nuiq [选项] 文件

功能

对文件内容去重,只去除相邻的重复行

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat test.txt 
1111
2222
2222
1111
1111
4444
4444
1111
4444
代码语言:javascript
复制
[root@VM-4-17-centos dir]# uniq test.txt 
1111
2222
1111
4444
1111
4444
在这里插入图片描述
在这里插入图片描述

sort

基本语法

代码语言:javascript
复制
sort [选项] 文件

功能

对文件每一行的每一个字符以ASCII值进行排序,默认从上到下升序排列

代码语言:javascript
复制
[root@VM-4-17-centos dir]# sort test.txt 
1111
1111
1111
1111
1234
1324
2222
2222
4444
4444
4444
abcd
bcde

于是我们可以先排序再去重

代码语言:javascript
复制
[root@VM-4-17-centos dir]# cat test.txt 
1111
2222
2222
1111
1111
4444
4444
1111
4444
1234
1324
abcd
bcde
代码语言:javascript
复制
[root@VM-4-17-centos dir]# sort test.txt | uniq
1111
1234
1324
2222
4444
abcd
bcde

zip/unzip

打包和压缩的目的

便于对文件进行传输和保存

zip

基本语法

代码语言:javascript
复制
tar [选项] 压缩后文件名.zip 待压缩文件

功能

压缩普通文件或目录(不包括目录里的内容)

代码语言:javascript
复制
[root@VM-4-17-centos dir]# ls
dir1  dir2  test.txt
[root@VM-4-17-centos dir]# zip dir1.zip dir1
  adding: dir1/ (stored 0%)
[root@VM-4-17-centos dir]# ls
dir1  dir1.zip  dir2  test.txt
代码语言:javascript
复制
[root@VM-4-17-centos dir]# zip test.zip test.txt 
  adding: test.txt (deflated 29%)
[root@VM-4-17-centos dir]# ls
dir1  dir1.zip  dir2  dir3  dir4  test.txt  test.zip

选项 -r

递归压缩目录,压缩的是目录及其所有子目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# zip -r dir2.zip dir2
  adding: dir2/ (stored 0%)
  adding: dir2/test.txt (deflated 29%)

unzip

基本语法

代码语言:javascript
复制
unzip 压缩文件名.zip 解压后文件名

功能

在当前目录解压文件

image.png
image.png
代码语言:javascript
复制
[root@VM-4-17-centos dir]# unzip dir2.zip 
Archive:  dir2.zip
   creating: dir2/
  inflating: dir2/test.txt
image.png
image.png

选项 -d

解压到指定目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# unzip dir2.zip -d ./dir3/
Archive:  dir2.zip
   creating: ./dir3/dir2/
  inflating: ./dir3/dir2/test.txt 
image.png
image.png

tar

基本语法

代码语言:javascript
复制
tar [选项] 文件/目录

功能

打包/解包,预览查看压缩包内容


选项 -c

建立一个压缩文件create

选项 -t

查看tarfile里的文件

选项 -z

使用gzip方式压缩

image.png
image.png

打包dir1目录并压缩打包后的文件为dir1.tgz

以.tgz后缀结尾的是压缩文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tar -czf dir1.tgz dir1
image.png
image.png

选项 -j

使用bzip2压缩

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tar -cjf dir2.tgz dir2
image.png
image.png

选项 -v

压缩文件过程中显式文件

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tar -czvf dir2.tgz dir2
dir2/
dir2/test.txt
image.png
image.png

选项 -f

使用档名,f之后不能再加参数

选项 -x

解开一个压缩文件,默认解压解包到当前目录

代码语言:javascript
复制
[root@VM-4-17-centos dir3]# tar -xzvf dir1.tgz 
dir1/
dir1/test.txt
image.png
image.png

选项 -C

解压到指定目录

代码语言:javascript
复制
[root@VM-4-17-centos dir]# tar -xzvf dir2.tgz -C ./dir4
image.png
image.png

bc

功能

进行浮点运算

代码语言:javascript
复制
[root@VM-4-17-centos dir4]# bc
image.png
image.png

uname

基本语法

代码语言:javascript
复制
uname [选项] 

功能

获取电脑和操作系统相关的信息,默认只输出简单信息

代码语言:javascript
复制
[root@VM-4-17-centos dir4]# uname
Linux

选项 -r

输出内核名称

代码语言:javascript
复制
[root@VM-4-17-centos dir4]# uname -r
3.10.0-1160.71.1.el7.x86_64

选项 -a/–all

输出详细信息,依次输出内核名称,主机名,内核版本号,内核版本,硬件名,处理器类 型,硬件平台类型,操作系统名称

代码语言:javascript
复制
[root@VM-4-17-centos dir4]# uname -a
Linux VM-4-17-centos 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
代码语言:javascript
复制
[root@VM-4-17-centos dir4]# uname --all
Linux VM-4-17-centos 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

lscpu

查看cpu信息

代码语言:javascript
复制
[root@VM-4-17-centos dir]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 94
Model name:            Intel(R) Xeon(R) Gold 6133 CPU @ 2.50GHz
Stepping:              3
CPU MHz:               2494.140
BogoMIPS:              4988.28
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              4096K
L3 cache:              28160K
NUMA node0 CPU(s):     0,1
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single fsgsbase bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 arat

lsmem

查看内存

代码语言:javascript
复制
[root@VM-4-17-centos dir]# lsmem
RANGE                                 SIZE  STATE REMOVABLE BLOCK
0x0000000000000000-0x000000007fffffff   2G online        no  0-15

Memory block size:       128M
Total online memory:       2G
Total offline memory:      0B

df

基本语法

代码语言:javascript
复制
df [选项] 文件

功能

显示文件所在的文件系统的信息,默认显示所有文件系统的信息

代码语言:javascript
复制
[root@VM-4-17-centos dir]# df test.txt 
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       41152716 3978024  35396868  11% /
代码语言:javascript
复制
[root@VM-4-17-centos dir]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs         1012336       0   1012336   0% /dev
tmpfs            1023340      24   1023316   1% /dev/shm
tmpfs            1023340     540   1022800   1% /run
tmpfs            1023340       0   1023340   0% /sys/fs/cgroup
/dev/vda1       41152716 3977912  35396980  11% /
tmpfs             204672       0    204672   0% /run/user/0

选项 -h

以我们可读的格式打印磁盘文件系统信息

代码语言:javascript
复制
[root@VM-4-17-centos dir]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        989M     0  989M   0% /dev
tmpfs          1000M   24K 1000M   1% /dev/shm
tmpfs          1000M  540K  999M   1% /run
tmpfs          1000M     0 1000M   0% /sys/fs/cgroup
/dev/vda1        40G  3.8G   34G  11% /
tmpfs           200M     0  200M   0% /run/user/0

热键相关

tab

命令补齐和档案补齐功能,非常方便

CTRL+c

停止当前程序

Ctrl+d

表示键盘输入结束End Of File或End Of Input; 代替exit,退出一层程序

CTRL +r

搜索历史命令,左右方向键确定命令,也很方便


关机指令

基本语法

代码语言:javascript
复制
shutdown [选项] 

选项 -h

将系统的服务停掉后,立即关机

代码语言:javascript
复制
[root@VM-4-17-centos ~]# shutdown -h
Shutdown scheduled for Thu 2022-10-27 20:23:58 CST, use 'shutdown -c' to cancel.
[root@VM-4-17-centos ~]# 
Broadcast message from root@VM-4-17-centos (Thu 2022-10-27 20:22:58 CST):

The system is going down for power-off at Thu 2022-10-27 20:23:58 CST!
image.png
image.png

选项 -r

将系统的服务停掉之后重启

代码语言:javascript
复制
[root@VM-4-17-centos ~]# shutdown -r
Shutdown scheduled for Thu 2022-10-27 20:22:59 CST, use 'shutdown -c' to cancel.
[root@VM-4-17-centos ~]# 
Broadcast message from root@VM-4-17-centos (Thu 2022-10-27 20:21:59 CST):

The system is going down for reboot at Thu 2022-10-27 20:22:59 CST!
image.png
image.png

选项 -t

经过几秒后关机

代码语言:javascript
复制
[root@VM-4-17-centos ~]# shutdown -t 60
Shutdown scheduled for Thu 2022-10-27 20:21:12 CST, use 'shutdown -c' to cancel.
[root@VM-4-17-centos ~]# 
Broadcast message from root@VM-4-17-centos (Thu 2022-10-27 20:20:12 CST):

The system is going down for power-off at Thu 2022-10-27 20:21:12 CST!
image.png
image.png


echo

语法

代码语言:javascript
复制
echo 内容

功能

在终端完全显示echo后的内容,不做改变

代码语言:javascript
复制
[root@VM-4-17-centos dir]# echo "hello world"
hello world
[root@VM-4-17-centos dir]# echo "hello world"
hello world
[root@VM-4-17-centos dir]# echo hello world
hello world
image.png
image.png

还可以把echo后的内容重定向到普通文件text.txt

代码语言:javascript
复制
[root@VM-4-17-centos dir]# echo hello world! > text.txt
[root@VM-4-17-centos dir]# cat text.txt 
hello world!
image.png
image.png

printf

在命令行格式化输出内容,同C语言printf用法

代码语言:javascript
复制
[root@VM-4-17-centos ~]# printf "hello,%d\n" 100
hello,100

who

查看当前登录的所有的用户

代码语言:javascript
复制
[root@VM-4-17-centos ~]# who
root     pts/0        2022-10-27 20:17 (221.176.187.238)
weihe    pts/1        2022-10-27 20:27 (221.176.187.238)
image.png
image.png

which

基本语法

代码语言:javascript
复制
which [选项] 待查指令

命令

查找指令/命令所在位置

代码语言:javascript
复制
[root@VM-4-17-centos dir]# which pwd
/usr/bin/pwd
[root@VM-4-17-centos dir]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
image.png
image.png

alias/unalias

alias

格式

代码语言:javascript
复制
alias 新名字==指令串

功能

指令重命名,如果没有参数就列出所有所有别名

代码语言:javascript
复制
[root@VM-4-17-centos dir]# alias weihe='ls -l --color=auto'
代码语言:javascript
复制
[root@VM-4-17-centos dir]# weihe
total 12
drwxr-xr-x 2 root root 4096 Oct 27 17:26 dir1
drwxr-xr-x 2 root root 4096 Oct 27 17:26 dir2
-rw-r--r-- 1 root root   42 Oct 27 17:35 test.txt
代码语言:javascript
复制
[root@VM-4-17-centos dir]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias weihe='ls -l --color=red'
alias weihe2='=ls -l'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

其中--color=auto是啥? 其实是让指令执行的结果带上高亮的颜色;


unalias

格式

代码语言:javascript
复制
unalics 指令别名

功能

删除指令名

代码语言:javascript
复制
[root@VM-4-17-centos dir]# unalias weihe
[root@VM-4-17-centos dir]# unalias weihe2

结语

本节主要介绍了Linux中一些常用的指令,但还有着许多的指令没有涉及,以后用到时需要主动查找学习。 下次再见!


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • mkdir
    • 基本语法
      • 选项 -p
      • rmdir
        • 基本语法
          • -p选项
          • rm
            • 基本语法
              • 选项 -r
                • 选项 -f
                  • 选项 -i
                  • stat
                  • man
                    • 基本语法
                      • 在线手册介绍
                        • 选项 number
                          • 选项 -k
                            • 选项 -a
                            • cp
                              • 基本语法
                                • 选项 - i
                                  • 选项 -f/–force
                                    • 选项 -r/-R/–recursive
                                    • mv
                                      • 基本语法
                                        • 选项 -f
                                          • 选项 -i
                                          • cat
                                            • 基本语法
                                              • 选项 -n
                                                • 选项 -b
                                                  • 选项 -s
                                                  • tac
                                                    • 基本语法
                                                    • more
                                                      • 基本语法
                                                        • 选项 -n
                                                        • less
                                                          • 基本语法
                                                            • 选项 -N
                                                              • 选项 -i
                                                                • 选项 /字符串
                                                                  • 选项 ?字符串
                                                                    • n
                                                                      • N
                                                                      • head
                                                                        • 基本语法
                                                                          • 选项 -n
                                                                          • tail
                                                                            • 基本语法
                                                                              • 选项 -n<行数>
                                                                                • 选项 -f
                                                                                • 重定向
                                                                                  • 输出重定向 >
                                                                                    • 基本语法
                                                                                  • 输入重定向 <
                                                                                    • 基本语法
                                                                                  • 追加 >>
                                                                                    • 基本语法
                                                                                  • 标准输入/标准输出/标准错误
                                                                                  • 管道 |
                                                                                  • date
                                                                                    • 基本语法
                                                                                      • 指定格式显示时间
                                                                                        • 时间戳
                                                                                      • 设置时间 -s
                                                                                      • cal
                                                                                        • 基本语法
                                                                                          • 选项 -3
                                                                                            • 选项 -j
                                                                                              • 选项 -y
                                                                                              • find
                                                                                                • 基本语法
                                                                                                • whereis
                                                                                                  • 基本语法
                                                                                                  • wc
                                                                                                    • 基本语法
                                                                                                    • grep
                                                                                                      • 基本语法
                                                                                                        • 选项 -i
                                                                                                          • 选项 -n
                                                                                                            • 选项 -v
                                                                                                              • 与 | 结合使用
                                                                                                                • 基本语法
                                                                                                                • sort
                                                                                                                  • 基本语法
                                                                                                                  • zip/unzip
                                                                                                                    • 打包和压缩的目的
                                                                                                                      • zip
                                                                                                                        • 基本语法
                                                                                                                        • 选项 -r
                                                                                                                      • unzip
                                                                                                                        • 基本语法
                                                                                                                      • 选项 -d
                                                                                                                      • tar
                                                                                                                        • 基本语法
                                                                                                                          • 选项 -c
                                                                                                                            • 选项 -t
                                                                                                                              • 选项 -z
                                                                                                                                • 选项 -j
                                                                                                                                  • 选项 -v
                                                                                                                                    • 选项 -f
                                                                                                                                      • 选项 -x
                                                                                                                                        • 选项 -C
                                                                                                                                        • bc
                                                                                                                                        • uname
                                                                                                                                          • 基本语法
                                                                                                                                            • 选项 -r
                                                                                                                                              • 选项 -a/–all
                                                                                                                                              • lscpu
                                                                                                                                              • lsmem
                                                                                                                                              • df
                                                                                                                                                • 基本语法
                                                                                                                                                  • 选项 -h
                                                                                                                                                  • 热键相关
                                                                                                                                                    • tab
                                                                                                                                                      • CTRL+c
                                                                                                                                                        • Ctrl+d
                                                                                                                                                          • CTRL +r
                                                                                                                                                          • 关机指令
                                                                                                                                                            • 基本语法
                                                                                                                                                              • 选项 -h
                                                                                                                                                                • 选项 -r
                                                                                                                                                                  • 选项 -t
                                                                                                                                                                  • echo
                                                                                                                                                                  • printf
                                                                                                                                                                  • who
                                                                                                                                                                  • which
                                                                                                                                                                    • 基本语法
                                                                                                                                                                    • alias/unalias
                                                                                                                                                                      • alias
                                                                                                                                                                        • 格式
                                                                                                                                                                      • unalias
                                                                                                                                                                        • 格式
                                                                                                                                                                    • 结语
                                                                                                                                                                    领券
                                                                                                                                                                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档