承接上文,本文将继续补充介绍一些Linux基本指令,以及探讨指令究竟是什么,又什么是权限?权限是 Linux 系统中非常重要的一部分,它决定了谁可以读取、写入或执行文件或目录。
mv指令是move的缩写,用来移动或重命名文件、目录,经常用来备份文件或目录。
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 dir
-rw-r--r-- 1 root root 78 Sep 16 20:04 test.c
root@hcss-ecs-8f13:~/112# mv test.c name.c
root@hcss-ecs-8f13:~/112# mv dir mydir
root@hcss-ecs-8f13:~/112# ls -l
total 8
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root 78 Sep 16 20:04 name.c
root@hcss-ecs-8f13:~/112# mv name.c ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 12
drwxr-xr-x 3 root root 4096 Sep 16 20:16 112
-rw-r--r-- 1 root root 78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# mv mydir ../
root@hcss-ecs-8f13:~/112# ls -l ../
total 16
drwxr-xr-x 2 root root 4096 Sep 16 20:16 112
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
-rw-r--r-- 1 root root 78 Sep 16 20:04 name.c
drwx------ 3 root root 4096 Sep 16 11:32 snap
root@hcss-ecs-8f13:~/112# ls -l
total 0
root@hcss-ecs-8f13:~/112#
语法: cat [选项] [文件] 功能: 读取文件内容并将其输出到标准输出设备(通常是终端或屏幕) 常用选项:
root@hcss-ecs-8f13:~/112# pwd
/root/112
root@hcss-ecs-8f13:~/112# touch test.c
root@hcss-ecs-8f13:~/112# ls
test.c
root@hcss-ecs-8f13:~/112# nano test.c
root@hcss-ecs-8f13:~/112# cat test.c
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
root@hcss-ecs-8f13:~/112# cat -b test.c
1 #include <stdio.h>
2 int main()
3 {
4 printf("hello world\n");
5 return 0;
6 }
root@hcss-ecs-8f13:~/112# cat -n test.c
1 #include <stdio.h>
2
3 int main()
4 {
5 printf("hello world\n");
6 return 0;
7 }
root@hcss-ecs-8f13:~/112# cat -s test.c
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
root@hcss-ecs-8f13:~/112#
cat会把文件中的所有内容显示出来,因此cat不适合看大文本,适合看小文本。
如果cat后什么都不跟,则通常情况下我们从键盘输入什么,屏幕上就输出什么。
root@hcss-ecs-8f13:~/112# cat
hello world
hello world
Are you ok?
Are you ok?
tac指令: 功能和cat相反,逆序打印文件内容。
root@hcss-ecs-8f13:~/112# ls
mydir name.c
root@hcss-ecs-8f13:~/112# tac name.c
}
return 0;
printf("hello world\n");
{
int main()
#include <stdio.h>
root@hcss-ecs-8f13:~/112#
功能:
root@hcss-ecs-8f13:~/112# ls -l
total 4
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir
root@hcss-ecs-8f13:~/112# echo "hello world"
hello world
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 20:41 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 20:13 mydir/
-rw-r--r-- 1 root root 12 Sep 16 20:41 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112#
Linux下一切皆文件,Linux下,显示器、键盘、网卡、普通文件等都可以看作文件,只不过显示器只有写方法,向显示器打印,其实就是向显示器文件写入,无法从显示器读取数据。键盘只有读方法。而普通文件具有读写两个功能。 echo指令默认把后面跟的文本写入显示器文件中。cat指令后面如果没有跟任何文件,则默认从键盘文件中读取数据,然后写入到显示器文件中。
上面我们用echo 文本>(输出重定向)文件将文本写入到文件中,如果文件不存在则新建。
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:47 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# echo "hello world">test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "aa" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
aa
root@hcss-ecs-8f13:~/112#
通过测试我们还可以得出,如果指定的文件里面有内容,则会先清空原内容,然后再写入(并不是覆盖)。
>文件名
可以新建空文件>文件名
可以清空文件内容root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root 0 Sep 16 22:58 test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root 12 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# >test.txt
root@hcss-ecs-8f13:~/112# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 16 22:58 ./
drwx------ 7 root root 4096 Sep 16 20:23 ../
drwxr-xr-x 2 root root 4096 Sep 16 21:57 dir/
-rw-r--r-- 1 root root 0 Sep 16 22:59 test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112#
用>
指定文件写入内容会把原内容删除掉,如果我们就不想删除原始内容,将新内容追加到原始内容后,可以用>>
(追加重定向)实现。
root@hcss-ecs-8f13:~/112# cat test.txt
root@hcss-ecs-8f13:~/112# echo "Are you ok?" > test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
root@hcss-ecs-8f13:~/112# echo "Hello" >> test.txt
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112#
除了>
和>>
进行输出,还有输入重定向符号<
。前面我们说了如果cat后面什么都不跟,则默认从键盘上取数据。所以如果我们用输入重定向符号<
指定文件,则会输出文件中的内容。
root@hcss-ecs-8f13:~/112# cat test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112# cat < test.txt
Are you ok?
Hello
root@hcss-ecs-8f13:~/112#
语法: more [选项] [文件] 功能: 分页显示文本文件内容,类似cat 常用选项:
快捷键:
当我们想要查看一个文件的内容,但又不想一次性将整个文件内容全部加载到终端(或屏幕)上时,可以用more代替cat。
但是more指令我们也不太使用,因为我们更方便的指令来代替more。
语法: less [参数] 文件 功能: less是一个功能强大的文本查看器,支持向前和向后滚动文本内容,以及搜索、跳转和复制等更多的功能。它特别适合用于查看大型文件或需要在文件的任意位置浏览内容的情况。 常用选项:
less指令也是按q退出,另外,less支持鼠标滑动来查看文件的上下内容, 相比之下less比more更好用。
语法: head [参数]… 文件… 功能: 显示文本文件的头部内容,默认打印文件内容的前10行 常用选项:
root@hcss-ecs-8f13:~/112# head log.txt
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
hello 10
root@hcss-ecs-8f13:~/112# head -n5 log.txt
hello 1
hello 2
hello 3
hello 4
hello 5
root@hcss-ecs-8f13:~/112#
语法: tail [参数] 文件 功能: 与 head 相反,它默认显示文件的最后10行内容,但同样可以通过命令行选项来定制。不指定文件时,作为输入信息进行处理,常用查看日志文件 常用选项:
root@hcss-ecs-8f13:~/112# tail log.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112# tail -n5 log.txt
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112#
| 管道举例:
管道符号|
:它允许将一个命令的输出直接作为另一个命令的输入。
命令1 | 命令2 | 命令3 ...
问题: 文件log.txt有1000行内容,打印出第500行到510行之间的10行内容。
root@hcss-ecs-8f13:~/112# head -n510 log.txt | tail -n10 log.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
root@hcss-ecs-8f13:~/112#
| date命令
root@hcss-ecs-8f13:~/112# date
Wed Sep 18 11:19:19 PM CST 2024
root@hcss-ecs-8f13:~/112# date +%Y:%m:%d
2024:09:18
root@hcss-ecs-8f13:~/112# date +%Y:%m:%d:%H:%M:%S
2024:09:18:23:24:04
root@hcss-ecs-8f13:~/112# date +%F
2024-09-18
root@hcss-ecs-8f13:~/112# date +%X
11:27:49 PM
root@hcss-ecs-8f13:~/112#
| 时间戳:
时间戳是指从某一特定时刻(通常是1970年1月1日 00:00:00 UTC,即协调世界时)起至现在的总秒数(或毫秒数、微秒数等,具体取决于系统和应用的精度需求)。这种时间表示方式是一种简单而有效的方法,用于在计算机系统和网络中记录时间信息。
root@hcss-ecs-8f13:~/112# date +%s
1726673405
root@hcss-ecs-8f13:~/112# date +%s
1726673436
root@hcss-ecs-8f13:~/112# date +%s
1726673452
root@hcss-ecs-8f13:~/112# date -d@1726673452
Wed Sep 18 11:30:52 PM CST 2024
root@hcss-ecs-8f13:~/112#
语法: find [路径] [选项] [操作] 功能: 在文件树中查找文件
root@hcss-ecs-8f13:~/112# find -name test*
./test.txt
root@hcss-ecs-8f13:~/112# find -type d
.
./dir
root@hcss-ecs-8f13:~/112#
语法: grep [选项]… 模式 [文件]… 功能: 在文件中搜索字符串,将找到的行打印出来 常用选项:
root@hcss-ecs-8f13:~/112# cat test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep hello test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep world test.txt
hello world
root@hcss-ecs-8f13:~/112# grep -i world test.txt
hello world
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep -v world test.txt
hello World
hello WOrld
hello WORld
hello WORLd
hello WORLD
root@hcss-ecs-8f13:~/112# grep -iv world test.txt
root@hcss-ecs-8f13:~/112# grep -vn WORLD test.txt
1:hello world
2:hello World
3:hello WOrld
4:hello WORld
5:hello WORLd
root@hcss-ecs-8f13:~/112#
实践中grep的使用率还是很高的,像查找进程等。
zip和unzip是两个非常常用的命令行工具,分别用于压缩文件和目录(打包成.zip格式)以及解压缩.zip文件。 语法: zip 压缩文件.zip 目录或文件 功能: 将目录或文件压缩成zip格式 常用选项:
root@hcss-ecs-8f13:~/112# ls -l
total 12
drwxr-xr-x 2 root root 4096 Sep 19 22:05 dir
drwxr-xr-x 2 root root 4096 Sep 19 22:09 mydir
-rw-r--r-- 1 root root 72 Sep 19 21:38 test.txt
root@hcss-ecs-8f13:~/112# tree dir
dir
├── test1
├── test2
└── test3
0 directories, 3 files
root@hcss-ecs-8f13:~/112# zip -r dir.zip dir
adding: dir/ (stored 0%)
adding: dir/test2 (stored 0%)
adding: dir/test1 (stored 0%)
adding: dir/test3 (stored 0%)
root@hcss-ecs-8f13:~/112# ls -l;
total 16
drwxr-xr-x 2 root root 4096 Sep 19 22:05 dir
-rw-r--r-- 1 root root 596 Sep 19 22:11 dir.zip
drwxr-xr-x 2 root root 4096 Sep 19 22:09 mydir
-rw-r--r-- 1 root root 72 Sep 19 21:38 test.txt
root@hcss-ecs-8f13:~/112# mv dir.zip ./mydir
root@hcss-ecs-8f13:~/112# cd mydir
root@hcss-ecs-8f13:~/112/mydir# pwd
/root/112/mydir
root@hcss-ecs-8f13:~/112/mydir# ls -l
total 4
-rw-r--r-- 1 root root 596 Sep 19 22:11 dir.zip
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip
Archive: dir.zip
creating: dir/
extracting: dir/test2
extracting: dir/test1
extracting: dir/test3
root@hcss-ecs-8f13:~/112/mydir# tree
.
├── dir
│ ├── test1
│ ├── test2
│ └── test3
└── dir.zip
1 directory, 4 files
root@hcss-ecs-8f13:~/112/mydir#
unzip默认是解压到当前目录,当然我们也可以加上-d选项后解压到指定目录下。
root@hcss-ecs-8f13:~/112# ll
total 20
drwxr-xr-x 5 root root 4096 Sep 19 22:27 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
drwxr-xr-x 2 root root 4096 Sep 19 22:27 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# zip -r dir.zip dir
adding: dir/ (stored 0%)
adding: dir/test2 (stored 0%)
adding: dir/test1 (stored 0%)
adding: dir/test3 (stored 0%)
root@hcss-ecs-8f13:~/112# ll
total 24
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
-rw-r--r-- 1 root root 596 Sep 19 22:28 dir.zip
drwxr-xr-x 2 root root 4096 Sep 19 22:27 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# mv dir.zip ./mydir
root@hcss-ecs-8f13:~/112# ll
total 20
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ./
drwx------ 7 root root 4096 Sep 19 21:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
drwxr-xr-x 2 root root 4096 Sep 19 22:28 mydir/
drwxr-xr-x 2 root root 4096 Sep 19 22:18 otherdir/
root@hcss-ecs-8f13:~/112# cd mydir
root@hcss-ecs-8f13:~/112/mydir# ll
total 12
drwxr-xr-x 2 root root 4096 Sep 19 22:28 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
-rw-r--r-- 1 root root 596 Sep 19 22:28 dir.zip
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip
Archive: dir.zip
creating: dir/
extracting: dir/test2
extracting: dir/test1
extracting: dir/test3
root@hcss-ecs-8f13:~/112/mydir# tree
.
├── dir
│ ├── test1
│ ├── test2
│ └── test3
└── dir.zip
1 directory, 4 files
root@hcss-ecs-8f13:~/112/mydir# unzip dir.zip -d ../otherdir
Archive: dir.zip
creating: ../otherdir/dir/
extracting: ../otherdir/dir/test2
extracting: ../otherdir/dir/test1
extracting: ../otherdir/dir/test3
root@hcss-ecs-8f13:~/112/mydir# ll
total 16
drwxr-xr-x 3 root root 4096 Sep 19 22:29 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
-rw-r--r-- 1 root root 596 Sep 19 22:28 dir.zip
root@hcss-ecs-8f13:~/112/mydir# cd ..
root@hcss-ecs-8f13:~/112# cd otherdir
root@hcss-ecs-8f13:~/112/otherdir# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 19 22:29 ./
drwxr-xr-x 5 root root 4096 Sep 19 22:28 ../
drwxr-xr-x 2 root root 4096 Sep 19 22:26 dir/
root@hcss-ecs-8f13:~/112/otherdir# tree
.
└── dir
├── test1
├── test2
└── test3
1 directory, 3 files
root@hcss-ecs-8f13:~/112/otherdir#
功能: 打包和解包文件 常用选项:
示例:
tar czf dir.tgz dir
:将目录dir打包成dir.zip文件tar cvzf dir.tgz dir
:在打包过程中显示详细过程tar xzf dir.tgz
:将文件dir.zip解压到当前目录tar xzf dir.tgz -C otherdir
:将文件dir.zip解压到指定目录一个任意精度的计算器语言,通常用于执行数学运算。
echo 1+2+3+4+5 | bc
:把计算结果输出到屏幕上语法: uname [选项] 功能: uname用来获取电脑和操作系统的相关信息 常用选项:
root@hcss-ecs-8f13:~/112# uname -a
Linux hcss-ecs-8f13 5.15.0-113-generic #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
root@hcss-ecs-8f13:~/112# uname -r
5.15.0-113-generic
root@hcss-ecs-8f13:~/112# uname -n
hcss-ecs-8f13
root@hcss-ecs-8f13:~/112# uname -s
Linux
root@hcss-ecs-8f13:~/112#
本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~