序言

书接上回,我们已经学习了linux的部分基本命令,这篇文章将会为大家继续讲解其他基本命令以及相关知识点

一、mv

介绍

mv命令是move的缩写,可以用来移动问问件或者将文件改名,经常用来备份文件或者目录
语法:mv [选项] 源文件或目录目标文件或目录
功能:

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

常用选项

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

重命名

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 22:35 file1.txt
-rw-r--r-- 1 root root 0 May 20 22:33 file2.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ mv file1.txt myfile.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 22:33 file2.txt
-rw-r--r-- 1 root root 0 May 20 22:35 myfile.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ 

覆盖文件

若当前路径存在同名文件,改名即覆盖

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 22:33 file2.txt
-rw-r--r-- 1 root root 0 May 20 22:35 myfile.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ mv myfile.txt file2.txt
mv: overwrite 'file2.txt'? y
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 22:35 file2.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ 

移动目录

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 22:35 file.txt
drwxr-xr-x 2 root root 6 May 20 22:41 temp
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ mv file.txt temp
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ tree temp
temp
└── file.txt

0 directories, 1 file
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ mv temp ../
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ls
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ls -d ../temp
../temp
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ 

二、printf&echo理解输出重定向操作

1. printf

介绍

跟C语言的printf类似,用于向显示器打印数据

使用

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ printf "hello %d\n" 114514
hello 114514

2. echo

介绍

将信息直接打印在显示器上

使用

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ echo hello world
hello world

3.以echo为例理解输出重定向

3.1 前置知识

一定要牢记,linux系统,一切皆文件,当进程启动前,系统会打开三个标准的数据流:

文件描述符 名称 默认目标 编号
标准输入 stdin 键盘 0
标准输出 stdout 终端屏幕 1
标准错误 stderr 终端屏幕 2

我们之前学的printf/cout都是向终端屏幕打印数据
重定向的本质,就是将这些流的目标从默认设备改为其他文件。

3.2 输出重定向

输出重定向是将原本输出到显示器上的数据输出到其他地方

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 20 23:07 hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ echo helloworld > hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
helloworld

可以这样理解:
在这里插入图片描述
当hello.txt不存在时,会新建一个hello.txt;如果它存在,那就会将其先清空,在写入helloworld。

3.3 追加重定向

将上文的 > 替换成 >> 就是追加重定向,追加重定向就是当文件存在时,不会将其清空,而是在文件内容后面继续写入数据

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
helloworld
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ echo hellolinux > hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
hellolinux
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ echo helloworld >> hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
hellolinux
helloworld

3.4 应用实例——深入理解一切皆文件

看以下情形 :
在这里插入图片描述
当我们登录两个终端时,存放显示器的目录/dev/pts多出了一个文件“1”
,这就是我们刚打开的终端
在这里插入图片描述
当我们将echo重定向到/dev/pts/1时,新建的终端也确实打印了"hello",这也恰恰印证了我们之前的观点

3.5 文件类型汇总

以下是linux各种文件类型的汇总:

类型 标识符 英文 作用
普通文件 - regular file 存储数据、代码、文本
目录文件 d directory 保存文件名与inode映射
链接文件 l link 类似快捷方式
字符设备文件 c character device 按字符流访问设备
块设备文件 b block device 按块访问设备
管道文件 p pipe/FIFO 进程通信
套接字文件 s socket 网络/本地通信

3.6 重定向的技巧——创建或清空文件

