Linux常见基本指令(爆肝三万字)

前言

本文主要介绍一下高频使用的Linux基本指令和常用热键。
Linux图片

话不多说,直接进入出题!!!

01. ls指令 :查看目录

语法:ls [选项][目录或文件]
功能:对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。

[zzg@VM-24-5-centos ~]$ ls
2023  learn  Linux
//显示的是当前目录下的文件或文件夹``

常见指令
ls -l(也可以写成 ll ) 显示详细信息
ls -a 显示所有文件(包括隐藏文件)

[zzg@VM-24-5-centos learn]$ ll
total 16
drwxrwxr-x 2 zzg zzg 4096 Jan  8 13:03 lesson6	//文件=内容+属性
drwxrwxr-x 2 zzg zzg 4096 Jan  8 22:39 lesson7	
//文件权限+目录(文件)个数+所有者+所属组+文件大小+修改时间+文件名
drwxrwxr-x 4 zzg zzg 4096 Jan 10 10:32 lesson8
drwxrwxr-x 3 zzg zzg 4096 Jan 10 11:27 lesson9
[zzg@VM-24-5-centos learn]$ ls -a
.  ..  lesson6  lesson7  lesson8  lesson9
[zzg@VM-24-5-centos learn]$ ls -al		//ls -l 和 ls -a 的简写
total 24
drwxrwxr-x  6 zzg zzg 4096 Jan 10 11:27 .		//当前路径
drwx------ 11 zzg zzg 4096 Jan  9 22:22 ..		//上级路径
drwxrwxr-x  2 zzg zzg 4096 Jan  8 13:03 lesson6
drwxrwxr-x  2 zzg zzg 4096 Jan  8 22:39 lesson7
drwxrwxr-x  4 zzg zzg 4096 Jan 10 10:32 lesson8
drwxrwxr-x  3 zzg zzg 4096 Jan 10 11:27 lesson9

常用指令

-a 列出目录下的所有文件,包括以 . 开头的隐含文件。
-d 将目录象文件一样显示,而不是显示其下的文件。 如: ls –d 指定目录
-i 输出文件的 i 节点的索引信息。 如 ls –ai 指定文件
-k 以 k 字节的形式表示文件的大小。 ls –alk 指定文件
-l 列出文件的详细信息。
-n 用数字的 UID,GID 代替名称。 (介绍 UID, GID)
-F 在每个文件名后附上一个字符以说明该文件的类型, “*”表示可执行的普通文件; “/”表示目录; “@”表
示符号链接; “|”表示FIFOs; “=”表示套接字(sockets)。(目录类型识别)
-r 对目录反向排序。
-t 以时间排序。
-s 在l文件名后输出该文件的大小。(大小排序,如何找到目录下最大的文件)
-R 列出所有子目录下的文件。 (递归)
-1 一行只输出一个文件。

跳转到目录

02. pwd指令 :显示所处(目录)路径

语法: pwd
功能:显示用户当前所在的目录

[zzg@VM-24-5-centos learn]$ pwd
/home/zzg/learn

Linux和Windows的路径分隔符不同

/home/zzg/learn						//Linux
E:\C++\test_practice\贪心算法		//Windows

Linux用的是 /
Windows用的是 \
跳转到目录

03. cd指令 :进入指定目录

语法: cd 目录名
功能:改变工作目录。将当前工作目录改变到指定的目录下。
在这里插入图片描述
路径分为:绝对路径和相对路径
绝对路径:从根目录开始依次向下查找对应文件(绝对唯一)
相对路径:从当前路径开始查找(当前路径消失则失效)

//绝对路径演示
[zzg@VM-24-5-centos ~]$ pwd
/home/zzg
[zzg@VM-24-5-centos ~]$ cd learn/lesson9/lesson8
[zzg@VM-24-5-centos lesson8]$ pwd
/home/zzg/learn/lesson9/lesson8
[zzg@VM-24-5-centos lesson8]$ 
//相对路径演示
[zzg@VM-24-5-centos lesson6]$ pwd
/home/zzg/learn/lesson6
[zzg@VM-24-5-centos lesson6]$ cd ../lesson9/lesson8
[zzg@VM-24-5-centos lesson8]$ pwd
/home/zzg/learn/lesson9/lesson8
[zzg@VM-24-5-centos lesson8]$ 

cd的其他用法:

cd .: 进入当前目录
cd . . : 进入上级目录
cd ~ : 进入家目录

[zzg@VM-24-5-centos lesson9]$ pwd
/home/zzg/learn/lesson9
[zzg@VM-24-5-centos lesson9]$ cd .
[zzg@VM-24-5-centos lesson9]$ pwd
/home/zzg/learn/lesson9
[zzg@VM-24-5-centos lesson9]$ cd ..
[zzg@VM-24-5-centos learn]$ pwd
/home/zzg/learn
[zzg@VM-24-5-centos learn]$ cd ~
[zzg@VM-24-5-centos ~]$ pwd
/home/zzg

