Linux知识点 -- 常见指令

Linux知识点 – 常见指令


一、Linux下基本指令

1.ls指令 - 列文件或目录信息

语法ls [选项][目录或文件]
功能对于目录,该指令列出该目录下所有的子目录与文件;对于文件,该指令列出文件名及其他信息;
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos code]$ ls
lesson07-20  lesson07-27
[lmx@VM-8-2-centos code]$ ls -l
total 8
drwxrwxr-x 2 lmx lmx 4096 Sep 28  2022 lesson07-20
drwxrwxr-x 3 lmx lmx 4096 Oct 10  2022 lesson07-27

注:
(1)如果在磁盘上创建一个空文件,还是要占空间的,因为文件 = 内容数据 + 属性数据,就算是空文件,还是有属性数据的;
(2)Linux是由隐藏文件/目录的,让文件/目录以 . 开头,就是隐藏文件/目录;用途:可以用来隐藏一些配置文件;
(3)ll是ls -l的缩写

[lmx@VM-8-2-centos code]$ ls -al
total 16
drwxrwxr-x 4 lmx lmx 4096 Oct 10  2022 .
drwx------ 9 lmx lmx 4096 Sep 28  2022 ..
drwxrwxr-x 2 lmx lmx 4096 Sep 28  2022 lesson07-20
drwxrwxr-x 3 lmx lmx 4096 Oct 10  2022 lesson07-27

其中 . 代表当前文件路径(可省略),. .代表上级文件路径,可以帮助我们进行路径切换;

2.pwd命令 - 显示用户当前所在目录

语法pwd
功能显示用户当前所在目录
常用选项

[lmx@VM-8-2-centos code]$ pwd
/home/lmx/code

3.cd指令 - 改变工作目录

语法cd 目录名
功能改变工作目录,将当前工作目录改变到指定路径下
常用选项

[lmx@VM-8-2-centos code]$ cd lesson07-20
[lmx@VM-8-2-centos lesson07-20]$ 

绝对路径:以根目录开头,定位到某一文件或文件夹;
相对路径:不以根目录开始,而是以当前目录为参考点,定位文件;

(1)cd … : 返回上级目录

[lmx@VM-8-2-centos code]$ cd lesson07-20
[lmx@VM-8-2-centos lesson07-20]$ cd ..
[lmx@VM-8-2-centos code]$ 

(2)cd /home/code/ :绝对路径

[lmx@VM-8-2-centos code]$ cd /home/
[lmx@VM-8-2-centos home]$ 

(3)cd …/…/code :相对路径

[lmx@VM-8-2-centos ~]$ cd code/lesson07-20
[lmx@VM-8-2-centos lesson07-20]$ cd ../lesson07-27

(4)cd ~ :进入当前用户的工作目录

[lmx@VM-8-2-centos lesson07-27]$ cd ~
[lmx@VM-8-2-centos ~]$ 

(5)cd - :跳转至上一次我所处的路径中

[lmx@VM-8-2-centos ~]$ cd -
/home/lmx/code/lesson07-27
[lmx@VM-8-2-centos lesson07-27]$ 

4.touch指令 - 更改文件时间或新建文件

语法touch[选项] 文件
功能可更改文档或目录的日期时间,包括存取和更改时间,或新建一个不存在的文件
常用选项
在这里插入图片描述
新建文件

[lmx@VM-8-2-centos lesson07-20]$ touch text.txt
[lmx@VM-8-2-centos lesson07-20]$ ll
total 4
-rw-rw-r-- 1 lmx lmx 112 Oct  8  2022 test.c
-rw-rw-r-- 1 lmx lmx   0 May 10 16:30 text.txt

在这里插入图片描述
在这里插入图片描述

5.mkdir指令 - 创建目录 / tree - 以树状形式显示目录

语法mkdir [选项] dirname
功能在当前目录下创建一个名为diename的目录
常用选项
在这里插入图片描述