刚才讲到,重定向时,文件不存在会新建文件,存在会清空文件,我们就可以利用这个性质来创建或清空文件

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
hellolinux
helloworld
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ > hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 21 12:16 hello.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ > file.txt
[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ ll
total 0
-rw-r--r-- 1 root root 0 May 21 12:17 file.txt
-rw-r--r-- 1 root root 0 May 21 12:16 hello.txt

三、以cat为例理解输入重定向

1. cat

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

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

在之前的示例中,已经用到了cat,cat的作用就很显而易见了,就是查看目标文件的内容

[root@iZbp1izzbuzgqbxbjqaq68Z ~]$ cat hello.txt
hellolinux

2. 输入重定向

以cat为例,当我们不使用重定向操作时,cat默认连着是键盘,我们输入什么,他就会打印什么

[root@H ~]$ cat
hellolinux
hellolinux
114514
114514

这是因为这个流程:

键盘(stdin) -> cat -> 屏幕(stdout)

按下Ctrl+C可以结束操作
输入重定向就是,我们不使用stdin作为输入了,改为其他文件

[root@H ~]$ cat < hello.txt
hellolinux

这样就完成了输入重定向,不读取stdin的数据,转而去读取hello.txt的数据

[root@H ~]$ echo hello > file.txt | cat < file.txt
hello

我们还可以通过管道操作同时创建文件并读取,这个我们后面再讲

四、more&less&head&tail

1. more

语法: more [选项]
功能:more命令,功能类似cat
常用选项:

  • -n:指定输出行数
  • q:退出more

可以按[S]键来下滑,但是不能上滑

[root@H ~]$ more -10 temp.txt
hellobite
hellobite
hellobite
hellobite
hellobite
hellobite
hellobite
hellobite
hellobite
hellobite
--More--(0%)

2. less

less工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大

  • less的用法比起more更加的有弹性,在more的时候,我们并没有办法向前面翻,只能往后面看
  • 但若使用了less时,就可以使用[pageup][pagedown]等按键的功能来往前往后翻看文件,更容易⽤来查看⼀个⽂件的内容
  • 除此之外,在less里头可以拥有更多的搜索功能,不⽌可以向下搜,也可以向上搜。

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

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

3. head

head与tail就像它的名字⼀样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head用来显示档案的开头至标准输出中,而tail想当然尔就是看档案的结尾。
语法
head[参数]…[文件]…
功能
head用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。
选项

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

4.tail

tail命令从指定点开始将文件写到标准输出,使用tail命令的-f选项可以放便的查阅正在改变的日志文件,tail -f [filename]会把filename里最尾部的内容显示在屏幕上,并且不断刷新,使你看到最新的文件内容
语法: tail 必要参数 [文件]
功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。
选项

  • -f循环读取
  • -n<行数>显示行数
    以上的指令都可以用来查阅大型文件,最典型的例子就是日志文件

4. 常用操作

4.1管道操作

temp.txt是hello1到hello2000,共2000行数据,如下操作就是在它前100行数据中取后10个,这就是管道操作

[root@H ~]$ head -100 temp.txt | tail -10
hello91
hello92
hello93
hello94
hello95
hello96
hello97
hello98
hello99
hello100

它的本质就是:一个进程的输出,作为另一个进程的输入

4.2 阻塞

当我们输入cat时,显示器会向我们的键盘读取数据,等待我们输入,这个现象就被称为阻塞

[root@H ~]$ cat

五、date&cal

1. date

时间戳: 时间戳是用来表示某一特定时间点的数字或字符编码
Unix时间戳: 它定义为从 格林威治标准时间(GMT/UTC)1970年1月1日00:00:00起到当前时刻所经过的总秒数或毫秒数
指定格式显示时间: date + %Y:%m:%d
用法: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
[root@H ~]$ date 
Thu May 21 17:55:31 CST 2026
[root@H ~]$ date +%Y:%m:%d
2026:05:21
[root@H ~]$ date +%Y-%m-%d_%H:%M:%S
2026-05-21_17:56:26

2. cal

cal命令可以用来显示公历(阳历)日历。公历是现在国际通用的历法,又称格列历,通称阳历。“阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”。
命令格式: cal 参数 [年份]
功能:⽤于查看日历等时间信息,如只有⼀个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常⽤选项:

  • -3:显示系统前⼀个月,当前月,下一个月的月历
  • -j:显示在当年中的第几天(⼀年日期按天算,从1月1号算起,默显示当前月在⼀年中的天数)
  • -y:显示当前年份的日历
[root@H ~]$ cal
      May 2026      
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@H ~]$ cal -y
                               2026                               

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

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