root的家目录与普通用户家目录不同

//root用户的家目录
[root@VM-24-5-centos dir]# pwd
/root/dir
[root@VM-24-5-centos dir]# cd ~
[root@VM-24-5-centos ~]# pwd
/root

跳转到目录

04. touch指令 :创建文件

语法:touch [选项]… 文件…
功能: touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件。

-a 或–time=atime或–time=access或–time=use只更改存取时间。
-c 或–no-create 不建立任何文档。
-d 使用指定的日期时间,而非现在的时间。
-f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
-m 或–time=mtime或–time=modify 只更改变动时间。
-r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
-t 使用指定的日期时间,而非现在的时间。

touch+文件名 :创建普通文件
可以直接创建,也可以运用路径创建

[zzg@VM-24-5-centos csdn]$ touch test1.txt
[zzg@VM-24-5-centos csdn]$ touch ../test2.txt
[zzg@VM-24-5-centos csdn]$ pwd
/home/zzg/csdn
[zzg@VM-24-5-centos csdn]$ touch /home/zzg/csdn/test3.txt
[zzg@VM-24-5-centos csdn]$ ls
test1.txt  test3.txt
[zzg@VM-24-5-centos csdn]$ cd ..
[zzg@VM-24-5-centos ~]$ ls
2023  csdn  learn  Linux  test2.txt

跳转到目录

05. mkdir指令 :创建(文件夹)目录

语法: mkdir [选项] dirname…
功能:在当前目录下创建一个名为 “dirname”的目录

-p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;

mkdir -p test1/test2/test3  //递归创建目录

mkdir + 目录名:在当前路径下创建一个目录

[zzg@VM-24-5-centos csdn]$ pwd
/home/zzg/csdn
[zzg@VM-24-5-centos csdn]$ ls
[zzg@VM-24-5-centos csdn]$ mkdir dir
[zzg@VM-24-5-centos csdn]$ ls
dir
[zzg@VM-24-5-centos csdn]$ mkdir test
[zzg@VM-24-5-centos csdn]$ ls
dir  test
[zzg@VM-24-5-centos csdn]$ mkdir -p a/b/c/d
[zzg@VM-24-5-centos csdn]$ tree a
a
`-- b
    `-- c
        `-- d

3 directories, 0 files

跳转到目录

06. rm&&rmdir指令 :删除(文件夹)目录

rmdir是一个与mkdir相对应的命令。 mkdir是建立目录,而rmdir是删除命令。
语法: rmdir [-p][dirName]
适用对象:具有当前目录操作权限的所有使用者
功能:删除空目录
常用选项:
-p 当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除。

[zzg@VM-24-5-centos dir]$ mkdir test_p
[zzg@VM-24-5-centos dir]$ ls
test_p
[zzg@VM-24-5-centos dir]$ rmdir test_p
[zzg@VM-24-5-centos dir]$ ls
[zzg@VM-24-5-centos dir]$ 
rmdir只能删除空目录,若目录内有文件则不能删除
必须用rm指令,带有相应的选项即可完成相关操作

语法: rm [-f-i-r-v][dirName/dir]
适用对象:所有使用者
功能:删除文件或目录
rm命令可以同时删除文件或目录
rm 文件名:删除普通文件,带选项可删除目录
rm -r 文件名:递归删除,目录(包括 非空目录)和文件都能删除
rm -rf 文件(目录名):无视权限强制删除,当然个别还是不能删除,除非root用户
rm -rf ./* :删除当前目录下文件及其所有子文件
==rm -rf/ ==:把从根目录开始的所有文件删除,最好别这样操作,危险!!!

[zzg@VM-24-5-centos dir]$ ls
普通文件  有文件目录  空目录
[zzg@VM-24-5-centos dir]$ rm 普通文件
[zzg@VM-24-5-centos dir]$ ls
有文件目录  空目录
[zzg@VM-24-5-centos dir]$ rm 空目录
rm: cannot remove ‘空目录’: Is a directory
[zzg@VM-24-5-centos dir]$ rm -r 空目录
[zzg@VM-24-5-centos dir]$ ls
有文件目录
//其他我就不过多演示了,大家可以自行尝试

跳转到目录

07. cp指令 :拷贝文件或目录

语法: cp [选项] 源文件或目录 目标文件或目录
功能: 复制文件或目录
说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息
cp 文件名 路径:拷贝文件到某个路径下
常用选项:

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

cp -r 目录名 路径:r 为递归,递归拷贝文件,即拷贝目录
cp -f 文件名 路径:强制拷贝。平常状况下,路径下有同名目录会给出覆盖提示;-f 强制拷贝,不会弹出提示信息。

[root@VM-24-5-centos ~]# alias cp
alias cp='cp -i'	//在这里可以看出cp系统默认为cp -i

这就是 cp -f 有的时候还是会弹出提示信息的原因

[root@VM-24-5-centos ~]# unalias cp
[zzg@VM-24-5-centos test]$ pwd
/home/zzg/csdn/test
[zzg@VM-24-5-centos test]$ ls
[zzg@VM-24-5-centos test]$ mkdir test
[zzg@VM-24-5-centos test]$ mkdir -p a/b/c/d
[zzg@VM-24-5-centos test]$ touch a.txt
[zzg@VM-24-5-centos test]$ tree
.
|-- a
|   `-- b
|       `-- c
|           `-- d
|-- a.txt
`-- test

5 directories, 1 file
[zzg@VM-24-5-centos test]$ ls
a  a.txt  test
[zzg@VM-24-5-centos test]$ cp a.txt ./test
[zzg@VM-24-5-centos test]$ ls
a  a.txt  test
[zzg@VM-24-5-centos test]$ tree
.
|-- a
|   `-- b
|       `-- c
|           `-- d
|-- a.txt
`-- test
    `-- a.txt

