Linux——基础指令(中)
💁♂️个人主页:进击的荆棘
👇作者其它专栏:
目录
1.Linux背景
2.使用XShell远程登录Linux
3.Linux下基本指令
4.Linux权限的问题
3.Linux下基本指令
3.9mv指令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move(rename)files),经常用来备份文件或目录
语法:mv [选项] 源文件或目录 目标文件或目录
功能:
1.视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中。
2.当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。
3.当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。
常用选项:
●-f:force强制的意思,若目标文件已经存在时,不会询问而直接覆盖
●-i:若目标文件(destination)已经存在时,就会询问是否覆盖
例如:
//更改名称
[root@VM-0-6-centos ~]# touch myfile.txt
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt
[root@VM-0-6-centos ~]# mv myfile.txt yourfile.txt
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:45 yourfile.txt
//若当前路径存在同名文件,改名即覆盖
[root@VM-0-6-centos ~]# touch myfile.txt
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:46 myfile.txt
-rw-r--r-- 1 root root 0 Jul 9 21:45 yourfile.txt
[root@VM-0-6-centos ~]# mv yourfile.txt myfile.txt
mv: overwrite ‘myfile.txt’? y
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt
//若当前路径存在同名文件,改名前可以添加i选项,让系统提出警告供用户做出选择
[root@VM-0-6-centos ~]# touch yourfile.txt
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:45 myfile.txt
-rw-r--r-- 1 root root 0 Jul 9 21:49 yourfile.txt
[root@VM-0-6-centos ~]# mv -i yourfile.txt myfile.txt
mv: overwrite ‘myfile.txt’? y
[root@VM-0-6-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 21:49 myfile.txt
//mv整个文件
[root@VM-0-6-centos ~]# touch myfile.txt
[root@VM-0-6-centos ~]# mkdir temp
[root@VM-0-6-centos ~]# ll
total 4
-rw-r--r-- 1 root root 0 Jul 9 21:55 myfile.txt
drwxr-xr-x 2 root root 4096 Jul 9 21:55 temp
[root@VM-0-6-centos ~]# mv myfile.txt temp
[root@VM-0-6-centos ~]# ll
total 4
drwxr-xr-x 2 root root 4096 Jul 9 21:55 temp
[root@VM-0-6-centos ~]# mv temp ../
[root@VM-0-6-centos ~]# ls
[root@VM-0-6-centos ~]# ls -d ../temp //-d只显示目录本身,不显示目录内容
../temp
3.10cat指令
语法:cat [选项] [文件]
功能:查看目标文件的内容
常用文件:
●-n对输出的所有行编号
例如:
//测试cat基本命令
[root@VM-0-6-centos ~]# echo 'hello linux' > myfile.txt
[root@VM-0-6-centos ~]# cat myfile.txt
hello linux
//cat输出携带行号
[root@VM-0-6-centos ~]# cat -n myfile.txt
1 hello linux
3.11more指令
语法:more [选项]
功能:more命令,功能类似cat
常用选项:
●-n指定输出行数
●q退出more
例如:
//命令行输出多行文本
[root@VM-0-6-centos ~]# nano myfile.txt
[root@VM-0-6-centos ~]# more myfile.txt
hello linux
hello me
hello you
hello hello linux
hello me
hello you
hello linux
hello me
hello you
hello linux
hello me
hello you
hello linux
hello me
hello you
//-n输出指定行数文本
[root@VM-0-6-centos ~]# more -10 myfile.txt
hello linux
hello me
hello you
hello hello linux
hello me
hello you
hello linux
hello me
hello you
hello linux
--More--(68%)
3.12less指令
●less工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能及其强大
●less的用法比起more更加的有弹性,在more的时候,并没有办法向前面翻,只能往后面看
●但若使用了less,就可以使用[pageup][pagedown]等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容
●除此之外,在less里可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜
语法:less [参数] 文件
功能:less与more类似,但使用less可以随意浏览文件,而more只能向前移动,却不能向后移动,而less在查看之前不会加载整个文件
选项:
-N 显示每行的行号
q:quit
例如:
//测试搜索和-N等功能
[root@VM-0-6-centos ~]# less -N myfile.txt
1 hello linux
2 hello me
3 hello you
4 hello hello linux
5 hello me
6 hello you
7 hello linux
8 hello me
9 hello you
10 hello linux
11 hello me
12 hello you
13 hello linux
14 hello me
15 hello you
16
myfile.txt (END)
3.13head指令
head与tail就像它的名字一样,它是用来显示开头或结尾某个数量的文字区块,head用来显示档案的开头到标准输出中,而tail想当然就是看档案的结尾。
语法:head [参数]… [文件]…
功能:head用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行
选项:
-n<行数> 显示的行数
例如:
[root@VM-0-6-centos ~]# head myfile.txt
hello linux
hello me
hello you
hello hello linux
hello me
hello you
hello linux
hello me
hello you
hello linux
[root@VM-0-6-centos ~]# head -5 myfile.txt
hello linux
hello me
hello you
hello hello linux
hello me
3.14tail指令
tail命令从指定点开始将文件写到标准输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新,使你看到最新的文件内容。
语法:tail 必要参数 [文件]
功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。
选项:
●-f循环读取
●-n<行数> 显示行数
例如:(重定向和管道)
[root@VM-0-6-centos ~]# tail myfile.txt
hello linux
hello me
hello you
hello linux
hello me
hello you
hello linux
hello me
hello you
[root@VM-0-6-centos ~]# tail -3 myfile.txt
hello me
hello you
//空行也算一行
//如何显示文件的[5,10]行的内容
[root@VM-0-6-centos ~]# head -10 myfile.txt | tail -5
hello you
hello linux
hello me
hello you
hello linux
3.15date指令
指定格式显示时间:date +%Y:%m:%d
用法:date [OPTION]… [FORMAT]
3.15.1在显示方面,使用者可以设定想要显示的格式,格式设定为一个加号后接数各标记,其中常用的标记列表如下
●%H:小时(00..23)
●%M:分钟(00..59)
●%S:秒(00..59)
●%X:相当于%H:%M:%S
●%d:日(01..31)
●%m:月份(01..12)
●%Y:完整年份(0000..9999)
●%F:相当于%Y-%m-%d
3.15.2在设定时间方面
●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" //这样可以设置全部时间
3.15.3时间戳
●时间->时间戳:date +%s
●时间戳->时间:date -d@ 1508749502
●Unix时间戳(英文为Unix epoch,Unix time,POSIX time或Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰数
//显示常规时间
[root@VM-0-6-centos ~]# date
Sat Jul 11 18:06:28 CST 2026
[root@VM-0-6-centos ~]# date +%Y/%m/%d
2026/07/11
[root@VM-0-6-centos ~]# date +%Y/%m/%d-%H:%M:%S
2026/07/11-18:07:25
//显示时间戳
[root@VM-0-6-centos ~]# date +%s
1783764501
//时间戳转化成可视时间
[root@VM-0-6-centos ~]# date +%Y/%m/%d-%H:%M:%S -d @0
1970/01/01-08:00:00
[root@VM-0-6-centos ~]# date +%Y/%m/%d-%H:%M:%S -d @100000
1970/01/02-11:46:40
[root@VM-0-6-centos ~]# date +%Y/%m/%d-%H:%M:%S -d @1000000000
2001/09/09-09:46:40
3.16cal指令
cal命令可以用来显示公历(阳历)。公历是现在国际通用的历法,又称列历,通称阳历。“阳历”又名“太阳历”,系以地球绕太阳一周为一年,为西方各国所通用,故又称“西历”。
命名格式:cal 参数 [年份]
功能:用于查看日历等时间信息,若只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常用选项:
●-3 显示系统前一个月,当前月,下一个月的月历
●-j 显示在当年中的第几天(一年日期按天算,从1月1日算起,默认显示当前月再一年中的天数)
●-y 显示当前年份的日历
//常规案例
[root@VM-0-6-centos ~]# cal
July 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@VM-0-6-centos ~]# cal 1949
1949
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 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
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 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 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 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 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 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 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
[root@VM-0-6-centos ~]# cal -3
June 2026 July 2026 August 2026
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 1 2 3 4 1
7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8
14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15
21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22
28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29
30 31
3.17find指令
●Linux下find命令在目录结构中搜索文件,并执行指定的操作。
●Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的到选项也很多。
●即使系统中含有网络文件系统(NFS),find命令在该文件系统中同样有效,只要具有相应的权限。
●在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长时间(30G字节以上的文件系统)。
●语法:find pathname -options
●功能:用于在文件树中查找文件,并作出相应处理(可能访问磁盘)
常用选项:
●-name 按照文件名查找文件●其它选项需要再查,此命令较为复杂
//从当前目录下搜索执行名称的文件
[root@VM-0-6-centos ~]# find -name myfile.txt
./myfile.txt
//在指定目录下(~,家目录)搜索执行名称的文件
[root@VM-0-6-centos ~]# find ~ -name myfile.txt
/root/myfile.txt
3.18which指令
功能:搜索系统指定的命令
例如:
[root@VM-0-6-centos ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@VM-0-6-centos ~]# which pwd
/usr/bin/pwd
3.19whereis指令
功能:用于找到程序的源、二进制文件或手册
例如:
[root@VM-0-6-centos ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@VM-0-6-centos ~]# whereis libc.so
libc: /usr/lib64/libc.so /usr/share/man/man7/libc.7.gz
//libc:C标准库
//.so:shared object动态共享库
3.20alias指令
功能:设置命令的别名
例如:
[root@VM-0-6-centos ~]# alias hello='ls -a -l'
[root@VM-0-6-centos ~]# which hello
alias hello='ls -a -l'
/usr/bin/ls
[root@VM-0-6-centos ~]# hello
total 84
dr-xr-x---. 7 root root 4096 Jul 9 22:03 .
dr-xr-xr-x. 19 root root 4096 Jul 11 20:26 ..
-rw------- 1 root root 20131 Jul 11 20:26 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
drwxr-xr-x 4 root root 4096 Mar 22 02:55 .cache
drwxr-xr-x 3 root root 4096 Mar 7 2019 .config
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw------- 1 root root 69 Jul 4 23:17 .lesshst
-rw-r--r-- 1 root root 162 Jul 9 22:09 myfile.txt
-rw-r--r-- 1 root root 44 Mar 20 22:54 .npmrc
drwxr-xr-x 2 root root 4096 Jul 2 21:41 .orca_term
drwxr-xr-x 2 root root 4096 Mar 20 22:54 .pip
-rw-r--r-- 1 root root 73 Mar 20 22:54 .pydistutils.cfg
drwx------ 2 root root 4096 Mar 20 23:34 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
3.21grep指令
语法:grep [选项] 搜索字符串 文件
功能:在文件中搜索字符串,将找到的行打印出来
常用选项:
●-i:忽略大小写的不同,所以大小写视为相同
●-n:顺便输出行号
●-v:反向选择,即显示出没有'搜索字符串'内容的那一行
//文件内容
[root@VM-0-6-centos ~]# nano tmp.txt
[root@VM-0-6-centos ~]# cat tmp.txt
hEllo Linux
hello Linux
heLlo Linux
Hello Linux
hello Linux
hellO Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
//基本查找
[root@VM-0-6-centos ~]# grep 'hello Linux' tmp.txt
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
//忽略大小写的不同,所以大小写视为相同
[root@VM-0-6-centos ~]# grep -i 'hello Linux' tmp.txt
hEllo Linux
hello Linux
heLlo Linux
Hello Linux
hello Linux
hellO Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
//顺便输出行号
[root@VM-0-6-centos ~]# grep -n 'hello Linux' tmp.txt
2:hello Linux
5:hello Linux
7:hello Linux
8:hello Linux
9:hello Linux
10:hello Linux
11:hello Linux
//反向选择,即没有'搜索字符串'内容的那一行
[root@VM-0-6-centos ~]# grep -v 'hello Linux' tmp.txt
hEllo Linux
heLlo Linux
Hello Linux
hellO Linux
[root@VM-0-6-centos ~]# grep -nv 'hello Linux' tmp.txt
1:hEllo Linux
3:heLlo Linux
4:Hello Linux
6:hellO Linux
12:
[root@VM-0-6-centos ~]# grep -nvi 'hello Linux' tmp.txt
12:
3.22top
1 top -d 1 -n 5
-d:刷新的时间间隔
-n:刷新的次数
q:退出
3.23zip/unzip指令
语法:zip dst.zip src(可以是目录或文件)
功能:将目录或文件压缩成zip格式
常用选项:
●-r:递归处理,将指定目录下的所有文件和子目录一并处理
例如:
将test文件压缩:zip test.zip test.txt
将test目录压缩:zip -r test.zip test
解压到root目录:unzip test.zip -d /root
关于sz/rz
这个工具用于Windows机器和远端的Linux机器通过XShell传输文件,安装完毕之后可以通过拖拽的文件将文件上传过去。
安装指令:sudo yum/apt install -y lrzlz
也可以用指令传输文件
将test.zip从Linux传输到Windows:sz test.zip
将test.zip从Windows传输到Linux:rz
3.24tar指令
打包/解包,不打开它,直接看内容
语法:tar [-cxtzjvf] dst.tgz src
常用选项:
●-c:建立一个压缩文件参数指令(即create)
●-x:解开一个压缩文件的参数指令
●-t:查看tarfile里面的文件
●-z:是否同时具有gzip的属性?亦即是否需要用gzip压缩?
●-j:是否同时具有bzip2的属性?亦即是否需要用bzip2压缩?
●-v:压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
●-f:使用档名,在f之后利己接档名!不要再加参数
●-C:解压到指定目录
例如:
示例一:将整个/etc目录下的文件全部打包成'/tmp/etc.tar'
[root@linux ~]$ tar -cvf /tmp/etc.tar /etc <==仅打包,不压缩!
[root@linux ~]$ tar -zcvf /tmp/etc.tar.gz /etc <==打包后,以gzip压缩
[root@linux ~]$ tar -jcvf /tmp/etc.tar.bz2 /etc <==打包后,以bzip2压缩
特别注意,在参数f之后的文件档名是自己取的,习惯上都用.tar来作为辨识。
若加z参数,则以.tar.gz或.tgz来代表gzip压缩过的tar file
若加j参数,则以.tar.bz2来作为附档名
示例二:查阅上述/tmp/etc.tar.gz文件内有哪些文件?
[root@linux ~]$ tar -tzvf /tmp/etc.tar.gz
由于使用gzip压缩,所以要查阅该tar file内的文件时,就要加上z这个参数!
示例三:将tmp/etc.tar.gz文件解压缩在/usr/local/src底下
[root@linux ~]$ cd /usr/local/src
[root@linux ~]$ tar -xzvf /tmp/etc.tar.gz
也可以直接
[root@linux ~]$ tar -xzvf /tmp/etc.tar.gz -C /usr/local/src
在预设情况下,可以将压缩档任何地方解开。另外,若进入/usr/local/src/etc会发现,该目录下的文件属性与/etc可能会有所不同。
示例四:在/tmp下,只想将tmp/etc.tar.gz内的etc/passwd解开
[root@linux ~]$ cd /tmp
[root@linux ~]$ tar -xzvf /tmp/etc.tar.gz etc/passwd
也可以直接
[root@linux ~]$ tar -xzvf /tmp/etc.tar.gz/passwd -C /tmp
可以通过tar -ztvf来查阅tar file内的文件名称,若单只要一个文件,就可以通过这个方式来下达。注意,etc.tar.gz内的根目录/是被拿掉了!
3.25bc指令
bc命令可以很方便的进行浮点运算
3.26uname -r指令
语法:uname [选项]
功能:uname用来获取电脑和操作系统的相关信息
补充说明:uname可显示Linux主机所用的操作系统的版本、硬件的名称等基本信息。
常用选项:
●-a或-all详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
●lsb_release -a:查看操作系统版本
3.27重要的几个热键
●[Tab]按键---具有【命令补全】和【档案补齐】的功能。快速摁两下。
●[Ctrl]+c按键---让当前的程序【停掉】
●[Ctrl]+d按键---通常代表着:【键盘输入结束(End Of File,EOF或End Of Input)】的意思;它也可以用来取代exit。退出当前用户
●[上下键]---查看历史命令
●[Ctrl]+r按键---搜索历史命令
3.28关机
语法:shutdown [选项]
常见选项:
●-h:将系统的服务停掉后,立即关机
●-r:在系统的服务停掉之后就重新启动
●-t sec:-t后加秒数,即【过几秒后关机】的意思
3.29扩展命令
●安装和登录命令: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、deluser、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。
3.30shell命令以及运行原理
Linux严格意义上说的是一个操作系统,我们称之为“核心(kernel)”,但对用户来说,不能直接使用kernel。而是通过kenel的“外壳”程序,即shell,去与kernel沟通。如何理解?为什么不能直接使用kernel?
从技术角度,Shell的最简单定义:命令行解释器(command interpreter)主要包含:
●将使用者的命令翻译给核心(kernel)处理
●同时,将核心的处理结果翻译给使用者
对于windows GUI,操作windows不是直接操作windows内核,而是通过图形接口,点击,从而完成相应的操作(如进入D盘的操作,通常是双击D盘盘符,运行应用程序也可以如此)。
shell对于Linux,有相应的作用,主要是对我们的指令进行解析,解析指令给Linux内核。反馈结果再通过内核运行出结果,通过shell解析给用户。
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐

所有评论(0)