文章目录
- 一、与文件路径相关的指令
- 0.补充知识:路径的认识
- 1.pwd 指令
- 2.cd 指令(含家目录的介绍)
- 二、创建和删除文件的指令
- 0.补充知识:普通文件和目录文件
- 1.touch 指令(可以修改文件的时间戳)
- 2.mkdir 指令
- 3.rmdir 指令
- 4.rm 指令
- 三、修改文件内容的指令
- 0.0 补充知识:下载指令或插件
- 0.1 补充知识:nano编辑器(用来修改普通文件内容)
- 1.echo 指令
- 四、查看文件属性和内容的指令
- 0.0 补充知识:文件属性和文件内容
- 0.1 补充知识:Linux下一切皆文件
- 1.ls 指令
- 2.tree 指令(如未内置需下载)
- 3.stat 指令
- 4.cat 指令
- 5.tac 指令
- 6.more 指令
- 7.less 指令(适合浏览大文件)
- 8.head 指令
- 9.tail 指令
- 五、复制和移动文件的指令
- 1.cp 指令
- 2.mv 指令
- 六、查找文件及文件中指定内容的指令
- 1.find 指令
- 2.which 指令
- 3.whereis 指令
- 4.grep 指令 (在文件中搜索字符串)
- 七、压缩和解压文件的指令
- 0.补充知识:lrzsz工具
- 1.zip/unzip 指令(如未内置需下载)
- 2.tar 指令
- 八、时间和日期相关的指令
- 0. 补充知识:时间戳和时区
- 1.date 指令
- 2.cal 指令
- 九、其他指令
- 1.alias 指令(为命令创建别名)
- 2.bc 指令(进行计算)
- 3.uname 指令(获取操作系统的相关信息)
- 4.shutdown 指令(关机)
一、与文件路径相关的指令
0.补充知识:路径的认识
- Linux系统中,磁盘上的普通文件和目录文件被组成⼀棵目录树,每个节点都是目录文件或普通文件
- 其中普通文件⼀定是目录树的叶子节点;目录文件可能是叶子节点(空目录)
- 理解路径存在的意义: 树状组织方式,都是为了保证快速定位查找到指定的文件,而定位文件就需要具有唯一性的方案来进行定位文件。其中任何一个节点,都只有一个父节点,所以,从根目录开始,定位指定文件,路径具有唯一性
- 绝对路径:从/(根目录)开始,不依赖其他目录的定位文件的方式。比如上图中httpd文件,它的绝对路径是:/var/httpd
- 相对路径:相对于当前用户所处目录,定位文件的路径方式。比如假设当前用户所处目录是ftp,要定位httpd文件的位置,相对路径就是:. ./httpd(. .表示上级目录,ftp的上级目录是var)
- 绝对路径⼀般不会随着用户的路径变化而丧失唯⼀性,⼀般在特定服务的配置文件中经常被使用
- 相对路径因为它的便捷性,⼀般在命令行中使用较多
1.pwd 指令
语法: pwd
功能:显示用户当前所在的目录(工作目录)
常用选项:无
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ pwd
/home/zh
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cd practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
2.cd 指令(含家目录的介绍)
语法: cd 目录文件名(指定路径)
功能:改变工作目录。将当前工作目录改变到指定的目录文件下
特殊用法:
cd ~(快速进入自己的家目录)
cd - (回退到最近一次所处的目录)
- 示例一(cd到指定的绝对路径下的目录文件):
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ tree .
.
└── d1└── d2└── d33 directories, 0 files
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ cd /home/zh/practice/d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ pwd
/home/zh/practice/d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ tree .
.
└── d2└── d32 directories, 0 files
// tree . ( . 表示当前目录文件)的作用是以树状形式展现当前目录文件下的文件(tree指令后面有介绍)
- 示例二(返回上级目录文件的方式):
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ pwd
/home/zh/practice/d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ tree .
.
└── d2└── d32 directories, 0 files
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ cd ..
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
// cd . . ( . . 表示上级目录文件)的作用是返回上级目录文件
- 示例三(cd到相对路径下的目录文件):
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ cd ./d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ pwd
/home/zh/practice/d1
实际上cd到当前工作目录下的目录文件可以直接写成:cd 目录文件名(不用指定路径,系统会默认在当前工作目录下找该文件),比如:
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ cd d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ pwd
/home/zh/practice/d1
[zh@iZbp1dr1jtgcuih41mw88oZ d1]$ cd d2
[zh@iZbp1dr1jtgcuih41mw88oZ d2]$ pwd
/home/zh/practice/d1/d2
[zh@iZbp1dr1jtgcuih41mw88oZ d2]$ cd d3
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ pwd
/home/zh/practice/d1/d2/d3
- 家目录的概念以及cd ~ 的使用
在Linux下有两种用户,root用户(管理员)和普通用户:
root用户在安装操作系统的时候,就已经内置了工作目录: /root,这就是root用户的家目录,登录root用户时会默认从这个路径开始
root用户可以创建普通用户,每次新建⼀个普通用户都会在/home目录下为新用户创建新的工作目录,目录以新用户名称命名。
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ cd /home
[zh@iZbp1dr1jtgcuih41mw88oZ home]$ ls
ccy zh
比如我的Linux机器下创建了两个普通用户,ccy 和 zh,那么他们的家目录分别是/home/ccy 和 /home/zh,当我们指定用户登录Linux机器时,他们都会默认从自己的家目录开始。
示例(root用户使用cd ~ 快速进入自己的家目录):
[root@iZbp1dr1jtgcuih41mw88oZ d2]# pwd
/root/practice/d1/d2
[root@iZbp1dr1jtgcuih41mw88oZ d2]# cd ~
[root@iZbp1dr1jtgcuih41mw88oZ ~]# pwd
/root
示例(普通用户使用cd ~ 快速进入自己的家目录):
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ pwd
/home/zh/practice/d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ cd ~
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ pwd
/home/zh
- cd - (回退到最近一次所处的目录)
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ pwd
/home/zh/practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ cd ./d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ pwd
/home/zh/practice/d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ d3]$ cd -
/home/zh/practice
[zh@iZbp1dr1jtgcuih41mw88oZ practice]$ cd -
/home/zh/practice/d1/d2/d3
二、创建和删除文件的指令
0.补充知识:普通文件和目录文件
Linux中文件的类型主要分成两大类,普通文件(相当于windows系统中的文本类文件,可以该文件里编辑内容)和目录文件(相当于windows系统中的文件夹,用来存储文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 11 17:08 test
-rw-rw-r-- 1 zh zh 0 Apr 11 19:52 test.txt
// ls -l :作用是展示当前工作目录下的文件详细信息(ls指令介绍在后面)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cd test.txt
bash: cd: test.txt: Not a directory
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cd test
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ touch 123
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
// touch 文件名:作用是在当前工作路径下创建一个普通文件(touch指令介绍在后面)
cd命令不可以进入普通文件(cd 普通文件 会报错:这个文件不是一个目录)。
cd命令可以进入目录文件,在目录文件下还可以创建文件(注:普通文件下不能创建文件)
1.touch 指令(可以修改文件的时间戳)
语法: touch [选项] 文件名
功能:若文件不存在,默认会新建一个普通文件;若文件存在,会修改文件的时间戳
常用选项:
• -a : change the access time(Change Time也会变)
• -m : change the modification time (Change Time也会变)
- touch 文件名(若文件不存在,默认会新建一个普通文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 11 21:46 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch hello
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 12 20:34 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 11 21:46 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
- touch 文件名(若文件存在,会修改文件的时间戳,Access Time、Modify Time和Change Time都被改变)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-11 19:52:56.802439608 +0800
Modify: 2025-04-11 19:52:56.802439608 +0800
Change: 2025-04-11 19:52:56.802439608 +0800Birth: -
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-12 20:23:30.744681728 +0800
Modify: 2025-04-12 20:23:30.744681728 +0800
Change: 2025-04-12 20:23:30.744681728 +0800Birth: -
// stat 文件名:作用是展示文件的详细信息,包括大小、权限、时间戳等(stat 指令介绍在后面)
- touch -a 文件名(若文件存在,修改文件的Access Time和Change Time;文件不存在,新建一个普通文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-12 20:23:30.744681728 +0800
Modify: 2025-04-12 20:23:30.744681728 +0800
Change: 2025-04-12 20:23:30.744681728 +0800Birth: -
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch -a test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-12 20:24:31.108949010 +0800
Modify: 2025-04-12 20:23:30.744681728 +0800
Change: 2025-04-12 20:24:31.108949010 +0800Birth: -
touch -m 文件名(若文件存在,修改文件的Modify Time和Change Time;文件不存在,新建一个普通文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-12 20:25:13.609545332 +0800
Modify: 2025-04-12 20:25:13.609545332 +0800
Change: 2025-04-12 20:25:13.609545332 +0800Birth: -
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ touch -m test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat test.txtFile: ‘test.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 922648 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-12 20:25:13.609545332 +0800
Modify: 2025-04-12 20:25:29.954159235 +0800
Change: 2025-04-12 20:25:29.954159235 +0800Birth: -
2.mkdir 指令
语法: mkdir [选项] 文件名
功能:在当前目录下创建一个目录文件
常用选项:
• -p: 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录
- mkdir 文件名(在当前目录下创建⼀个目录文件)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ mkdir drk
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
drwxrwxr-x 2 zh zh 4096 Apr 12 21:08 drk
- mkdir -p 文件名(在当前目录下递归建立多个目录文件)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ mkdir d1/d2/d3
mkdir: cannot create directory ‘d1/d2/d3’: No such file or directory
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ mkdir -p d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
├── 123
├── d1
│ └── d2
│ └── d3
└── drk
// 不带 -p 选项只能一次创建一个目录文件
3.rmdir 指令
语法: rmdir [选项] 文件名
适用对象:具有当前目录操作权限的所有使用者
功能:删除空目录
常用选项:
• -p:当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除,即递归删除多个目录文件
- rmdir 文件名(删除空目录)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
drwxrwxr-x 3 zh zh 4096 Apr 12 21:09 d1
drwxrwxr-x 2 zh zh 4096 Apr 12 21:08 drk
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
├── 123
├── d1
│ └── d2
│ └── d3
└── drk4 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ rmdir drk
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
drwxrwxr-x 3 zh zh 4096 Apr 12 21:09 d1
- rmdir -p 文件名(递归删除多个目录文件)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
├── 123
└── d1└── d2└── d33 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ rmdir -p d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
└── 123
不带 -p 选项的效果,只删除了路径最深层的d3空目录
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
├── 123
└── d1└── d2└── d33 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ rmdir d1/d2/d3
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ tree .
.
├── 123
└── d1└── d2
4.rm 指令
语法: rm [选项] 文件
功能:删除普通文件或目录(可以同时删除普通文件和目录)
常用选项:
• -i 删除文件前逐一询问确认
• -r 删除目录及其下所有文件
• -f 即使文件属性为只读(即写保护),亦直接删除
- rm 普通文件
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
drwxrwxr-x 3 zh zh 4096 Apr 17 21:14 ppt
-rw-rw-r-- 1 zh zh 41 Apr 17 20:34 test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ rm test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 3 zh zh 4096 Apr 17 21:14 ppt
rm -i 普通文件(删除文件前询问用户是否删除文件,y表示是,n表示否):
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 17 21:25 ppt.txt
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ rm -i test
rm: remove regular empty file ‘test’? y
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 2 zh zh 4096 Apr 17 21:25 ppt.txt
- rm 多个文件(如果多个文件中含目录文件,需使用-r选项,否则无法删除目录)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 17 21:23 hello.txt
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test
-rw-rw-r-- 1 zh zh 0 Apr 17 21:23 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ rm *.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test
// * 表示通配符,匹配所有.txt结尾的文件名
多个文件中含目录文件,需使用 -r 选项:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 hello.txt
drwxrwxr-x 2 zh zh 4096 Apr 17 21:21 ppt.txt
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ rm -r *.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 17 21:20 test
- rm -r 目录(删除目录及其下所有文件需要使用 -r 选项)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 3 zh zh 4096 Apr 17 21:14 ppt
-rw-rw-r-- 1 zh zh 0 Apr 17 21:16 test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ppt
ppt
├── d1
│ └── d2
└── kfc2 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ rm -r ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 17 21:16 test
三、修改文件内容的指令
0.0 补充知识:下载指令或插件
下载指令或插件的方法(一定要在root用户身份下,运行下面的指令):
# CentOS 7 64位系统下
yum install -y sofeware # sofeware表示指令或插件名
# Ubuntu 系统下
apt install -y sofeware
普通用户转root用户的方法(执行su 指令后输入root用户的密码):
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ su
Password:
[root@iZbp1dr1jtgcuih41mw88oZ test]# whoami
root
root用户转回普通用户的方法(su 普通用户,不用输密码):
[root@iZbp1dr1jtgcuih41mw88oZ test]# su zh
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ whoami
zh
0.1 补充知识:nano编辑器(用来修改普通文件内容)
使用方法:nano 文件名(注:只有普通文件可以进行内容编辑)
// 未内置nano编辑器的话,需手动下载(yum install -y nano)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ nano 123
nano 普通文件:进入普通文件内部,可以进行内容修改
修改完成之后的退出步骤:
1.ctrl + x(修改了文件内容才会有后续步骤)
2.y(表示保存修改)
3.回车
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 11 21:46 123
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ nano 123
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat 123
abcdefg
aaaaabbbbb
// cat 文件名:打印普通文件中的内容(cat 指令后面有介绍)
关于Linux nano编辑器的常用热键功能说明:
(1) C t r l + O Ctrl+O Ctrl+O:保存当前文件(Write Out),系统会提示确认文件名
(2) C t r l + X Ctrl+X Ctrl+X:退出nano编辑器,若有未保存内容会提示保存
(3) C t r l + K Ctrl+K Ctrl+K:剪切当前行内容到剪贴缓冲区
(4) C t r l + U Ctrl+U Ctrl+U:粘贴剪贴缓冲区内容到当前光标位置
(5) C t r l + C Ctrl+C Ctrl+C:显示当前光标位置的行号/列号
(6) A l t + U Alt+U Alt+U:撤销最后一次操作
(7) A l t + E Alt+E Alt+E:重做被撤销的操作
(8) C t r l + G Ctrl+G Ctrl+G:显示完整帮助文档(含所有热键说明)
1.echo 指令
语法: echo [选项] [字符串或变量]
功能:输出文本或变量的值(实际是默认把文本或变量的值输出到显示器)
常用选项:
• -e 启用转义字符
• -n 取消末尾换行
- echo 字符串或变量(把文本或变量的值输出到显示器)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo hasee
hasee
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'aaa bbb ccc'
aaa bbb ccc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ name=Linux
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "i love $name"
i love Linux
echo命令输出字符串时使用引号的规则与场景:
(1)一般情况下,当字符串不含空格、特殊符号(如$ 、; 、\ 等)时,才可直接省略引号。 否则会出现以下情景:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo hasee;
hasee
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'hasee;'
hasee;
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo hasee;abc
hasee
-bash: abc: command not found
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'hasee;abc'
hasee;abc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo hasee$abc
hasee
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'hasee$abc'
hasee$abc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo Hello World !>hello
-bash: !: event not found
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'Hello World !'>hello
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
Hello World !
想输出一个字符串时不加引号可能会得到很多奇怪的结果,所以,输出字符串时最好用单引号或双引号把它括成一个整体。
(2)用单引号 或 双引号 把字符串括起来在绝大多数场景下没有任何区别。不同之处在于:双引号允许变量替换,单引号禁止变量替换。 场景如下:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ name=Linux
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "i love $name"
i love Linux
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo 'i love $name'
i love $name
- echo -e 字符串或变量(把文本或变量的值输出到显示器,并启用转义字符)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo '第一行\n第二行\t制表符'
第一行\n第二行\t制表符
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo -e '第一行\n第二行\t制表符'
第一行
第二行 制表符
- echo -n 字符串或变量(把文本或变量的值输出到显示器,并取消末尾换行)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "无换行内容"; echo "接续输出"
无换行内容
接续输出
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo -n "无换行内容"; echo "接续输出"
无换行内容接续输出
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo -n "无换行内容"; echo -n "接续输出"
无换行内容接续输出[zh@iZbp1dr1jtgcuih41mw88oZ ~]$
- echo 字符串或变量>文件名(>的作用是输出重定向,把文本或变量的值输出到指定普通文件)
(1)echo 字符串或变量>文件名:每次输出内容到文件时,文件中旧内容会被清空,从而写入新内容;如果指定文件不存在,就会新建
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 13 19:47 tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ echo 'hello world'>tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat tx.txt
hello world
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ echo 'www.4399.com'>tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat tx.txt
www.4399.com
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 13 Apr 13 20:35 tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ echo 'one piece'>txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 10 Apr 13 20:41 txt
-rw-rw-r-- 1 zh zh 13 Apr 13 20:35 tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat txt
one piece
(2)>文件名(如果指定普通文件存在,清空文件内容;如果指定文件不存在,就会新建)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat txt
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ >txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 13 20:44 txt
-rw-rw-r-- 1 zh zh 13 Apr 13 20:35 tx.txt
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ >ace
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 13 20:45 ace
-rw-rw-r-- 1 zh zh 0 Apr 13 20:44 txt
-rw-rw-r-- 1 zh zh 13 Apr 13 20:35 tx.txt
- echo 字符串或变量>>文件名(>>的作用是追加重定向,把文本或变量的值追加到指定文件原有内容之后;如果指定文件不存在,就会新建)
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat ace
abcd
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ echo 'one'>>ace
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat ace
abcd
one
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ echo 'two'>>ace
[zh@iZbp1dr1jtgcuih41mw88oZ test]$ cat ace
abcd
one
two
四、查看文件属性和内容的指令
0.0 补充知识:文件属性和文件内容
文件 = 文件内容 + 文件属性(即文件的详细信息,包括大小、权限、时间戳等等)
凡是要对文件进行操作,不外乎以下两种:
(1)对文件内容进行操作
(2)对文件属性进行操作
0.1 补充知识:Linux下一切皆文件
Linux 将普通文件、目录、硬件设备(键盘/鼠标)、网络套接字等所有资源都抽象为文件对象,通过统一的文件操作接口(open/read/write/close)进行访问。
echo 字符串:将字符串内容打印到显示器,显示器也是一个文件,所以换一种更准确的描述是:向显示器文件进行写入
[root@iZbp1dr1jtgcuih41mw88oZ pts]# echo 'hello world'
hello world
cat>文件名:从键盘读取数据到指定的普通文件中,键盘也是一个文件,所以换一种更准确的描述是:从键盘文件里读取数据,输出到指定的普通文件中
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat>hello
hello world
12345
^C
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
hello world
12345
Linux下一切皆文件,Linux下的所有操作都可以理解成对文件的操作!
1.ls 指令
语法: ls [选项] [指定路径文件]
功能:列出当前目录下的所有文件
常用选项:
• -l 列出文件的详细信息
• -a 列出目录下的所有文件,包括以 . 开头的隐藏文件。
• -d 展示当前目录文件,而不是列出其下的文件。
- ls(展现当前目录下的所有文件,不包括隐藏文件)
ls -l(展现当前目录下的所有文件及其详细信息)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls
hello practice test test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 12 20:34 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
2.ls -a(展现当前目录下的所有文件,包括以 . 开头的隐含文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls
hello practice test test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -a
. .. .bash_history .bash_logout .bash_profile .bashrc hello practice test test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l -a
total 32
drwx------ 4 zh zh 4096 Apr 12 21:05 .
drwxr-xr-x. 4 root root 4096 Apr 10 20:58 ..
-rw------- 1 zh zh 612 Apr 11 22:32 .bash_history
-rw-r--r-- 1 zh zh 18 Nov 25 2021 .bash_logout
-rw-r--r-- 1 zh zh 193 Nov 25 2021 .bash_profile
-rw-r--r-- 1 zh zh 231 Nov 25 2021 .bashrc
-rw-rw-r-- 1 zh zh 0 Apr 12 20:34 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
// 每个目录文件中都会包含 . 和 . . 这两个隐藏文件,. 表示当前目录,. . 表示上级目录
注: 选项是可以组合起来使用,而且组合的写法可以有多种:
ls -l -a 还可以写成 ls -a -l 或 ls -la 或 ls -al(这四种写法效果都是一样的)
- ls -ld(展示当前目录文件及其详细信息)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ pwd
/home/zh
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 12 20:34 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -ld
drwx------ 4 zh zh 4096 Apr 12 21:05 .
- ls -l 指定路径文件(指定路径文件为目录文件,会列出其下文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── hello
├── practice
│ └── d1
│ └── d2
│ └── d3
├── test
│ ├── 123
│ └── 567
└── test.txt5 directories, 4 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ./test
total 4
-rw-rw-r-- 1 zh zh 21 Apr 12 22:27 123
-rw-rw-r-- 1 zh zh 0 Apr 12 22:39 567
指定路径文件为普通文件,会展示该普通文件
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ./test/123
-rw-rw-r-- 1 zh zh 21 Apr 12 22:27 ./test/123
2.tree 指令(如未内置需下载)
语法: tree [选项] 文件
功能: tree命令用于以树状图形式展示目录结构
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── hello
├── practice
│ └── wy.txt
├── test
│ ├── ace
│ ├── txt
│ └── tx.txt
└── test.txt2 directories, 6 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 16
-rw-rw-r-- 1 zh zh 18 Apr 13 22:47 hello
drwxrwxr-x 2 zh zh 4096 Apr 13 19:50 practice
drwxrwxr-x 2 zh zh 4096 Apr 13 20:56 test
-rw-rw-r-- 1 zh zh 10 Apr 13 16:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ./practice
total 0
-rw-rw-r-- 1 zh zh 0 Apr 13 19:50 wy.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ./test
total 8
-rw-rw-r-- 1 zh zh 13 Apr 13 20:54 ace
-rw-rw-r-- 1 zh zh 0 Apr 13 20:44 txt
-rw-rw-r-- 1 zh zh 13 Apr 13 20:35 tx.txt
3.stat 指令
语法:stat [选项] 文件名
功能: 显示文件的详细状态信息,包括文件权限、文件类型、所有者、大小、时间戳等。
常用选项:
• -f 显示文件系统信息
- stat 文件名(显示文件的详细状态信息)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat helloFile: ‘hello’Size: 18 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 922803 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-13 22:48:05.241711070 +0800
Modify: 2025-04-13 22:47:59.520496972 +0800
Change: 2025-04-13 22:47:59.520496972 +0800Birth: -
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat testFile: ‘test’Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd01h/64769d Inode: 922645 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1000/ zh) Gid: ( 1000/ zh)
Access: 2025-04-13 20:56:10.687357382 +0800
Modify: 2025-04-13 20:56:09.529313993 +0800
Change: 2025-04-13 20:56:09.529313993 +0800Birth: -
- stat -f 文件名(显示文件系统信息)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ stat -f testFile: "test"ID: 5198b778e3733252 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 10288203 Free: 9603256 Available: 9127329
Inodes: Total: 2621440 Free: 2558688
4.cat 指令
语法: cat [选项] [文件]
功能: 打印普通文件的内容(实际是默认把指定文件内容输出到显示器文件)
常用选项:
• -n 对输出的所有行编号
• -b 对非空输出行编号
• -s 不输出多行空行
首先使用nano编辑普通文件hello中的内容:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 12 20:34 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ nano hello
- cat 文件名(打印普通文件的内容 )
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
aaaaaaaaaaaaaaaa
bbbbbbbbbbbb
ccccccccc
ddddddeeeeeeee
ffffffffffffggggggggggggggg
- cat -n 文件名(打印普通文件的内容,对输出的所有行编号)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat -n hello1 aaaaaaaaaaaaaaaa2 bbbbbbbbbbbb3 ccccccccc4 dddddd5 6 7 8 eeeeeeee9 ffffffffffff10 11 12 13 ggggggggggggggg
cat -b 文件名(打印普通文件的内容,对非空输出行编号)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat -b hello1 aaaaaaaaaaaaaaaa2 bbbbbbbbbbbb3 ccccccccc4 dddddd5 eeeeeeee6 ffffffffffff7 ggggggggggggggg
- -s 选项(不输出多行空行,折叠空行)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat -ns hello1 aaaaaaaaaaaaaaaa2 bbbbbbbbbbbb3 ccccccccc4 dddddd5 6 eeeeeeee7 ffffffffffff8 9 ggggggggggggggg
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat -bs hello1 aaaaaaaaaaaaaaaa2 bbbbbbbbbbbb3 ccccccccc4 dddddd5 eeeeeeee6 ffffffffffff7 ggggggggggggggg
- cat 指令 :需以文件(普通文件 或 键盘文件)作为输入,默认输出到显示器文件(可以输出重定向到普通文件)
单独写一个cat指令,就会默认从键盘文件输入,输出到显示器文件。 此时观察到的现象就是无论我们输入什么内容,再按回车,显示器会输出同样的内容(按ctrl + c可以结束此过程):
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat
abcdefg
abcdefg
1234567
1234567
hhhhhhhh
hhhhhhhh
^C
cat>文件名(表示从键盘文件输入,输出到指定的普通文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat>hello
123456789
jasjsajidodjo
^C
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
123456789
jasjsajidodjo
cat 文件… > 文件名 (cat可以合并多个文件的内容;合并后的文件不存在,会新建)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 12
-rw-rw-r-- 1 zh zh 45 Apr 13 16:22 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 0 Apr 12 20:25 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.txt
123
cat
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
123456789
jasjsajidodjo
369852147
assdfkwodk
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.txt hello>cmb
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 20
-rw-rw-r-- 1 zh zh 55 Apr 13 16:37 cmb
-rw-rw-r-- 1 zh zh 45 Apr 13 16:22 hello
drwxrwxr-x 3 zh zh 4096 Apr 11 17:07 practice
drwxrwxr-x 2 zh zh 4096 Apr 12 22:39 test
-rw-rw-r-- 1 zh zh 10 Apr 13 16:36 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat cmb
123
cat
123456789
jasjsajidodjo
369852147
assdfkwodk
5.tac 指令
语法: tac [选项] 文件
功能:cat的反写,功能与cat相反,按行逆序输出文件内容
cat的反写,功能与cat相反,按行逆序输出文件内容
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
zxcvbnm
asdfghjklqwertyuiop
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tac hello
qwertyuiopasdfghjkl
zxcvbnm
6.more 指令
语法: more [选项] 文件
功能:more 指令,功能类似 cat 。区别是cat适合小文件,直接显示所有内容,用cat查看大文件会直接刷屏;more用于分页查看文件内容,适合浏览大文件,more是单向逐页查看文件内容(空格键向下翻页,Enter逐行滚动),无法回退查看已显示内容。more在查看之前会加载整个文件至内存缓冲区。
常用选项:
• -数字:指定输出行数
快捷按键:
• q:退出more
Linux系统中的日志文件是大文件,CentOS 7 的日志文件默认存储在 /var/log 目录下,包含以下关键文件:
(1)/var/log/messages:综合系统日志,记录内核、服务等通用信息
(2)/var/log/secure:安全相关日志(如用户登录、认证事件)
(3)/var/log/cron:定时任务执行记录
(4)/var/log/boot.log:系统启动过程日志
(5)/var/log/maillog:邮件服务日志
- more 文件(使用more指令来查看一下日志文件,注:要在root身份下查看,普通用户权限不够)
[root@iZbp1dr1jtgcuih41mw88oZ log]# more /var/log/messages
一次查看一整页:
按空格,一次翻一页(看左下角进度条,从4%变到8%,证明是向下翻了一页,每页占总内容的4%):
- more -数字 文件(指定输出行数,一次查看指定输出行数内容,按空格向下翻指定行数内容)
[root@iZbp1dr1jtgcuih41mw88oZ log]# more -10 /var/log/messages
一次查看指定行数内容:
按空格,一次翻指定行数内容(看左下角进度条,从1%变到2%,证明是向下翻了指定行数内容,指定行数内容占总内容的1%):
7.less 指令(适合浏览大文件)
语法: less [选项] 文件
功能:less与more类似,分页查看文件内容,适合浏览大文件,而且less是支持双向自由浏览(上下箭头、PgUp/PgDn、Ctrl+B/Ctrl+F等),允许回退和跳转至文件任意位置。less在查看之前不会加载整个文件,而是按需加载,仅读取当前显示内容,内存占用更优。
常用选项:
• -N 显示每行的行号
• -i 忽略搜索时的大小写
快捷按键:
• /字符串:向下搜索“字符串”的功能
• ?字符串:向上搜索“字符串”的功能
• n:重复前⼀个搜索(与 / 或 ? 有关)
• N:反向重复前⼀个搜索(与 / 或 ? 有关)
• q:退出less
使用less 指令去查看日志文件不能很好演示其搜索的功能,所以我们可以自己写一个大文件来演示其功能。下面这行代码的功能是连续写入1000行"hello bit"到普通文件test.txt中:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ count=1; while [ ${count} -le 1000 ]; do echo "hello bit"; let count++; done>test.txt
- less -N 文件(分页查看文件内容,且显示每行的行号)
先演示不带 -N 选项的效果,发现观察这种重复的内容很不直观:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ less test.txt
带 -N 选项的效果:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ less -N test.txt
再进行less 指令的搜索功能之前,使用nano编辑器对test.txt文件内容进行了一些修改(在16行、276行、611行 以及 909行附近添加了一些内容)
- /字符串:向下搜索“字符串”的功能 (?字符串,与 /字符串 搜索方向相反)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ less -N test.txt
输入 /good,再按回车,开始向下搜索“good”:
接下来按 n,就会重复前⼀个搜索(与 / 或 ? 有关),这里前一个搜索是 / ,按 n 就会重复向下找字符串的操作。
- -i 选项:忽略搜索时的大小写
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ less -N -i test.txt
当忽略搜索时的大小写,再输入 /good,进行向下搜索时,good、GooD、gOOd、GOOd这些大小写混搭的全部能搜到:
8.head 指令
语法:head [选项] 文件
功能:head 用来显示指定文件开头内容至标准输出中,默认head命令打印其相应文件的开头10行。
常用选项:
• -数字:显示的行数
我们自己写一个大文件来演示其功能。下面这行代码的功能是写入"hello 0"、“hello 1”、…“hello 99”、"hello 100"共100行内容到普通文件hello中:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ count=1; while [ ${count} -le 100 ]; do echo "hello $count"; let count++; done>hello
- head 文件(默认head命令打印指定文件的开头10行)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ head hello
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
- head -数字 文件(head命令打印指定文件开头内容的指定行数)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ head -18 hello
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
hello 11
hello 12
hello 13
hello 14
hello 15
hello 16
hello 17
hello 18
9.tail 指令
语法:tail [选项] [文件]
功能:用于显示指定文件末尾内容,默认tail命令打印其相应文件的末尾10行。常用查看日志文件。
常用选项:
• -数字:显示的行数
• -f 循环读取
- tail -数字 文件(tail命令打印指定文件末尾内容的指定行数)
默认tail命令打印其相应文件的末尾10行:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tail hello
hello 91
hello 92
hello 93
hello 94
hello 95
hello 96
hello 97
hello 98
hello 99
hello 100
tail命令打印指定文件末尾内容的指定行数:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tail -16 hello
hello 85
hello 86
hello 87
hello 88
hello 89
hello 90
hello 91
hello 92
hello 93
hello 94
hello 95
hello 96
hello 97
hello 98
hello 99
hello 100
五、复制和移动文件的指令
1.cp 指令
语法:cp [选项] 源文件 目标文件
(源文件:普通文件或目录;目标文件:普通文件或目录)
功能:复制普通文件或目录(如同时指定两个以上的文件或目录,且最后的目的地是⼀个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中)
常用选项:
• -i 覆盖文件之前先询问用户
• -f 覆盖文件前不会再询问用户
• -r 递归处理,将指定目录下的普通文件与子目录⼀并处理,复制到指定目录下
- cp 普通文件 目标文件
(1)目标文件不存在,会新建一个目标普通文件(并拷贝源普通文件中内容)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 12
-rw-rw-r-- 1 zh zh 10062 Apr 14 14:53 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "hello world">test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.txt
hello world
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp test.txt hello.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 12 Apr 15 21:04 hello.txt
-rw-rw-r-- 1 zh zh 12 Apr 15 21:03 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello.txt
hello world
(2)目标文件为普通文件,目标文件中的内容会被源文件内容覆盖
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 12 Apr 15 21:04 hello.txt
-rw-rw-r-- 1 zh zh 12 Apr 15 21:03 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "one piece">test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test.txt
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp test.txt hello.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello.txt
one piece
使用 -i 选项在覆盖文件会之前先询问用户(用户输入y表示同意,n表示拒绝):
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp -i test.txt hello.txt
cp: overwrite ‘hello.txt’? y
(3)目标文件为目录文件,源文件会被复制到该目标文件下
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mkdir king
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp test.txt king
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 12
-rw-rw-r-- 1 zh zh 10 Apr 15 21:09 hello.txt
drwxrwxr-x 2 zh zh 4096 Apr 15 21:11 king
-rw-rw-r-- 1 zh zh 10 Apr 15 21:08 test.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ./king
./king
└── test.txt0 directories, 1 file
- cp 多个文件 目录
(1)多个普通文件 可以直接 复制到指定目录
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 12
-rw-rw-r-- 1 zh zh 10 Apr 15 21:18 hello.txt
-rw-rw-r-- 1 zh zh 10 Apr 15 21:08 test.txt
drwxrwxr-x 2 zh zh 4096 Apr 15 21:38 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc0 directories, 0 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp *.txt ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc
├── hello.txt
└── test.txt0 directories, 2 files
// * 表示通配符,匹配所有.txt结尾的文件名
(2)多个文件中包含目录文件,需要使用 -r 选项,否则目录文件不能被复制到指定目录下
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 16
drwxrwxr-x 2 zh zh 4096 Apr 15 21:43 ggb.txt
-rw-rw-r-- 1 zh zh 10 Apr 15 21:18 hello.txt
-rw-rw-r-- 1 zh zh 10 Apr 15 21:08 test.txt
drwxrwxr-x 2 zh zh 4096 Apr 15 21:43 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc0 directories, 0 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp -r *.txt ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc
├── ggb.txt
├── hello.txt
└── test.txt1 directory, 2 files
- cp 目录 目录( 复制目录文件需使用 -r 选项,将指定目录下的普通文件与子目录⼀并处理,复制到指定目录下)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ll
total 8
drwxrwxr-x 3 zh zh 4096 Apr 15 21:57 ppt
drwxrwxr-x 2 zh zh 4096 Apr 15 21:54 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ppt
ppt
├── d1
│ └── d2
└── test.txt2 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc0 directories, 0 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cp -r ppt ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc
└── ppt├── d1│ └── d2└── test.txt3 directories, 1 file
2.mv 指令
语法: mv [选项] 源文件 目标文件
(源文件:普通文件或目录;目标文件:普通文件或目录)
功能:
mv命令是move的缩写,可以用来移动文件或者将文件改名。
视mv命令中第二个参数(目标文件)类型的不同(普通文件还是目录),mv命令将文件重命名或将其移至一个新的目录中:
(1)当第二个参数是已存在的普通文件时,mv命令将源文件重命名为目标文件名并覆盖目标文件(移动到目标文件路径下进行覆盖),此时,源文件只能有一个(且只能是普通文件)
(2)当第二个参数是不存在的文件名,mv命令将源文件重命名并移动到目标文件路径下,此时,源文件只能有一个(可以是普通文件或目录),它将所给的源文件重命名为给定的目标文件名。
(3)当第二个参数是已存在的目录名称时,源文件(普通文件或目录)参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。
常用选项:
• -i :覆盖文件之前先询问用户
• -f :覆盖文件前不会再询问用户
- mv 普通文件 普通文件
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
drwxrwxr-x 2 zh zh 4096 Apr 17 18:57 ufc
-rw-rw-r-- 1 zh zh 10 Apr 17 19:01 you
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ufc
total 4
-rw-rw-r-- 1 zh zh 12 Apr 17 19:01 me
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── ufc
│ └── me
└── you1 directory, 2 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat you
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat ufc/me
hello world
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv you ufc/me
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
└── ufc└── me1 directory, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat ufc/me
one piece
mv you ufc/me:第二个参数是已存在的普通文件me时,mv命令将源文件you重命名为目标文件名me并移动到目标文件路径下对其覆盖。
- mv 源文件 文件名(路径下不存在对应文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 10 Apr 17 19:23 hello
drwxrwxr-x 2 zh zh 4096 Apr 17 19:02 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat hello
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc
└── me0 directories, 1 file
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv hello kfc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv ufc ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 10 Apr 17 19:23 kfc
drwxrwxr-x 2 zh zh 4096 Apr 17 19:02 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat kfc
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ppt
ppt
└── me0 directories, 1 file
当第二个参数指定的文件名在对应路径下并不存在对应文件,它会将所给的源文件(普通文件或目录)重命名为给定的目标文件名,并将其移动到目标文件路径(即使目标文件原来是不存在的,但其指定的路径是原来就存在的)。 如下:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 10 Apr 17 19:36 kfc
drwxrwxr-x 2 zh zh 4096 Apr 17 19:35 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── kfc
└── ppt└── me1 directory, 2 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat kfc
one piece
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv kfc ppt/mma
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
└── ppt├── me└── mma1 directory, 2 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat ppt/mma
one piece
- mv 多个文件 目录
当第二个参数是已存在的目录名称时,源文件(普通文件或目录)参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。
(注:源文件参数中普通文件和目录可以混搭,会一起被移动到目标目录下)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 17 19:52 d1.txt
-rw-rw-r-- 1 zh zh 0 Apr 17 19:52 d2.txt
drwxrwxr-x 2 zh zh 4096 Apr 17 19:52 d3
drwxrwxr-x 2 zh zh 4096 Apr 17 19:51 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ mv *.txt d3 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
drwxrwxr-x 3 zh zh 4096 Apr 17 19:52 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree ufc
ufc
├── d1.txt
├── d2.txt
└── d31 directory, 2 files
六、查找文件及文件中指定内容的指令
1.find 指令
(1)功能概述
find
是 Linux 中功能最强大的文件搜索工具,支持递归搜索、多条件筛选和对搜索结果执行操作。核心能力包括:
- 按文件名、类型、大小、时间戳等属性搜索文件
- 在指定目录及其子目录中递归搜索
- 对搜索结果执行删除、移动、修改权限等操作
(2)基本语法
find [搜索路径] [搜索条件] [操作]
- 搜索路径:默认为当前目录(
.
),可指定绝对路径(如/home
)或相对路径 - 搜索条件:通过选项定义过滤规则(如按文件名、文件大小等)
- 操作:对匹配的文件执行操作,默认操作为
-print
(输出路径)
(3)常用搜索条件
条件参数 | 功能说明 | 示例 |
---|---|---|
-name | 按文件名匹配(区分大小写) | find . -name "*.txt" |
-iname | 按文件名匹配(不区分大小写) | find /var -iname "Log*" |
-type | 按文件类型过滤 | find ~ -type d (搜索目录) |
-size | 按文件大小过滤 | find / -size +100M (>100MB) |
-mtime | 按修改时间过滤(天数) | find . -mtime -7 (7天内修改) |
-user | 按文件所有者过滤 | find /home -user alice |
-perm | 按文件权限过滤 | find . -perm 644 |
-maxdepth | 限制搜索深度 | find /etc -maxdepth 2 |
(4)操作参数
操作参数 | 功能说明 | 示例 |
---|---|---|
-print | 输出文件路径(默认操作) | find . -name "*.log" |
-delete | 直接删除匹配的文件 | find /tmp -mtime +30 -delete |
-exec | 执行自定义命令 | find . -name "*.bak" -exec rm {} \; |
-ok | 交互式确认后执行命令 | find . -type f -ok chmod 644 {} \; |
-ls | 显示详细信息(类似 ls -l ) | find /var/log -name "*.log" -ls |
(5)典型使用场景
- 按文件名搜索
# 搜索当前目录下所有 .jpg 文件
find . -name "*.jpg"
- 按文件大小过滤
# 搜索 /var 目录下大于 100MB 的文件
find /var -size +100M
- 按时间戳搜索
# 查找 7 天内修改过的 .log 文件
find /var/log -name "*.log" -mtime -7
- 组合条件搜索
# 搜索当前用户拥有的可执行文件(排除目录)
find ~ -type f -perm /u=x -user $(whoami)
- 批量操作文件
# 删除所有空目录
find . -type d -empty -delete# 修改 .conf 文件权限为 644
find /etc -name "*.conf" -exec chmod 644 {} \;
2.which 指令
语法:which 指令
功能:搜索系统指定路径下的指令
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ which ls
alias ls='ls --color=auto'/usr/bin/ls
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ which cd
/usr/bin/cd
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ which echo
/usr/bin/echo
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ which cat
/usr/bin/cat
Linux下一切皆文件,指令实际上就是系统指定目录/usr/bin下的一个可执行文件
which 实际上是 Linux 中用于定位可执行文件路径的命令。它通过搜索用户环境变量 $PATH 中的目录,返回第一个匹配的命令路径。此外还支持别名处理,会默认显示别名(如 ls 可能被定义为 ls --color=auto)
3.whereis 指令
语法:whereis [选项] 文件名
功能:whereis 是 Linux 中用于快速定位程序文件的命令,支持搜索二进制文件、源代码和手册页(man pages)的路径。
常用选项:
• -b :仅搜索二进制文件(可执行程序)
• -m :仅搜索手册页(man pages)
• -s :仅搜索源代码文件
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz
4.grep 指令 (在文件中搜索字符串)
语法: grep [选项] 字符串 文件
功能:在文件中搜索字符串,将搜索到包含指定字符串的行打印出来
常用选项:
• -i :忽略大小写的不同,所以大小写视为相同
• -n :为输出行添加行号
• -v :反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行
- grep -i 字符串 文件(将搜索到包含指定字符串的行打印出来,忽视字符串大小写)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cat test
abbabc
Abcbc
aacdff
deskdox
zzzox
pptdy[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep "abc" test
abbabc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep -i "abc" test
abbabc
Abcbc
- -n(为输出行添加行号 )
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep -i "abc" test
abbabc
Abcbc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep -in "abc" test
1:abbabc
2:Abcbc
- -v :反向选择,即打印出没有包含 ‘搜寻字符串’ 内容的行
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep -vn "abc" test
2:Abcbc
3:aacdff
4:deskdox
5:zzzox
6:pptdy
7:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ grep -ivn "abc" test
3:aacdff
4:deskdox
5:zzzox
6:pptdy
7:
七、压缩和解压文件的指令
0.补充知识:lrzsz工具
这个工具用于 windows 机器和远端的 Linux 机器通过 XShell 传输文件。(先在Linux 机器中安装lrzsz工具,才能使用下述功能)
- 远端的 Linux 机器通过 XShell 向 windows 机器传输文件:
sz 指令:从Linux下载文件到Windows
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 736 Apr 18 21:31 test.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ sz test.zip
我选择保存到windows 机器的桌面,再按确定:
2. windows 机器通过 XShell 向 远端的 Linux 机器传输文件:
(方法1)rz 指令:从Windows上传文件到Linux
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 736 Apr 18 21:31 test.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ rz
我选择 windows 桌面中的 hello.zip 文件,再按打开:
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 744
-rw-r--r-- 1 zh zh 756378 Apr 18 22:08 hello.zip
-rw-rw-r-- 1 zh zh 736 Apr 18 21:31 test.zip
(方法2)直接拖拽windows 机器中的文件到xshell界面,也可以传输文件到Linux
直接拖拽windows 机器中的 piece.zip 文件到xshell界面,可以直接传输该文件到Linux:
1.zip/unzip 指令(如未内置需下载)
语法: zip 压缩文件 目录或普通文件
( 压缩文件自己命名,要以 .zip 结尾)
功能:将目录或普通文件压缩成zip格式(可以同时列多个文件一起压缩)
常用选项
• -r:递归处理,将指定目录下的所有文件和子目录一并处理
语法: unzip 压缩文件
功能:解压zip文件(默认解压到当前目录下)
常用选项
• -d:unzip 压缩文件 -d 指定目录(将zip文件解压到指定目录)
• -l:unzip -l 压缩文件(查看压缩包内文件列表)
- zip 压缩文件 普通文件
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 0
-rw-rw-r-- 1 zh zh 0 Apr 18 20:30 d1.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ zip d1.zip d1.txtadding: d1.txt (stored 0%)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 18 20:30 d1.txt
-rw-rw-r-- 1 zh zh 162 Apr 18 20:37 d1.zip
unzip 压缩文件(默认解压到当前目录下)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 162 Apr 18 20:37 d1.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ unzip d1.zip
Archive: d1.zipextracting: d1.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 0 Apr 18 20:30 d1.txt
-rw-rw-r-- 1 zh zh 162 Apr 18 20:37 d1.zip
unzip 压缩文件 -d 指定目录(将zip文件解压到指定目录)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 162 Apr 18 20:37 d1.zip
drwxrwxr-x 2 zh zh 4096 Apr 18 21:04 ppt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ppt
total 0
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ unzip d1.zip -d ppt
Archive: d1.zipextracting: ppt/d1.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ppt
total 0
-rw-rw-r-- 1 zh zh 0 Apr 18 20:30 d1.txt
- zip -r 压缩文件 目录
当不带 -r 选项压缩目录文件时,不会压缩目录文件下的文件:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
drwxrwxr-x 3 zh zh 4096 Apr 18 21:11 mma
drwxrwxr-x 2 zh zh 4096 Apr 18 21:10 ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── mma
│ ├── d
│ │ └── d2.txt
│ └── d1.txt
└── ufc3 directories, 2 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ zip ufc/mma.zip mmaadding: mma/ (stored 0%)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cd ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 158 Apr 18 21:13 mma.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ unzip mma.zip
Archive: mma.zipcreating: mma/
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 8
drwxrwxr-x 2 zh zh 4096 Apr 18 21:11 mma
-rw-rw-r-- 1 zh zh 158 Apr 18 21:13 mma.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ tree mma
mma0 directories, 0 files
带 -r 选项压缩目录文件时,将指定目录下的所有文件和子目录一并压缩:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── mma
│ ├── d
│ │ └── d2.txt
│ └── d1.txt
└── ufc3 directories, 2 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ zip -r ufc/mma.zip mmaadding: mma/ (stored 0%)adding: mma/d/ (stored 0%)adding: mma/d/d2.txt (stored 0%)adding: mma/d1.txt (stored 0%)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cd ufc
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 598 Apr 18 21:18 mma.zip
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ unzip mma.zip
Archive: mma.zipcreating: mma/creating: mma/d/extracting: mma/d/d2.txt extracting: mma/d1.txt
[zh@iZbp1dr1jtgcuih41mw88oZ ufc]$ tree .
.
├── mma
│ ├── d
│ │ └── d2.txt
│ └── d1.txt
└── mma.zip2 directories, 3 files
- zip 压缩文件 多个文件(包含目录文件记得加-r选项,否则目录文件下的文件不会被一起压缩)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d1.txt
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d2.txt
drwxrwxr-x 2 zh zh 4096 Apr 18 21:31 ppt
drwxrwxr-x 3 zh zh 4096 Apr 18 21:28 test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── d1.txt
├── d2.txt
├── ppt
└── test├── d3.txt└── dnf3 directories, 3 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ zip -r ppt/test.zip *.txt testadding: d1.txt (stored 0%)adding: d2.txt (stored 0%)adding: test/ (stored 0%)adding: test/d3.txt (stored 0%)adding: test/dnf/ (stored 0%)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── d1.txt
├── d2.txt
├── ppt
│ └── test.zip
└── test├── d3.txt└── dnf3 directories, 4 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ unzip ppt/test.zip -d ppt
Archive: ppt/test.zipextracting: ppt/d1.txt extracting: ppt/d2.txt creating: ppt/test/extracting: ppt/test/d3.txt creating: ppt/test/dnf/
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── d1.txt
├── d2.txt
├── ppt
│ ├── d1.txt
│ ├── d2.txt
│ ├── test
│ │ ├── d3.txt
│ │ └── dnf
│ └── test.zip
└── test├── d3.txt└── dnf5 directories, 7 files
2.tar 指令
语法:(1)压缩: tar [选项] 压缩文件 目录或普通文件
(2)解压: tar [选项] 压缩文件 -C 目录
功能:将多个文件/目录打包为单个文件(归档),再进行压缩;解压压缩文件。
常用选项:
• -c :建立⼀个归档文件
• -x :解压⼀个压缩文件(或归档文件)
• -t :查看压缩文件(或归档文件)中内容
• -z :gzip压缩
• -j :bzip2压缩
• -v :压缩或解压的过程中显示文件(也就是显示操作过程)
• -f :指定压缩文件名(必须选项)。注:在 f 之后要立即接压缩文件名,不要再加参数!
• -C : 解压到指定目录
注意,在参数 f 之后的压缩文件名是自己取的,习惯上都用 .tar 后缀来作为辨识:
如果加 z 参数(使用gzip压缩),则以 .tar.gz 或 .tgz 作为后缀来代表 gzip 压缩过的 压缩文件名
如果加 j 参数(使用bzip2压缩),则以 .tar.bz2 作为后缀来代表 bzip2 压缩过的 压缩文件名
- tar -czvf 压缩文件 多个文件
将多个文件打包为单个文件(归档文件),再对归档文件进行gzip压缩形成压缩文件:
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d1.txt
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d2.txt
drwxrwxr-x 2 zh zh 4096 Apr 19 14:22 ppt
drwxrwxr-x 3 zh zh 4096 Apr 18 21:28 test
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── d1.txt
├── d2.txt
├── ppt
└── test├── d3.txt└── dnf3 directories, 3 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tar -czvf ppt/test.tgz *.txt test
d1.txt
d2.txt
test/
test/d3.txt
test/dnf/
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ tree .
.
├── d1.txt
├── d2.txt
├── ppt
│ └── test.tgz
└── test├── d3.txt└── dnf3 directories, 4 files
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ ls -l ppt
total 4
-rw-rw-r-- 1 zh zh 183 Apr 19 14:23 test.tgz
tar -xzvf 压缩文件(默认解压到当前目录下)
解压一个 gzip 压缩的压缩文件:
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 4
-rw-rw-r-- 1 zh zh 183 Apr 19 14:23 test.tgz
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ tar -xzvf test.tgz
d1.txt
d2.txt
test/
test/d3.txt
test/dnf/
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ ls -l
total 8
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d1.txt
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d2.txt
drwxrwxr-x 3 zh zh 4096 Apr 18 21:28 test
-rw-rw-r-- 1 zh zh 183 Apr 19 14:23 test.tgz
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ tree .
.
├── d1.txt
├── d2.txt
├── test
│ ├── d3.txt
│ └── dnf
└── test.tgz2 directories, 4 files
八、时间和日期相关的指令
0. 补充知识:时间戳和时区
核心概念解析:
-
时间戳 (Timestamp)
时间戳是从 UTC 时间 1970年1月1日00:00:00(Unix纪元) 开始计算的秒数或毫秒数,表示一个绝对的时间点,与时区无关。例如:1620000000(秒级时间戳)
,代表2021-5-3 00:00:00
UTC时间(计算方法:1970年1月1日00:00:00 加上 1620000000s)。 -
时区 (Time Zone)
时区是地理区域遵循的统一时间规则,例如:- UTC+8(北京时间,东8区):比UTC时间早8小时
- UTC-5(纽约时间):比UTC时间晚5小时
( 时区用于将时间戳转换为本地可读的时间格式)
时间戳与时区的关系:
-
时间戳的绝对性
时间戳是与时区无关的全局统一值,例如:- 无论用户处于哪个时区,时间戳
1620000000
始终指向同一时刻。 - 文件中时间(Access Time、Modify Time 和 Change Time)的记录基于时间戳,在不同时区的系统中查看时会被转换为本地时间。
- 无论用户处于哪个时区,时间戳
-
时区的作用
时区决定了如何将时间戳转换为本地时间字符串。例如:1620000000
在UTC+8时区显示为2021-05-03 08:00:00
- 在UTC-5时区显示为
2021-05-02 19:00:00
。
1.date 指令
语法:date [选项] [+设定格式]
功能:指定格式显示时间
- 在显示方面,使用者可以设定欲显示的格式,格式设定为⼀个加号后接数个标记,其中常用的标记列表如下:
• %Y : 完整年份 (0000~9999)
• %m : 月份 (01~12)
• %d : 日 (01~31)
• %H : 小时 (00~23)
• %M : 分钟 (00~59)
• %S : 秒 (00~59)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date
Tue Apr 15 12:45:38 CST 2025
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y/%m/%d-%H:%M:%S
2025/04/15-12:45:42
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y.%m.%d/%H:%M:%S
2025.04.15/12:46:20
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y-%m-%d~%H:%M:%S
2025-04-15~12:50:45
- 时间戳与时间的转换:
• 时间->时间戳:date +%s
• 时间戳->时间:date -d @1508749502
• Unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%s
1744717558
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%s
1744717571
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date -d @0
Thu Jan 1 08:00:00 CST 1970
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y/%m/%d-%H:%M:%S -d @0
1970/01/01-08:00:00
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y/%m/%d-%H:%M:%S -d @1000000000
2001/09/09-09:46:40
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ date +%Y/%m/%d-%H:%M:%S -d @1744717571
2025/04/15-19:46:11
2.cal 指令
语法: cal [选项] [年份]
功能:用于查看日历等时间信息
常用选项:
• -3 显示系统前一个月,当前月,下一个月的日历
• -y 显示当前年份的日历
- cal -3(显示系统前一个月,当前月,下一个月的日历 )
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ calApril 2025
Su Mo Tu We Th Fr Sa1 2 3 4 56 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cal -3March 2025 April 2025 May 2025
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 1 2 3 4 5 1 2 32 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 109 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
- cal 年份(显示指定年份的日历)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cal 19991999 January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 1 2 3 4 5 6 1 2 3 4 5 63 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
31April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 1 1 2 3 4 54 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 3030 31July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 1 2 3 4 5 6 7 1 2 3 44 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 30October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 1 2 3 4 5 6 1 2 3 43 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
- cal -y(显示当前年份的日历)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ cal -y2025 January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 1 15 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 2930 31April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 5 1 2 3 1 2 3 4 5 6 76 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
27 28 29 30 25 26 27 28 29 30 31 29 30July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 5 1 2 1 2 3 4 5 66 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
27 28 29 30 31 24 25 26 27 28 29 30 28 29 3031October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa1 2 3 4 1 1 2 3 4 5 65 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 3130
九、其他指令
1.alias 指令(为命令创建别名)
alias 是 Linux 中用于为命令创建别名的核心工具,可将复杂命令简化为短指令,提升操作效率。别名支持临时或永久配置,是日常开发和系统管理的常用功能
语法:
(1)定义别名:alias [别名]=‘原始命令’
(2)查看当前所有别名:alias
(3)查看指定别名:alias [别名]
(4)删除指定别名:unalias [别名]
配置方式:
(1)临时生效(仅当前终端会话有效)
直接在终端输入alias
命令,例如:alias rm='rm -i'
(2)永久生效(需写入配置文件)
1.用户级配置:编辑~/.bashrc
或~/.bash_profile
,添加别名后执行source ~/.bashrc
2.系统级配置(所有用户生效):编辑/etc/bash.bashrc
或/etc/profile
- alias(查看当前所有别名)
alias [别名] (查看指定别名)
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ alias
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 vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ alias ll
alias ll='ls -l --color=auto'
- alias [别名]=‘原始命令’ (定义别名)
unalias [别名](删除指定别名)
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ alias la='ls -la'
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ alias la
alias la='ls -la'
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ la
total 16
drwxrwxr-x 3 zh zh 4096 Apr 19 14:33 .
drwx------ 4 zh zh 4096 Apr 19 14:22 ..
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d1.txt
-rw-rw-r-- 1 zh zh 0 Apr 18 21:27 d2.txt
drwxrwxr-x 3 zh zh 4096 Apr 18 21:28 test
-rw-rw-r-- 1 zh zh 183 Apr 19 14:23 test.tgz[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ unalias la
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ alias la
-bash: alias: la: not found
[zh@iZbp1dr1jtgcuih41mw88oZ ppt]$ la
-bash: la: command not found
2.bc 指令(进行计算)
bc是“Basic Calculator”的缩写,是一个用于数学计算的高精度计算器语言,支持交互式运算、脚本执行及复杂数学函数计算。 以下是其基本功能介绍:
- 交互模式(终端输入bc进入交互界面)
在交互模式中,用户输入计算式再按回车,就会立刻得到结果 (站在文件角度分析:从键盘文件输入数据,bc对输入数据进行处理,再将结果输出到显示器文件)
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ 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'.
100+11
111
1.5+5
6.5
3.14*5.5
17.27
^C
(interrupt) Exiting bc.
使用 ctrl+c 或者 输入quit再按回车 可以退出交互模式
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ 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'.
11+5
16
1.5+4
5.5
3.14*5.5
17.27
quit
- 非交互模式(通过管道传递表达式)
| 是管道,因为linux下一切皆文件,所以 | 也是文件,叫管道文件。
站在文件角度分析下列现象:echo 指令 将字符串"5 * 7"输出到管道文件 | 中,然后管道文件中的内容又作为bc 指令的输入,bc对输入数据进行处理,再将结果输出到显示器文件。
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "5 * 7" | bc
35
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ echo "3.14 * 7.5" | bc
23.55
3.uname 指令(获取操作系统的相关信息)
语法:uname [选项]
功能: uname用来获取电脑和操作系统的相关信息(例如:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息 )
常用选项:
• -a:详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
[zh@iZbp1dr1jtgcuih41mw88oZ ~]$ uname -a
Linux iZbp1dr1jtgcuih41mw88oZ 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
4.shutdown 指令(关机)
语法:shutdown [选项]
常见选项:
• -h:将系统的服务停掉后,立即关机。
• -r:在将系统的服务停掉之后就重新启动
• -t 秒数:-t 后面加秒数,亦即“过几秒后关机”的意思