5 directories, 2 files
[zzg@VM-24-5-centos test]$ cp -r a test
[zzg@VM-24-5-centos test]$ tree
.
|-- a
|   `-- b
|       `-- c
|           `-- d
|-- a.txt
`-- test
    |-- a
    |   `-- b
    |       `-- c
    |           `-- d
    `-- a.txt

9 directories, 2 files
[zzg@VM-24-5-centos test]$ 

跳转到目录

08. mv指令 :剪切/重命名(文件/目录)

mv命令是move的缩写,可以 剪切文件/目录 或者 重命名文件/目录。
语法: mv [选项] 源文件或目录 目标文件或目录
功能:

mv会根据后面所带的文件或目录的不同选择重命名文件、剪切文件到新目录中或者重命名。

  1. mv 文件名 不存在文件名:重命名
  2. mv 文件名 路径:剪切
  3. mv 文件名 目录 + 不存在文件名:剪切并重命名文件
  4. mv 目录名 目录 + 不存在目录名:剪切并重命名目录
[root@VM-24-5-centos dir]# touch test
[root@VM-24-5-centos dir]# mkdir work
[root@VM-24-5-centos dir]# ls
test  work						//原文件 原目录
[root@VM-24-5-centos dir]# mv test newtest
[root@VM-24-5-centos dir]# ls
newtest  work					//重命名文件  原目录
[root@VM-24-5-centos dir]# mv newtest ./work
[root@VM-24-5-centos dir]# ls
work
[root@VM-24-5-centos dir]# ls work
newtest				//newtest剪切到work中
[root@VM-24-5-centos dir]# touch a.txt
[root@VM-24-5-centos dir]# ls
a.txt  work				//创建新的a.txt
[root@VM-24-5-centos dir]# mv a.txt work/new.txt
[root@VM-24-5-centos dir]# ls
work
[root@VM-24-5-centos dir]# ls work
newtest  new.txt	//剪切并重命名a.txt到work中变成new.txt
[root@VM-24-5-centos dir]# mv work ../newwork
[root@VM-24-5-centos dir]# ls
[root@VM-24-5-centos dir]# cd
[root@VM-24-5-centos ~]# ls
dir  newwork		//拷贝work目录到上级目录中

跳转到目录

09. man指令 :man手册(查询指令信息)

Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是
man 语法: man [选项] 命令
root安装man手册

yum intall -y man-pages

sudo普通用户安装man手册

sudo yum intall -y man-pages

在这里插入图片描述

查找手册介绍:在这里插入图片描述
注:☆ 为常用手册

man 默认从1号手册开始查找 man 后的内容,按照手册顺序依次向后查找,找到就显示,找不到就是找不到

q退出查阅
常用选项

-k 根据关键字搜索联机帮助
num 只在第num章节找
-a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用a选项,当按 下q退出,他会继续往后面搜索,直到所有章节都搜索完毕。

解释一下,man手册分为8章
1 是普通的命令
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文 件)
3 是库函数,如printf,fread
4 是特殊文件,也就是/dev下的各种设备文件
5 是指文件的格式,比如passwd,就会说明这个文件中各个字段的含义
6 是给游戏留的,由各个游戏自己定义
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
8 是系统管理用的命令,这些命令只能由root使用,如ifconfig

跳转到目录

10. cat指令 :查看文件内容

语法:cat [选项] [文件]
功能: 查看目标文件的内容
常用选项:

  • -b 对非空输出行编号
  • -n 对输出的所有行编号
  • -s 不输出多行空行

将所要查看文件的内容打印到屏幕上

[root@VM-24-5-centos dir]# echo "echo >>追加" >>test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
read two
echo >>追加
[root@VM-24-5-centos dir]
[root@VM-24-5-centos dir]# echo "再次追加" >>test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
read two
echo >>追加
再次追加

cat -s 文件名:省略多余空行 查看文件内容,说白了就是指令之间最多输出一个空行
cat -sn 文件名:-n 是对输出行编号,-sn组合使用就是省略空行输出编号并打印内容
cat < 文件名:输入重定向,将文件中内容读取到 cat 中,并打印
cat :默认从键盘中读数据