lmx@VM-8-2-centos lesson07-20]$ mkdir test
[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.c  text.txt
[lmx@VM-8-2-centos lesson07-20]$ cd test
[lmx@VM-8-2-centos test]$ mkdir -p d1/d2/d3/d4
[lmx@VM-8-2-centos test]$ ls
d1

tree:以树状形式显示目录
需要安装,必须是root用户;

[root@VM-8-2-centos ~]# yum install -y tree

使用:

[lmx@VM-8-2-centos lesson07-20]$ tree test
test
`-- d1
    `-- d2
        `-- d3
            `-- d4

4 directories, 0 files

6.rmdir指令 && rm指令(重要) - 删除目录或文件

(1)rmdir是一个与mkdir相对应的命令,rmdir是删除目录;
语法emdir [-p] [dirame]
适用对象具有当前目录操作权限的所有使用者;
功能删除空目录
常用选项
在这里插入图片描述
mkdir默认只能删除空目录;

[lmx@VM-8-2-centos test]$ ls
d1
[lmx@VM-8-2-centos test]$ rmdir d1
rmdir: failed to remove ‘d1’: Directory not empty

(2)rm命令可以同时删除文件或目录;
语法rm [-f-i-r-v] [dirname/dir]
适用对象:所有使用者
功能删除文件或目录
常用选项
在这里插入图片描述
删除前确认:

[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.c  text.txt
[lmx@VM-8-2-centos lesson07-20]$ rm -i text.txt 
rm: remove regular empty file ‘text.txt’? y
[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.c

Linux没有回收站,删除了就没了;

强制删除:

[lmx@VM-8-2-centos test]$ rm -rf d1
[lmx@VM-8-2-centos test]$ ls

rm -rf *.c
*是一种通配结构,意思是删除当前目录下所有的.c后缀的文件;
rm -rf file *:删除所有file开头的文件;
rm -rf *:删除当前目录下所有文件;

[lmx@VM-8-2-centos test]$ ls
s1.c  s2.c  s3.c  s4.c  s5.c
[lmx@VM-8-2-centos test]$ rm -rf *.c
[lmx@VM-8-2-centos test]$ ls
[lmx@VM-8-2-centos test]$ 

7.man指令(重要)- 手册

语法man [选项] 命令
功能Linux手册页
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos test]$ man pwd

man手册分为8章:
在这里插入图片描述
配置man手册:

yum install -y man-pages

8.cp指令(重要)- 复制

语法cp[选项] 源文件或目录 目标文件或目录
功能复制文件或目录
常用选项
在这里插入图片描述
nano文本编辑器

[lmx@VM-8-2-centos test]$ touch hello.c
[lmx@VM-8-2-centos test]$ nano hello.c    #nano文本编辑

在这里插入图片描述
复制文件到绝对路径

[lmx@VM-8-2-centos test]$ cp hello.c /home/lmx/code/lesson07-20/test/d1
[lmx@VM-8-2-centos test]$ cd d1
[lmx@VM-8-2-centos d1]$ ls
hello.c

复制到上级目录

[lmx@VM-8-2-centos test]$ cp hello.c ../
[lmx@VM-8-2-centos test]$ cd ..
[lmx@VM-8-2-centos lesson07-20]$ ls
hello.c  test  test.c

cp -rf :强制拷贝

[lmx@VM-8-2-centos test]$ cp -rf d1 ../
[lmx@VM-8-2-centos test]$ cd ..
[lmx@VM-8-2-centos lesson07-20]$ ls
d1  hello.c  test  test.c

9.mv指令(重要)- 移动文件或将文件改名

语法mv [选项] 源文件或目录 目标文件或目录
功能移动文件或将文件改名
常用选项
在这里插入图片描述
文件移动

[lmx@VM-8-2-centos lesson07-20]$ ls
d1  hello.c  test  test.c
[lmx@VM-8-2-centos lesson07-20]$ mv hello.c ./d1
[lmx@VM-8-2-centos lesson07-20]$ cd d1
[lmx@VM-8-2-centos d1]$ ls
hello.c

目录移动

[lmx@VM-8-2-centos lesson07-20]$ mv d1 ./test
[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.c
[lmx@VM-8-2-centos lesson07-20]$ cd test
[lmx@VM-8-2-centos test]$ ls
d1  hello.c
[lmx@VM-8-2-centos test]$ cd ..
[lmx@VM-8-2-centos lesson07-20]$ tree .
.
|-- test
|   |-- d1
|   |   `-- hello.c
|   `-- hello.c
`-- test.c

2 directories, 3 files

文件改名

[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.c
[lmx@VM-8-2-centos lesson07-20]$ nano test.c
[lmx@VM-8-2-centos lesson07-20]$ mv test.c test.cpp
[lmx@VM-8-2-centos lesson07-20]$ ls
test  test.cpp

文件改名时必须在当前路径下操作;
mv源文件后面跟的不是路径,而是文件名,就成了文件重命名操作;

10.cat指令 - 查看目标文件内容

语法cat [选项] [文件]
功能查看目标文件内容
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ cat test.cpp
#include<stdio.h>

int main()
{
    
    
    printf("hello world\n");
    
    printf("hello world\n");

    return 0;
}

10.more指令 - 显示文件(一页)

语法more [选项] [文件]
功能与cat类似,但是只把文件显示一个屏幕的内容,之后的不显示了,按回车继续显示
常用选项
在这里插入图片描述

11.less指令(重要)- 显示文件(翻页)

语法less [参数] 文件
功能与more类似,粗看文本(推荐),跟more作用一样,但可以上下翻,q退出
常用选项
在这里插入图片描述

less file.txt 

在这里插入图片描述
按↑和↓或者PageUp和PageDown可以翻页;
q退出;

12.head指令 - 提取文件头部若干行

语法head [参数] [文件]
功能提取文件头部若干行,默认10行
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ head -15 file.txt 
dwadsa
dasdsa


asd
s

s
as
sa
d
a
d
as
d

13.tail指令 - 提取文件尾部若干行 / 管道

语法tail [参数] [文件]
功能提取文件尾部若干行,默认10行
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ tail -15 file.txt 
2

3
3
2

54

6
8
7
98
2
2

提取文件中间n行
方法一:
创建临时文件取出前30行,重定向保存到tmp.txt

[lmx@VM-8-2-centos lesson07-20]$ head -30 file.txt > tmp.txt
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp  tmp.txt

再从tmp.txt取后10行

[lmx@VM-8-2-centos lesson07-20]$ tail -10 tmp.txt 

3543
5
4
32

3

3
3

方法二
使用管道:

[lmx@VM-8-2-centos lesson07-20]$ head -30 file.txt | tail -10

3543
5
4
32

3

3
3

| 就是管道,用来传导数据的,左边是入口,右边是出口,先从file.txt文件中提取数据(前30行文本),传输到管道中,再从管道中的数据提取后10行的文本打印出来;
管道文件是内存级的文件,没有在磁盘上;

[lmx@VM-8-2-centos lesson07-20]$ head -30 file.txt | tail -10 | wc -l
10

wc -l是统计文本行数;

14.时间相关的指令 - date

date显示
语法date [选项] [+格式]
(1)显示方面,可以设定时间显示格式
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ date +%Y:%m:%d
2023:05:10
[lmx@VM-8-2-centos lesson07-20]$ date +%F
2023-05-10

(2)在设定时间方面
在这里插入图片描述
(3)时间戳
时间 -> 时间戳 : date +%s
时间戳 -> 时间:date -d@1508749502

Unix时间戳是从1970年1月1日开始所经过的秒数,不考虑闰秒;

[lmx@VM-8-2-centos lesson07-20]$ date +%s
1683726978
[lmx@VM-8-2-centos lesson07-20]$ date -d@1683726978
Wed May 10 21:56:18 CST 2023
[lmx@VM-8-2-centos lesson07-20]$ date +%F -d@1683726978
2023-05-10

单向递增,具有唯一性;

15.cal指令 - 显式公历

语法cal[参数][月份][年份]
功能显式公历
常用选项在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ cal 3 2015
     March 2015     
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
[lmx@VM-8-2-centos lesson07-20]$ cal -y 2021
                               2021                               

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

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

16.find指令(非常重要)- 搜索文件

语法find 目录 [选项] 文件名
功能在目录结构中搜索文件,并执行指定的操作
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ find ./ -name file.txt
./file.txt
[lmx@VM-8-2-centos lesson07-20]$ find ~ -name tmp.txt
/home/lmx/code/lesson07-20/tmp.txt

17.grep指令 - 在文件中搜索字符串

语法grep [选项] 搜寻字符串 文件
功能在文件中搜索字符串,将找到的行打印出来
常用选项
在这里插入图片描述

[lmx@VM-8-2-centos lesson07-20]$ grep -in 'a' file.txt 
1:dwadsa
2:dasdsa
5:asd
9:as
10:sa
12:a
14:as
16:as
18:sa
20:a

管道操作

[lmx@VM-8-2-centos lesson07-20]$ grep -in 'a' file.txt | head -3 > tmp.txt 
[lmx@VM-8-2-centos lesson07-20]$ cat tmp.txt 
1:dwadsa
2:dasdsa
5:asd

18.zip/unzip指令 - 压缩/解压文件

语法zip 压缩文件.zip 目录或文件
功能将目录或文件压缩成zip格式
常用选项
在这里插入图片描述
将整个目录及其内容全部打包

[lmx@VM-8-2-centos lesson07-20]$ zip -r test.cip test
  adding: test/ (stored 0%)
  adding: test/hello.c (stored 0%)
  adding: test/d1/ (stored 0%)
  adding: test/d1/hello.c (stored 0%)
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cip  test.cpp  tmp.txt

解压缩到当前路径

[lmx@VM-8-2-centos lesson07-20]$ unzip test.cip
Archive:  test.cip
   creating: test/
 extracting: test/hello.c            
   creating: test/d1/
 extracting: test/d1/hello.c         
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cip  test.cpp  tmp.txt

解压缩到目标路径

[lmx@VM-8-2-centos lesson07-20]$ unzip test.cip -d /home/lmx/code/lesson07-20/tmp
Archive:  test.cip
   creating: /home/lmx/code/lesson07-20/tmp/test/
 extracting: /home/lmx/code/lesson07-20/tmp/test/hello.c  
   creating: /home/lmx/code/lesson07-20/tmp/test/d1/
 extracting: /home/lmx/code/lesson07-20/tmp/test/d1/hello.c  

19.tar指令(重要)- 压缩/解压文件

语法tar [选项] 文件与目录 参数
功能打开/解包,不打开它,直接看内容
常用选项
在这里插入图片描述
压缩

[lmx@VM-8-2-centos lesson07-20]$ tar -czf test.tgz test
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp  test.tgz
[lmx@VM-8-2-centos lesson07-20]$ rm test.tgz 
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp
[lmx@VM-8-2-centos lesson07-20]$ tar -czvf test.tgz test
test/
test/hello.c
test/d1/
test/d1/hello.c
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp  test.tgz

c:创建文件;
z:压缩;
v:压缩可视化;
f:使用文件名(需要在最后);

解压到当前路径

[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test.cpp  test.tgz
[lmx@VM-8-2-centos lesson07-20]$ tar -xzvf test.tgz 
test/
test/hello.c
test/d1/
test/d1/hello.c
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp  test.tgz

解压到指定路径

[lmx@VM-8-2-centos lesson07-20]$ mkdir tmp
[lmx@VM-8-2-centos lesson07-20]$ tar -xzvf test.tgz -C /home/lmx/code/lesson07-20/tmp/
test/
test/hello.c
test/d1/
test/d1/hello.c
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  test  test.cpp  test.tgz  tmp

x:解压文件;
C:解压到指定路径;

20.bc指令 - 浮点运算

[lmx@VM-8-2-centos lesson07-20]$ echo "21 / 5" | bc
4

21.uname指令 - 获取电脑和操作系统的相关信息

用来获取电脑和操作系统的相关信息

[lmx@VM-8-2-centos lesson07-20]$ uname -a
Linux VM-8-2-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

22.shutdown指令 - 关机

语法shutdown [选项]
功能关机
常用选项
在这里插入图片描述

23.history指令 - 打印历史命令

[lmx@VM-8-2-centos lesson07-20]$ history > order.txt
[lmx@VM-8-2-centos lesson07-20]$ ls
file.txt  order.txt  test  test.cpp

24.xargs - 将管道的输出结果,以命令行的参数,交给后面的程序

将管道的输出结果,以命令行的参数,交给后面的程序;

[lmx@VM-8-2-centos lesson07-20]$ echo "-l -a -i" | ls
file.txt  order.txt  test  test.cpp

这样是以字符串的形式传给ls;

[lmx@VM-8-2-centos lesson07-20]$ echo "-l -a -i" | xargs ls
total 36
660520 drwxrwxr-x 3 lmx lmx  4096 May 11 11:37 .
660519 drwxrwxr-x 4 lmx lmx  4096 Oct 10  2022 ..
656163 -rw-rw-r-- 1 lmx lmx   168 May 10 21:13 file.txt
656001 -rw-rw-r-- 1 lmx lmx 16083 May 11 11:37 order.txt
921016 drwxrwxr-x 3 lmx lmx  4096 May 10 20:54 test
660521 -rw-rw-r-- 1 lmx lmx   112 Oct  8  2022 test.cpp

以参数的形式传给ls;

二、Linux快捷键

1.终止异常的命令:CTRL + C,让当前的程序停掉
2.XSHELL下的复制:CTRL + insert
3.XSHELL下的粘贴:SHIFT + insert
4.补全指令:TAB
5.文本编辑器:nano 文件名
6.快速退出:CTRL + D,代表着键盘输入结束(EOF)的意思,也可以取代exit;
7.寻找之前的指令:CTRL + R,找寻之前输入的指令,找到后回车即可运行

猜你喜欢

转载自blog.csdn.net/kissland96166/article/details/130602921