[root@VM-24-5-centos dir]# nano test.txt  //自行写入点东西
[root@VM-24-5-centos dir]# cat test.txt 
aaaa
bbbbbb





1111111
3333


bbbbbbbb
[root@VM-24-5-centos dir]# cat -s test.txt 	//省略多余空行
aaaa
bbbbbb

1111111
3333

bbbbbbbb
[root@VM-24-5-centos dir]# cat -ns test.txt //省略空行同时打印行数
     1	aaaa
     2	bbbbbb
     3	
     4	1111111
     5	3333
     6	
     7	bbbbbbbb
[root@VM-24-5-centos dir]# cat <test.txt 	//输出重定向,与查看一个样子,打印文件内容
aaaa
bbbbbb





1111111
3333


bbbbbbbb
[root@VM-24-5-centos dir]# cat      //输入什么回车后输出什么,ctrl + c 停止输入
a
a
avcdfdfff
avcdfdfff
adadadadadadadadad
adadadadadadadadad
^C
[root@VM-24-5-centos dir]# 

跳转到目录

11. more指令 :查看文件内容

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

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

常用选项:

  • -n 对输出的所有行编号
  • q 退出more
cat适合少量文本内容阅读,大量文本阅读会直接看到文档的末尾内容
more指令查看内容,可以从头查看,按回车翻页依次查看
[root@VM-24-5-centos dir]# more test.txt 
aaaa
bbbbbb





1111111
3333


bbbbbbbb
[root@VM-24-5-centos dir]# 

跳转到目录

12. less指令 :查看文件内容

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

语法: less [参数] 文件

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

常用选项:

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

向上翻阅, 向下翻阅,q 退出

less 支持查阅功能,方法为 / + 查阅内容,在一次查阅后,无法查阅到上次查阅内容前面的内容:

[root@VM-24-5-centos dir]# cnt=0; while [ $cnt -le 1000 ]; do echo "hello $cnt"; let cnt++; done > mylog.txt			//写入数据
[root@VM-24-5-centos dir]# ls
mylog.txt  test.txt
[root@VM-24-5-centos dir]# less mylog.txt 
[root@VM-24-5-centos dir]# less mylog.txt 
[root@VM-24-5-centos dir]# 

在这里插入图片描述

跳转到目录

13. head指令 :打印开头10行

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

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

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

选项

  • -n<行数> 显示的行数

head -5 文件名:显示前五行,写成 head -5

[root@VM-24-5-centos dir]# head -5 mylog.txt 
hello 0
hello 1
hello 2
hello 3
hello 4
[root@VM-24-5-centos dir]# 

跳转到目录

14. tail指令 :打印尾部十行

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

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

选项:

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

tail 默认打印尾部十行内容。

tail -n5 文件名:显示文件尾部五行内容

[root@VM-24-5-centos dir]# tail -5 mylog.txt 
hello 996
hello 997
hello 998
hello 999
hello 1000
[root@VM-24-5-centos dir]# 

找头和找尾都知道用head和tail了,那中间部分怎么办呢?

方法1:借助中间文件 + 重定向

  1. head -520 mylog.txt > tmp.txt —— 将前520内容重定向到 tmp.txt 中
  2. tail -20 tmp.txt —— tail 截取 tmp.txt 的后20行
[root@VM-24-5-centos dir]# head -520 mylog.txt > tmp.txt
[root@VM-24-5-centos dir]# 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@VM-24-5-centos dir]# 

方法2:管道
管道就是传输资源的通路
| 就是管道的标志。计算机中最重要的资源是数据,| 就会被 shell 解释为链接两条指令用来传输数据的指令。我们可以把管道也当做一种文件。
本质就是把处理后的结果交给管道后的指令继续进行处理,可以多指令联合处理对数据加工。

[root@VM-24-5-centos dir]# cat mylog.txt | head -520 | 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@VM-24-5-centos dir]# 

跳转到目录

15. date指令 :与时间相关

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

date 用法:date [OPTION]… [+FORMAT]

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

  • %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

在设定时间方面:

  • date -s //设置当前时间,只有root权限才能设置,其他只能查看。
  • date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00
  • date -s 01:01:01 //设置具体时间,不会对日期做更改
  • date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
  • date -s “01:01:01 20080523″ //这样可以设置全部时间
  • date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
  • date -s “20080523 01:01:01″ //这样可以设置全部时间

时间戳:

时间->时间戳: date +%s
时间戳->时间: date -d@1508749502
Unix时间戳(英文为Unix epoch,Unix time, POSIX time 或 Unixtimestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

date +%Y-%m:%d/%H:%M:%S:特殊格式显示时间
date +%s:显示时间戳时间
date +%Y-%m:%d/%H:%M:%S -d@时间戳:将时间戳转换为格式时间

[root@VM-24-5-centos dir]# date +%Y-%m:%d/%H:%M:%S
2023-01:13/23:11:07
[root@VM-24-5-centos dir]# date +%s
1673622721
[root@VM-24-5-centos dir]# date +%Y-%m:%d/%H:%M:%S -d@1673622721
2023-01:13/23:12:01

跳转到目录

16. cal指令 :查看日历

语法: cal [参数] [月份] [年份]

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

常用选项:

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

cal:默认查看本月日历
cal 年份:查看一年的日历
cal -3 :显示系统前一个月,当前月,下一个月的月历

[root@VM-24-5-centos dir]# cal				//本月日历
    January 2023    
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

[root@VM-24-5-centos dir]# cal 2023				//一年的日历
                               2023                               

       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  2  3  4  5  6  7             1  2  3  4             1  2  3  4
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
29 30 31               26 27 28               26 27 28 29 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       1  2  3  4  5  6                1  2  3
 2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
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          1  2  3  4  5                   1  2
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
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  2  3  4  5  6  7             1  2  3  4                   1  2
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
                                              31

[root@VM-24-5-centos dir]# cal -3	//附近三个月的日历
    December 2022         January 2023          February 2023   
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  2  3  4  5  6  7            1  2  3  4
 4  5  6  7  8  9 10   8  9 10 11 12 13 14   5  6  7  8  9 10 11
11 12 13 14 15 16 17  15 16 17 18 19 20 21  12 13 14 15 16 17 18
18 19 20 21 22 23 24  22 23 24 25 26 27 28  19 20 21 22 23 24 25
25 26 27 28 29 30 31  29 30 31              26 27 28            
                                                                
[root@VM-24-5-centos dir]# 

跳转到目录

17. find指令和which指令以及whereis指令 :查找文件并处理

find

语法: find pathname -options

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

常用选项:

  • name 按照文件名查找文件。

find 路径 -name 查找名称:把从这个路径开始的所有 带有查找名称 的文件查找出来

[root@VM-24-5-centos ~]# find ./dir -name test.txt
./dir/test.txt

which

which 指令: 可以查看指令路径
例如ls的路径:

[root@VM-24-5-centos ~]# which ls
alias ls='ls --color=auto'		
	/usr/bin/ls				//ls路径
[root@VM-24-5-centos ~]# which head
/usr/bin/head				//head路径
[root@VM-24-5-centos ~]# which clear
/usr/bin/clear				//clear路径
[root@VM-24-5-centos ~]# 

/usr/bin 存放所有用户都可用的应用程序,我们常用的指令都在这里能找到

whereis

whereis 搜索指定名称的文件/程序/归档文件

[root@VM-24-5-centos ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@VM-24-5-centos ~]# whereis head
head: /usr/bin/head /usr/share/man/man1/head.1.gz /usr/share/man/man1p/head.1p.gz
[root@VM-24-5-centos ~]# 

跳转到目录

18. grep指令 :搜索字符串/文件,打印

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

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

常用选项:

  • -i :忽略大小写的不同,所以大小写视为相同
  • -n :顺便输出行号
  • -v :反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行
  • grep ‘字符串’ 文件:筛选出 含有该字符串 的内容
  • grep -v ‘字符串’ 文件:筛选出 不含有该字符串 的内容
[root@VM-24-5-centos ~]# cat test 
aaaaaaaaa
bbbbbbbbbb
aaavvvv
kkkdasdsd
aabdis
ffffff
dad

dadaaaaaaaaa
sdddddddddd
daddwd55555
[root@VM-24-5-centos ~]# grep 'aa' test 
aaaaaaaaa
aaavvvv
aabdis
dadaaaaaaaaa
[root@VM-24-5-centos ~]# grep -v 'aa' test 
bbbbbbbbbb
kkkdasdsd
ffffff
dad

sdddddddddd
daddwd55555

  • grep -i ‘字符串’ 文件:忽略大小写 正向匹配
  • grep -ivn ‘字符串’ 文件 :忽略大小写反向匹配并给上行号
[root@VM-24-5-centos ~]# cat test 
aaaaaaaaa
bbbbbbbbbb
aaavvvv
kkkdasdsd
aabdis
ffffff
dad
AAAAAA
AaaaAA
ABBaaBBAA
dadaaaaaaaaa
sdddddddddd
daddwd55555
[root@VM-24-5-centos ~]# grep -i 'aa' test 
aaaaaaaaa
aaavvvv
aabdis
AAAAAA
AaaaAA
ABBaaBBAA
dadaaaaaaaaa
[root@VM-24-5-centos ~]# grep -ivn 'aa' test 
2:bbbbbbbbbb
4:kkkdasdsd
6:ffffff
7:dad
12:sdddddddddd
13:daddwd55555

  • cat test.txt | grep ‘aa’: 通过管道来匹配 aa
[root@VM-24-5-centos ~]# cat test |grep 'aa'
aaaaaaaaa
aaavvvv
aabdis
AaaaAA
ABBaaBBAA
dadaaaaaaaaa
[root@VM-24-5-centos ~]# 

跳转到目录

19. zip/unzip指令 :目录/文件压缩与解压缩(zip格式)

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

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

常用选项:

  • -r 递归处理,将指定目录下的所有文件和子目录一并处理
  • zip -r 压缩文件.zip 目录或文件名:压缩文件/目录
[root@VM-24-5-centos ~]# ls
dir  newwork  test
[root@VM-24-5-centos ~]# ls dir
mylog.txt  test.txt  tmp.txt
[root@VM-24-5-centos ~]# tree dir
dir
|-- mylog.txt
|-- test.txt
`-- tmp.txt

0 directories, 3 files
[root@VM-24-5-centos ~]# zip -r dir.zip dir
  adding: dir/ (stored 0%)
  adding: dir/mylog.txt (deflated 80%)
  adding: dir/test.txt (deflated 44%)
  adding: dir/tmp.txt (deflated 80%)
[root@VM-24-5-centos ~]# ls
dir  dir.zip  newwork  test

unzip 解压缩文件:解包到当前路径

[root@VM-24-5-centos ~]# ls newwork/
newtest  new.txt
[root@VM-24-5-centos ~]# cd newwork/
[root@VM-24-5-centos newwork]# unzip ../dir.zip 
Archive:  ../dir.zip
   creating: dir/
  inflating: dir/mylog.txt           
  inflating: dir/test.txt            
  inflating: dir/tmp.txt             
[root@VM-24-5-centos newwork]# ls
dir  newtest  new.txt	//dir解压到了当前路径
[root@VM-24-5-centos newwork]# tree
.
|-- dir
|   |-- mylog.txt
|   |-- test.txt
|   `-- tmp.txt
|-- newtest
`-- new.txt

1 directory, 5 files
[root@VM-24-5-centos newwork]# 

打包压缩的意义:防止数据丢失并节省空间。

跳转到目录

20. tar指令 :打包/解包

tar 指令
语法:tar [-cxtzjvf] 文件与目录 … 参数:

常用选项:

  • -c :建立一个压缩文件的参数指令(create 的意思);
  • -x :解开一个压缩文件的参数指令!
  • -t :查看 tarfile 里面的文件!
  • -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
  • -j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?
  • -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
  • -f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
  • -C : 解压到指定目录

tar既可以打包也可以解包,后缀为tgz

tar -czf dst src :将 src 中的内容压缩到 dst 中。

tar 命令默认可以文件(包括目录)中的文件全部打包。
在这里插入图片描述
tar -xzf dst:将压缩文件 dst 指定到当前路径
在这里插入图片描述
tar -ztvf dst:不解压文件,查看压缩包内容。
tar xzf dst -C 路径:将压缩文件 dst 解压到指定路径

在这里插入图片描述

跳转到目录

21. bc指令 :浮点运算

bc指令可以很方便的做浮点运算

[root@VM-24-5-centos newwork]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1+1
2
3.414+5.235
8.649
3.11111111+6.5555555
9.66666661
^C
(interrupt) Exiting bc.
[root@VM-24-5-centos newwork]# 

跳转到目录

22. uname指令 :显示操作系统相关信息

语法:uname [选项]

功能: uname用来获取电脑和操作系统的相关信息。

补充说明:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。

常用选项:

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

uname 可以查看软硬件体系结构。
uname:直接打印 Linux
uname -a:显示当前使用的服务器的相关信息
uname -r:仅查看体系结构
cat /etc/redhat-release:查看Linux系统的商业化发行版

在这里插入图片描述

跳转到目录

23. shundown指令 :关机

语法:shutdown [选项] 常见选项

  • -h : 将系统的服务停掉后,立即关机。
  • -r : 在将系统的服务停掉之后就重新启动
  • -t sec : -t 后面加秒数,亦即『过几秒后关机』的意思

使用 reboot(需要超级用户权限) 可以重启 Linux

跳转到目录

24. top指令 :任务管理器

就类似于Windows的任务管理器,看起来很复杂

在这里插入图片描述

跳转到目录

25. alias指令 :起别名

当一条指令过于复杂时,可以使用alias 为文件起别名,类似于引用
简单来讲就是:张飞,黑旋风,都i是同一个人,黑旋风就是张飞的别名

系统的指令部分就是别名,例如 ls

[root@VM-24-5-centos ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

unalias 别名:取消别名

[root@VM-24-5-centos ~]# ls -a -l -i -n
total 72
393219 dr-xr-x---.  9 0 0 4096 Jan 13 20:24 .
     2 dr-xr-xr-x. 19 0 0 4096 Jan 13 23:39 ..
393318 -rw-------   1 0 0 5684 Jan 13 23:39 .bash_history
397151 -rw-r--r--.  1 0 0   18 Dec 29  2013 .bash_logout
397152 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bash_profile
397153 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bashrc
393280 drwxr-xr-x   4 0 0 4096 Jan  3 22:24 .cache
393313 drwxr-xr-x   3 0 0 4096 Jan  3 22:24 .config
397154 -rw-r--r--.  1 0 0  100 Dec 29  2013 .cshrc
790266 drwxr-xr-x   2 0 0 4096 Jan 13 22:45 dir
790273 drwxr-xr-x   2 0 0 4096 Jan 13 20:24 newwork
393690 drwxr-xr-x   2 0 0 4096 Jul 25 17:53 .pip
658053 drwxrw----   3 0 0 4096 Jan  8 12:56 .pki
394368 -rw-r--r--   1 0 0   73 Jan  3 22:05 .pydistutils.cfg
135701 drwx------   2 0 0 4096 Jan  3 22:34 .ssh
397155 -rw-r--r--.  1 0 0  129 Dec 29  2013 .tcshrc
393679 -rw-------   1 0 0  982 Jan  8 22:53 .viminfo
[root@VM-24-5-centos ~]# alias myls 'ls -a -l -i -n'
-bash: alias: myls: not found
-bash: alias: ls -a -l -i -n: not found
[root@VM-24-5-centos ~]# clear
[root@VM-24-5-centos ~]# ls -a -l -i -n
total 72
393219 dr-xr-x---.  9 0 0 4096 Jan 13 20:24 .
     2 dr-xr-xr-x. 19 0 0 4096 Jan 13 23:40 ..
393318 -rw-------   1 0 0 5769 Jan 13 23:40 .bash_history
397151 -rw-r--r--.  1 0 0   18 Dec 29  2013 .bash_logout
397152 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bash_profile
397153 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bashrc
393280 drwxr-xr-x   4 0 0 4096 Jan  3 22:24 .cache
393313 drwxr-xr-x   3 0 0 4096 Jan  3 22:24 .config
397154 -rw-r--r--.  1 0 0  100 Dec 29  2013 .cshrc
790266 drwxr-xr-x   2 0 0 4096 Jan 13 22:45 dir
790273 drwxr-xr-x   2 0 0 4096 Jan 13 20:24 newwork
393690 drwxr-xr-x   2 0 0 4096 Jul 25 17:53 .pip
658053 drwxrw----   3 0 0 4096 Jan  8 12:56 .pki
394368 -rw-r--r--   1 0 0   73 Jan  3 22:05 .pydistutils.cfg
135701 drwx------   2 0 0 4096 Jan  3 22:34 .ssh
397155 -rw-r--r--.  1 0 0  129 Dec 29  2013 .tcshrc
393679 -rw-------   1 0 0  982 Jan  8 22:53 .viminfo
[root@VM-24-5-centos ~]# alias myls='ls -a -l -i -n'
[root@VM-24-5-centos ~]# myls
total 72
393219 dr-xr-x---.  9 0 0 4096 Jan 13 20:24 .
     2 dr-xr-xr-x. 19 0 0 4096 Jan 13 23:40 ..
393318 -rw-------   1 0 0 5836 Jan 13 23:40 .bash_history
397151 -rw-r--r--.  1 0 0   18 Dec 29  2013 .bash_logout
397152 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bash_profile
397153 -rw-r--r--.  1 0 0  176 Dec 29  2013 .bashrc
393280 drwxr-xr-x   4 0 0 4096 Jan  3 22:24 .cache
393313 drwxr-xr-x   3 0 0 4096 Jan  3 22:24 .config
397154 -rw-r--r--.  1 0 0  100 Dec 29  2013 .cshrc
790266 drwxr-xr-x   2 0 0 4096 Jan 13 22:45 dir
790273 drwxr-xr-x   2 0 0 4096 Jan 13 20:24 newwork
393690 drwxr-xr-x   2 0 0 4096 Jul 25 17:53 .pip
658053 drwxrw----   3 0 0 4096 Jan  8 12:56 .pki
394368 -rw-r--r--   1 0 0   73 Jan  3 22:05 .pydistutils.cfg
135701 drwx------   2 0 0 4096 Jan  3 22:34 .ssh
397155 -rw-r--r--.  1 0 0  129 Dec 29  2013 .tcshrc
393679 -rw-------   1 0 0  982 Jan  8 22:53 .viminfo
[root@VM-24-5-centos ~]# unalias myls
[root@VM-24-5-centos ~]# myls
-bash: myls: command not found
[root@VM-24-5-centos ~]# 

跳转到目录

26. tree指令 :用树状图展示目录

tree :用树状图展示当前目录及其子目录
普通用户可能没有该命令,需要用root用户安装该指令

yum install -y tree
[zzg@VM-24-5-centos ~]$ tree learn/lesson9
learn/lesson9
`-- lesson8
    |-- gdb
    |   |-- Makefile
    |   |-- test
    |   `-- test.c
    |-- Makefile
    `-- proc
        |-- linux
        |   |-- LICENSE
        |   |-- Makefile
        |   |-- proc.c
        |   |-- process
        |   |-- proc.h
        |   |-- README.en.md
        |   |-- README.md
        |   `-- test.c
        `-- test
            |-- Makefile
            |-- proc.c
            |-- process
            |-- proc.h
            `-- test.c

5 directories, 17 files
[zzg@VM-24-5-centos ~]$ 

跳转到目录

27. nano指令 :编辑文件

没学vim之前,可以先用nano对普通文件进行编辑
root安装nano的方式:

yum install -y nano

nano 文件名:对文件进行编辑

nano
ctrl + x 退出 y(保存),n(不保存),c(取消),然后按下回车 enter 退出。
如果没有此文件,退出的时候会让修改文件名,按照提示即可
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

注意:进入nano后,键盘右边的数字按键不能用,鼠标滚轮不能用。

跳转到目录

28. echo指令 :打印到文件或屏幕

echo “字符串”:可以把字符串打印到屏幕上

[root@VM-24-5-centos ~]# echo "这里是echo指令"
这里是echo指令
[root@VM-24-5-centos ~]# 

echo 在打印的时候默认是向显示器打印的。

echo “字符串” > 文件名:将字符串写入文件中,若文件不存在会创建文件,为输出重定向

解释一下什么是输出重定向:

Linux下可以把一切都看成文件,显示器也是文件,键盘也是文件
输出重定向就是把原本写入到 显示器文件 的内容写入到文件中
注意:输出重定向是覆盖式写入,新的写入会覆盖上一次的写入

所以为了解决这个问题,追加重定向来了!!!

echo “字符串” >> 文件名:将字符串内容追加到文件中,为 追加重定向
追加重定向自带换行,不断向目标文件中新增内容

[root@VM-24-5-centos dir]# touch test.txt
[root@VM-24-5-centos dir]# ls
test.txt
[root@VM-24-5-centos dir]# echo "write one" >test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
write one
[root@VM-24-5-centos dir]# echo "read two" >test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
read two		// >输入重定向覆盖上次的写入
[root@VM-24-5-centos dir]# echo "echo >>追加" >>test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
read two
echo >>追加
[root@VM-24-5-centos dir]
[root@VM-24-5-centos dir]# echo "再次追加" >>test.txt 
[root@VM-24-5-centos dir]# cat test.txt 
read two
echo >>追加
再次追加				// >>追加不会覆盖上次写入,并且自动换行
[root@VM-24-5-centos dir]# 

跳转到目录

29. sort指令 :排序

对文本内容进行排序,按照 ascii码值 排序,默认升序,-r 选项降序。

sort 文件名:按 ascii 码值排序文件内容并打印,不会影响到文件本身
sort -r 文件名:倒序排序

sort 文件名 | uniq:uniq 是对相邻文本内容进行去重,将 sort 文件名的内容放到 管道 中,使用 uniq 进行去重。

[root@VM-24-5-centos dir]# cat test.txt    //原文本内容
aaaa
bbbbbb





1111111
3333


bbbbbbbb
[root@VM-24-5-centos dir]# sort test.txt      //正序排序







1111111
3333
aaaa
bbbbbb
bbbbbbbb
[root@VM-24-5-centos dir]# sort -r test.txt 		//倒叙排序
bbbbbbbb
bbbbbb
aaaa
3333
1111111







[root@VM-24-5-centos dir]# sort test.txt |uniq	//去重正序排序

1111111
3333
aaaa
bbbbbb
bbbbbbbb
[root@VM-24-5-centos dir]# 

跳转到目录

重要的热键

clear:清理屏幕
whoami:显示当前用户
alt + enter:全屏,退出全屏也是 alt + enter
tab:智能补全,系统自动补全未完成的输入
ctrl + c:处理疯狂刷屏的情况,终止程序运行
wc[-lwm] 文件名-l :仅列出行 -w :仅列出多少字(英文单字) -m :多少字符
history : 查看历史命令,上面保存于输入的所有命令

跳转到目录

指令扩展

◆ 安装和登录命令: login、 shutdown、 halt、 reboot、 install、 mount、 umount、 chsh、 exit、 last;
◆ 文件处理命令: file、 mkdir、 grep、 dd、 find、 mv、 ls、 diff、 cat、 ln;
◆ 系统管理相关命令: df、 top、 free、 quota、 at、 lp、 adduser、 groupadd、 kill、 crontab;
◆ 网络操作命令: ifconfig、 ip、 ping、 netstat、 telnet、 ftp、 route、 rlogin、 rcp、 finger、 mail、 nslookup;
◆ 系统安全相关命令: passwd、 su、 umask、 chgrp、 chmod、 chown、 chattr、 sudo ps、 who;
◆ 其它命令: tar、 unzip、 gunzip、 unarj、 mtools、 man、 unendcode、 uudecode。

跳转到目录

总结

到这里本篇博客就结束了,Linux的常见基本指令就介绍这么多,我会继续更新Linux相关知识。
我是翻转的乌龟,如果感觉我写的不错,希望一键三连,如有错误,望指正!
爱学习的你一定有所收获,下期见,拜拜啦!

在这里插入图片描述

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

翻转的乌龟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值