发布时间:2025-12-09 19:17:29 浏览次数:4
该博客以不同分类的形式简绍了Linux系统中常见的指令,并伴随着这些指令的学习简绍了一些Linux系统中相关的知识点。
该篇博客篇幅过长,看的时候可能会带来麻烦,大家如果需要可以看作学习过程一个”字典“,以划分好的模块中去查找对应指令,这也是我最初的想法。如果发现那块出现问题或写的不够完整,欢迎大家批评指正,互相学习。该博客部分内容参考自《Linux就该这样学》。
为什么我们要学习Linux指令:
大家以后做的项目,大部分是部署在Linux服务器中的。Linux系统从出生到洗澡,绝大部分的操作都是命令行,如:维护服务器、进程管理、目录操作、磁盘操作、文件存取等等,都要用指令,这是一个程序员的基本技能,如果不会指令,就不会Linux。
在Linux系统中,目录被组织成一个单根倒置树结构,文件系统目录结构从根目录开始,用" / "表示
根目录所存放的文件我们在这里只需要了解两个一个home目录,一个root目录。
| /root | 超级管理员的家目录,普通用户是无法进入的 |
我们学习Linux的基本指令主要使用普通用户的家目录和超级管理员的家目录。
语法: ls [选项] [目录或文件]
功能: 对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。
常用选项:
高频选项:
ls -l 显示更多文件信息
-l ·相当于list·,意义为:以列表的形式,列出更多的信息
[root@VM-16-5-centos ~]# lscourse test[root@VM-16-5-centos ~]# ls -ltotal 8drwxr-xr-x 4 root root 4096 Nov 29 15:11 coursedrwxr-xr-x 2 root root 4096 Dec 14 02:01 test[root@VM-16-5-centos ~]# ll // ls -l可以简写为lltotal 8drwxr-xr-x 4 root root 4096 Nov 29 15:11 coursedrwxr-xr-x 2 root root 4096 Dec 14 02:01 test文件 = 内容 + 属性
对文件的操作就是对文件内容和属性的操作。
多出来的信息为文件的权限,修改的时间等等。
ls -a显示目录下的隐藏文件
[root@VM-16-5-centos ~]# ls -a. .. .bash_history .bash_logout .bash_profile .bashrc .cache .config course .cshrc .lesshst .pip .pydistutils.cfg .ssh .tcshrc test.和…还有以.开头的文件都是隐藏文件,它和Windows操作系统中的下图选项类似,除非将他选中否则不会显示在用户面前。
其中每行以 - 或 d开头,它们表示的意义如下:
-表示为文件
d表示为目录
既然.和…是隐藏文件那它们分别所存放的是什么内容?
. 表示当前文件
… 表示上一级目录
ls -n:用数字的UID,GID代替名称。
UID:用户ID
GID:用户组ID
这里我们就要了解一下最开始那两个root的意义:
第一个root表示用户,第二个表示用户组
它们显示为字符为root,显示为数字则是它们对应的UID和GID
ls -d:将目录象文件一样显示,而不是显示其它的文件。
[root@VM-16-5-centos ~]# ls -d // 只显示当前目录.[root@VM-16-5-centos ~]# ls -d /root/root[root@VM-16-5-centos ~]# ls /rootcourse test[root@VM-16-5-centos ~]# ls -d ....[root@VM-16-5-centos ~]# ls -d ../home../home语法: pwd
功能: 显示用户当前所在的目录
[root@VM-16-5-centos ~]# pwd/root[root@VM-16-5-centos course]# pwd/root/coursechange directories——改变目录
语法: cd 目录名
功能: 改变工作目录。将当前工作目录改变到指定的目录下。
cd的本质就是切换路径,最开始我们已经简绍了Linux操作系统的目录结构,
而我们平常标识文件位置最好的方法就是使用路径,因为路径具有唯一性。
举例:
[root@VM-16-5-centos ~]# cd .. //返回上级目录,此时为root目录,返回上一级目录为根目录‘/’[root@VM-16-5-centos /]# cd /root/course //绝对路径[root@VM-16-5-centos course]# cd ../test/ //相对路径[root@VM-16-5-centos test]# cd ~ //返回用户家目录[root@VM-16-5-centos ~]# cd - //返回最近访问目录/root/test[root@VM-16-5-centos test]# cd ~YX //进入其他用户的家目录[root@VM-16-5-centos YX]#这个命令应该是Linux中最常用的一个命令。可以通过cd命令迅速、灵活地切换到不同的工作目录。
语法: touch [选项]… 文件…
功能: touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件。
常用选项:
举例:
在当前路径下,创建一个普通文件。
[root@VM-16-5-centos blogs]# pwd/root/blogs[root@VM-16-5-centos blogs]# touch hello.c[root@VM-16-5-centos blogs]# lltotal 0-rw-r--r-- 1 root root 0 Dec 14 16:22 hello.c使用相对路径创建文本
[root@VM-16-5-centos blogs]# pwd/root/blogs[root@VM-16-5-centos blogs]# touch ../hello.c[root@VM-16-5-centos blogs]# cd ..[root@VM-16-5-centos ~]# pwd/root[root@VM-16-5-centos ~]# lltotal 12drwxr-xr-x 2 root root 4096 Dec 14 16:22 blogsdrwxr-xr-x 4 root root 4096 Dec 14 16:20 course-rw-r--r-- 1 root root 0 Dec 14 16:24 hello.cdrwxr-xr-x 2 root root 4096 Dec 14 02:01 test使用绝对路径创建文本
[root@VM-16-5-centos ~]# touch course/hello.c[root@VM-16-5-centos ~]# lltotal 12drwxr-xr-x 2 root root 4096 Dec 14 16:22 blogsdrwxr-xr-x 4 root root 4096 Dec 14 16:33 course-rw-r--r-- 1 root root 0 Dec 14 16:24 hello.cdrwxr-xr-x 2 root root 4096 Dec 14 02:01 test[root@VM-16-5-centos ~]# cd course[root@VM-16-5-centos course]# lltotal 12-rw-r--r-- 1 root root 0 Dec 14 16:33 hello.cdrwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3drwxr-xr-x 3 root root 4096 Dec 8 23:25 lesson4-rw-r--r-- 1 root root 937 Nov 29 14:20 temp.zip语法: tree 目录名 或 直接在想要tree的目录下使用tree
功能: 以树状图的形式,显示当前目录下的所有目录和文件。
举例:
在根目录下使用tree会显示出很长的一个树状图,会出现疯狂刷屏的现象
语法: mkdir [选项] dirname …
功能: 在当前目录下创建一个名为“dirname”的目录
常用选项:
举例:
创建单个目录
[root@VM-16-5-centos blogs]# pwd/root/blogs[root@VM-16-5-centos blogs]# mkdir test1[root@VM-16-5-centos blogs]# lltotal 4-rw-r--r-- 1 root root 0 Dec 14 16:22 hello.cdrwxr-xr-x 2 root root 4096 Dec 15 02:13 test1递归创建多个目录
[root@VM-16-5-centos blogs]# mkdir -p test/d1/d2/d3/d4/d5[root@VM-16-5-centos blogs]# lltotal 8-rw-r--r-- 1 root root 0 Dec 14 16:22 hello.cdrwxr-xr-x 3 root root 4096 Dec 15 02:14 testdrwxr-xr-x 2 root root 4096 Dec 15 02:13 test1[root@VM-16-5-centos blogs]# tree testtest`-- d1`-- d2`-- d3`-- d4`-- d55 directories, 0 filesrmdir是一个与mkdir相对应的命令,mkdir是建立目录,而rmdir是删除目录,但它比较单一,只能删除空目录。
语法: rmdir [-p] [dirName]
适应对象: 具有当前目录操作权限的所有使用者。
功能: 删除空目录
常用选项:
举例:
删除空目录
[root@VM-16-5-centos blogs]# cd test/d1/d2/d3/d4/[root@VM-16-5-centos d4]# lltotal 4drwxr-xr-x 2 root root 4096 Dec 15 02:24 d5[root@VM-16-5-centos d4]# lltotal 4drwxr-xr-x 2 root root 4096 Dec 15 02:24 d5[root@VM-16-5-centos d4]# rmdir d5[root@VM-16-5-centos d4]# lltotal 0rmdir -p 删除目录
[root@VM-16-5-centos blogs]# tree.|-- hello.c|-- test| `-- d1| `-- d2| `-- d3| `-- d4`-- test16 directories, 1 file[root@VM-16-5-centos blogs]# rmdir -p test/d1/d2/d3/d4/[root@VM-16-5-centos blogs]# tree.|-- hello.c`-- test11 directory, 1 filerm指令可以同时删除文件和目录,并且在使用特殊选项的时候可以无视目录是否为空,rm是我们经常使用的删除指令
语法: rm [-f -i -r] [dirName/dir]
适应对象: 所有使用者
功能: 删除文件或目录
常用选项:
注意:
举例:
rm -r 文件名: 递归删除,目录(包括非空目录)
rm -rf:强制删除,不会出现提示,询问是否删除
[root@VM-16-5-centos blogs]# tree.|-- d1| `-- d2| `-- d3| `-- d4| `-- d5|-- test1`-- test.c6 directories, 1 file[root@VM-16-5-centos blogs]# rm -rf d1[root@VM-16-5-centos blogs]# rm -rf test.c[root@VM-16-5-centos blogs]# tree.`-- test11 directory, 0 files这里要注意一点在,删除文件时,哪怕用户对这个文件没有没有删除的权力,依然可以使用rm -rf命令删除文件
这显然是不合理的,解决这个问题的方法为粘滞位,在之后的其他该系列博客中会,大家要是感兴趣也可以去搜一下
语法: cp [选项] 源文件或目录 目标文件或目录
功能: cp —— copy,复制文件或目录
说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已存在的目录,则它会把前面指定的所有文件或目录复制到此目录中,若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息。
常见选项:
举例:
cp 文件名 路径:不使用参数,只能拷贝文件,无法拷贝目录
//将test.c文件拷贝到test1目录中[root@VM-16-5-centos blogs]# lltotal 4drwxr-xr-x 2 root root 4096 Dec 15 15:37 test1-rw-r--r-- 1 root root 0 Dec 15 15:37 test.c[root@VM-16-5-centos blogs]# tree.|-- test1`-- test.c1 directory, 1 file[root@VM-16-5-centos blogs]# cp test.c testtest1/ test.c [root@VM-16-5-centos blogs]# cp test.c test1/[root@VM-16-5-centos blogs]# tree.|-- test1| `-- test.c`-- test.c1 directory, 2 filescp -r 目录名 路径:r为递归参数,递归拷贝文件,即拷贝目录
[root@VM-16-5-centos blogs]# tree.|-- test1| `-- test.c|-- test2`-- test.c2 directories, 2 files[root@VM-16-5-centos blogs]# cp -r test2 test1[root@VM-16-5-centos blogs]# tree.|-- test1| |-- test2| `-- test.c|-- test2`-- test.c3 directories, 2 filescp -f 文件名 路径:强制拷贝,一般,目录下有同名文件时会给出是否覆盖的确认消息;-f为暴力,不给提示,直接覆盖
但实际操作却有一些偏差
[root@VM-16-5-centos blogs]# tree.|-- test1| |-- test2| |-- test3| `-- test.c|-- test2|-- test3`-- test.c5 directories, 2 files[root@VM-16-5-centos blogs]# cp -f test.c test1cp: overwrite ‘test1/test.c’? y //还是给出了确认消息这是因为cp本身的问题,这里我们可以使用 alias查看它的别名:
[root@VM-16-5-centos blogs]# alias cpalias cp='cp -i'这里我们可以看到,cp在使用中的本质就是cp -i,所以当我们使用-f时也就是使用-f -i,而cp -i在上面我们已经见过,在覆盖文件之前先询问用户,无论是否加-f都将出现提示消息。
这里我们使用unalias指令来取消别名,来解决这个问题:
[root@VM-16-5-centos blogs]# unalias cp取消别名后:
[root@VM-16-5-centos blogs]# tree.|-- test1| |-- test2| |-- test3| `-- test.c|-- test2|-- test3`-- test.c5 directories, 2 files[root@VM-16-5-centos blogs]# cp -f test.c test1[root@VM-16-5-centos blogs]#回复别名:
[root@VM-16-5-centos blogs]# alias cp='cp -i'[root@VM-16-5-centos blogs]# alias cpalias cp='cp -i'mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename)files),是Linux系统下常用的命令,经常用来备份文件或者目录。
语法: mv [选项] 源文件或目录 目标文件或目录
功能:
剪切:
mv 文件或目录 目录
重命名:
mv
常用选项:
举例:
重命名
mv 文件或目录 不从在文件/目录名:重命名
[root@VM-16-5-centos blogs]# lltotal 12drwxr-xr-x 4 root root 4096 Dec 15 15:55 test1drwxr-xr-x 2 root root 4096 Dec 15 15:43 test2drwxr-xr-x 2 root root 4096 Dec 15 15:55 test3-rw-r--r-- 1 root root 0 Dec 15 15:37 test.c[root@VM-16-5-centos blogs]# mv test1 test[root@VM-16-5-centos blogs]# lltotal 12drwxr-xr-x 4 root root 4096 Dec 15 15:55 testdrwxr-xr-x 2 root root 4096 Dec 15 15:43 test2drwxr-xr-x 2 root root 4096 Dec 15 15:55 test3-rw-r--r-- 1 root root 0 Dec 15 15:37 test.c剪切
mv 文件或目录 其他目录 :剪切到指定路径
[root@VM-16-5-centos blogs]# mkdir test1[root@VM-16-5-centos blogs]# tree.|-- test| `-- test.c|-- test1`-- test.c2 directories, 2 files[root@VM-16-5-centos blogs]# mv test1 test[root@VM-16-5-centos blogs]# tree.|-- test| |-- test1| `-- test.c`-- test.c2 directories, 2 files剪切+重命名
mv 文件或目录 不从在文件/目录名+其他目录:剪切+重命名
[root@VM-16-5-centos blogs]# tree.|-- test| |-- test1| `-- test.c`-- test.c2 directories, 2 files[root@VM-16-5-centos blogs]# mv test.c test/test1.c[root@VM-16-5-centos blogs]# tree.`-- test|-- test1|-- test1.c`-- test.c2 directories, 2 files这一类的知识比较杂,需要配合着重定向等知识来讲解,我将这一块的知识整理到了另一个博客中,大家要是对这块知识了解的不够多,建议结合输入输出重定向一起学习。
语法: echo 字符串 或 echo 字符串 > 文件名
功能: 向显示器打印—写入到显示文件 或 配合重定向向文件打印
举例:
向显示器打印
[root@VM-16-5-centos blogs]# echo "hello world"hello world[root@VM-16-5-centos blogs]# echo hello worldhello world向文件打印
[root@VM-16-5-centos blogs]# echo hello > test.c[root@VM-16-5-centos blogs]# cat test.c // 查看文件内数据hello使用输出重定向,向文件test.c中输出数据。
语法: nano 文件名
功能: 在Linux上使用文件编写代码
nano指令需要通过以下命令安装后方可使用
yum install -y nano举例:
nano 文件名:对文件进行编辑
注意:编写程序时右边的小键盘不能使用。
编写好后,使用ctrl+x退出
询问是否保存代码,输入“y”(保存),“n”(不保存),“c”(取消),按回车退出
编译刚刚写好的代码,并输出:
[root@VM-16-5-centos blogs]# gcc test1.c //使用gcc编译器对test.c进行编译[root@VM-16-5-centos blogs]# lltotal 16-rwxr-xr-x 1 root root 8360 Dec 17 01:17 a.out //a.out为test1.c生产的可执行文件-rw-r--r-- 1 root root 76 Dec 17 01:17 test1.c[root@VM-16-5-centos blogs]# ./a.out //执行当前路径下的a.out程序hello world!语法: cat [选项] [文件]
功能: 查看目标文件的内容(内容较少的)
常见选项:
了解:
Linux系统中有多个用于查看文本内容的命令,每个命令都有自己的特点,比如 cat命令就适合用于查看内容较少的纯文本文件的。cat这个命令也很好记,在英语中cat时“猫”的意思,猫是不是给你一种较小、可爱的感觉呢?
举例:
我们先nano一个文件向其加入数据,在使用cat查看
[root@VM-16-5-centos blogs]# nano test.c[root@VM-16-5-centos blogs]# cat test.caaaaaaabbbbbbbccccccccddddddddeeeeeeeeeeaaaaaaaccccccccaaaaaaaaaaaaarrrrrrcat -s 文件名:省略多余空行,输出内容时,如果两个字符串之间隔着多个空行,只输出一个空行
[root@VM-16-5-centos blogs]# cat -s test.caaaaaaabbbbbbbccccccccddddddddeeeeeeeeeeaaaaaaaccccccccaaaaaaaaaaaaarrrrrrcat -sn 文件名:-n为对输出行进行编号, -sn为省略空行打印行数
[root@VM-16-5-centos blogs]# cat -sn test.c1aaaaaaa2bbbbbbb34cccccccc5dddddddd67eeeeeeeeee8aaaaaaa9cccccccc10aaaaaaaaaaaaa11rrrrrr12只输入cat,后面不加文件,默认从键盘中读取数据
cat < 文件名:输入重定向,将文件内容输入到cat,并打印
[root@VM-16-5-centos blogs]# cat < test.c //通过输入重定向读取数据aaaaaaabbbbbbbccccccccddddddddeeeeeeeeeeaaaaaaaccccccccaaaaaaaaaaaaarrrrrr[root@VM-16-5-centos blogs]# cat test.c //直接读取数据aaaaaaabbbbbbbccccccccddddddddeeeeeeeeeeaaaaaaaccccccccaaaaaaaaaaaaarrrrrr由上述两个例子可知,输入重定向就是改变从键盘上读取数据的方式,改为从其他文件中读取数据。
语法: more [选项] [文件]
功能: more命令,功能类似cat,用于查看纯文本文件(内容较多的)
常用选项:
在使用more之前,我们先写一个命令行脚本:
[root@VM-16-5-centos blogs]# touch test.txt[root@VM-16-5-centos blogs]# cnt=0; while [ $cnt -le 1000 ]; do echo "hello $cnt"; let cnt++; done > mylog.txt向test.txt文件中写入 hello 1 ~ hello 1000
使用cat查看一下该文件:
more 文件名:从头部查看文本内容,按回车逐行阅读
语法: less [参数] 文件
功能:
选项:
举例:
less 文件名:查看文件内容支持上下翻阅
↑ 向上翻阅,↓ 向下翻阅,q 退出
使用 / 和 ? 在文件中查找,并使用 n 重复上一个搜索
小结论:
head与tail就像它的名字一样的浅显易懂,它是用来显示开头和结尾某个数量的文字区块,head用来显示档案的开头至标准输出中,而tail就是看档案的结尾。
语法: head [参数]… [文档]…
功能: head用来显示文档的开头至标准输出中,默认head命令命令打其相应文件的开头10行
选项:
举例:
head -5 test.txt:查看文件中前5行的内容
[root@VM-16-5-centos blogs]# head -5 test.txthello 0hello 1hello 2hello 3hello 4[root@VM-16-5-centos blogs]#tail命令从指定点开始将文件写到标准输出,使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容。
语法: tail [必要参数] [选择参数] [文件]
功能: 用于显示指点文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件,默认查看后10行的内容
选项:
举例:
tail -5 test.txt:查看文件中后5行的内容
[root@VM-16-5-centos blogs]# tail -10 test.txthello 991hello 992hello 993hello 994hello 995hello 996hello 997hello 998hello 999hello 1000我们既然学习了head和tail指令,知道一个文件的头和尾如何去查找,那我们如何直接查找一个文件中间的行数。这里有两个方法:
方法1:管道符
这里就需要用到管道符命令 |,管道命令符的作用可以用一句话来概括“把前一个命令原本要输出到屏幕的数据当作时最后一个命令的标准输入”,就是当第一个管道符前的命令执行完之后,不输出,将结果用于第一个管道符之后的指令。
head -500 test.txt | tail -10 :查看第490行到500行的内容
[root@VM-16-5-centos blogs]# head -500 test.txt | tail -10 hello 490hello 491hello 492hello 493hello 494hello 495hello 496hello 497hello 498hello 499方法2:输出重定向 + 中间文件
如果对输出重定向不熟悉,可以查看这篇博客:输入输出重定向
查看第490行到500行的内容
[root@VM-16-5-centos blogs]# head -500 test.txt > mylog.txt [root@VM-16-5-centos blogs]# tail -10 mylog.txt hello 490hello 491hello 492hello 493hello 494hello 495hello 496hello 497hello 498hello 499语法: wc [-lwm]
功能: 统计文件的行数、单词数、字节数
选项:
扩展:
每次看到这个命令,我总联系到一种公共设施,即使两者毫无关系。如果为了方便自己记住这个命令的作用,也可以联想到上厕所时好无聊,无聊到数完了手中杂志上有多少行字。
举例:
wc 文件名 :显示文本文件的行数、单词数和字节数
[root@VM-16-5-centos blogs]# cat test.ca saddsad[root@VM-16-5-centos blogs]# wc test.c2 3 11 test.c在Linux系统中,passwd是用来保存系统账户信息的文件,要统计当前系统中有多少个用户,可以使用下面的命令来进行查询。
wc -l /etc/passwd :显示文本文件的行数
[root@VM-16-5-centos blogs]# wc -l /etc/passwd28 /etc/passwd语法: stat 文件名
功能: 获取文件的所有属性,如具体存储信息和时间等信息。
举例:
stat 文件名 :获取文件信息
[root@VM-16-5-centos blogs]# stat test.cFile: ‘test.c’Size: 11 Blocks: 8 IO Block: 4096 regular fileDevice: fd01h/64769dInode: 786515 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2022-12-19 02:34:08.786214892 +0800Modify: 2022-12-19 02:34:07.945218407 +0800Change: 2022-12-19 02:34:07.945218407 +0800Birth: -语法: sort [选项] 文件名
功能: 对文件中的数据按ASCII码表排序,先比较第一个字符,第一个相等比较第二个,依此类推。
选项:
举例:
sort 文件名 :对文件进行排序
[root@VM-16-5-centos blogs]# cat test.c1111222233366667774444443666678888[root@VM-16-5-centos blogs]# sort test.c1111222233336666444444666677778888语法: uniq 文件名
功能: 相邻两行数据重复时,进行去除操作
举例:
uniq 文件名 :对文件进行去重操作
[root@VM-16-5-centos blogs]# uniq test.c1111222233366667774444443666678888[root@VM-16-5-centos blogs]# sort test.c | uniq1111222233336666444444666677778888date指定格式显示时间语法:date + %Y:%m:%d
date语法:date [OPTION]… [+FORMAT]
1.在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中常用的标记列表如下
2.在设定时间方面
3.时间戳
时间->时间戳: date +%s
时间戳->时间: date -d@1508749502
Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒
举例:
date + :显示时间
[root@VM-16-5-centos lesson3]# dateMon Nov 28 23:20:38 CST 2022[root@VM-16-5-centos lesson3]# date +%Y2022[root@VM-16-5-centos lesson3]# date +%Y-%m2022-11[root@VM-16-5-centos lesson3]# date +%Y-%m-%d2022-11-28[root@VM-16-5-centos lesson3]# date +%Y-%m:%d2022-11:28[root@VM-16-5-centos lesson3]# date +%Y-%m/%d2022-11/28[root@VM-16-5-centos lesson3]# date +%Y-%m-%d/%H:%M:%S2022-11-28/23:25:16date +%s :显示时间戳
[root@VM-16-5-centos lesson3]# date +%s1669649722[root@VM-16-5-centos lesson3]# date +%s1669649726[root@VM-16-5-centos lesson3]# date +%s1669649727[root@VM-16-5-centos lesson3]# date +%s1669649729[root@VM-16-5-centos lesson3]# date +%s1669649730[root@VM-16-5-centos lesson3]# date +%s1669649731date -d :时间戳转换
[root@VM-16-5-centos lesson3]# date -d @1508749502Mon Oct 23 17:05:02 CST 2017[root@VM-16-5-centos lesson3]# date +%Y-%m-%d/%H:%M:%S -d @15087495022017-10-23/17:05:02cal命令可以用来显示公历(阳历)日历。公历是现在国际通用的历法,又称格列历,通称阳历。“阳历”又称“太阳历”,系已地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”。
语法: cal [参数] [月份] [年份]
功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常用选项:
举例:
cal :查看当月日历
[root@VM-16-5-centos blogs]# calDecember 2022 Su Mo Tu We Th Fr Sa1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31cal 2000 :查看2000年全年的日历
[root@VM-16-5-centos blogs]# cal 20002000 January February March 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 3 42 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 119 10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 1816 17 18 19 20 21 22 20 21 22 23 24 25 26 19 20 21 22 23 24 2523 24 25 26 27 28 29 27 28 29 26 27 28 29 30 3130 31April May June 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 6 1 2 32 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 109 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 1716 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 2423 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 3030July August September 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 22 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 99 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 1616 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 2323 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 3030 31October 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 5 6 7 1 2 3 4 1 28 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 915 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 1622 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 2329 30 31 26 27 28 29 30 24 25 26 27 28 29 3031C语言在时间戳上的使用
[root@VM-16-5-centos lesson3]# cat time.c #include<stdio.h>int main(){printf("time:%u\n",(unsigned int)time(NULL));return 0;}[root@VM-16-5-centos lesson3]# gcc time.c //使time.c变为可执行文件[root@VM-16-5-centos lesson3]# ./a.out //a.out为生产的可执行文件,./a.out为执行当前目录下的a.out文件time:1669652597[root@VM-16-5-centos lesson3]# ./a.out time:1669652754[root@VM-16-5-centos lesson3]# ./a.out time:1669652755[root@VM-16-5-centos lesson3]# ./a.out time:1669652756[root@VM-16-5-centos lesson3]# ./a.out time:1669652757[root@VM-16-5-centos lesson3]# ./a.out time:1669652757Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是man
语法: man [选项] 命令
功能: man 指令可以查阅 Linux 的联机手册
常见选项:
常用按键及用途:
| 空格键 | 向下翻一页 |
| ↑ | 向下翻一页 |
| ↓ | 向上翻一页 |
| home | 直接前往首页 |
| end | 直接前往尾页 |
| / | 从上至下搜索某个关键词,如“/linux” |
| ? | 从下至上搜索某个关键词,如“?linux” |
| n | 定位到下一个搜索到的关键词 |
| N | 定位到上一个搜索到的关键词 |
| q / ctrl+z | 退出帮助文档 |
安装man
一般我们要使用man指令,需要先在root用户下通过下面命令安装:
yum intall -y man-pages查找手册:
我们先使用man来查看man本身,看看man指令是如何使用的:
查找手册介绍:
| 1☆ | Executable programs or shell commands | 普通的命令 |
| 2☆ | System calls (functions provided by the kernel) | 系统调用 |
| 3☆ | Library calls (functions within program libraries) | (C语言)库函数 |
| 4 | Special files (usually found in /dev) | 特殊文件 |
| 5 | File formats and conventions, e.g. /etc/passwd | 文件格式 |
| 6 | Games | 游戏 |
| 7☆ | Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) | 附件和变量 |
| 8 | System administration commands (usually only for root) | 系统管理命令(通常为root使用) |
| 9 | Kernel routines [Non standard] | 内核例程(非标准) |
注意:☆为常用手册
信息结构及意义:
一般来讲,使用man命令查看到的帮助内容信息都会很长很多,如果读者不了解帮助文档信息的目录结构和操作方法,咋一看到这么多的信息可能会感到相当困惑。
man alias:产看alias指令。(随便查看一个指令,引出信息结构)
man printf:查找printf函数
如图所示,这很明显是一个指令,而非我们要找的函数。
这是因为Linux下也有printf指令,在1号手册中。
在Linux下调用printf指令
[YX@VM-16-5-centos ~]$ printf "%d:%s\n" 1 "hello world"1:hello worldman 3 printf:在三号手册中查找printf函数
语法: find pathname -options
功能: 用于在文件树中查找文件,并作出相应的处理(可能访问磁盘)
常用选项:
举例:
在Linux下使用find查找文件
[root@VM-16-5-centos course]# lltotal 12-rw-r--r-- 1 root root 0 Dec 14 16:33 hello.cdrwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3drwxr-xr-x 3 root root 4096 Dec 8 23:25 lesson4-rw-r--r-- 1 root root 937 Nov 29 14:20 temp.zip[root@VM-16-5-centos course]# find lesson3 //lesson3目录在该目录下,在该目录下使用find无须在使用选项,所得内容很详细lesson3lesson3/time.clesson3/mylog.txtlesson3/test.txtlesson3/my.txtlesson3/file2.txtlesson3/tmp.txtlesson3/dir1lesson3/dir1/file.txt[root@VM-16-5-centos course]# cd ~[root@VM-16-5-centos ~]# find lesson3 //再root的家目录下查找,不使用选项,无法找到find: ‘lesson3’: No such file or directory[root@VM-16-5-centos ~]# find -name lesson3 //使用选项可以找到所找目录的路径./course/lesson3功能: 搜索特定指令的位置
举例:
[root@VM-16-5-centos lesson3]# which pwd/usr/bin/pwd[root@VM-16-5-centos lesson3]# which llalias ll='ls -l --color=auto'/usr/bin/ls[root@VM-16-5-centos lesson3]# which touch/usr/bin/touch功能: 搜索在系统默认路径下搜素指定名称的文件、程序或者指定的归档文件(压缩包)
举例:
[root@VM-16-5-centos lesson3]# whereis lsls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz[root@VM-16-5-centos lesson3]# whereis mymy: /etc/my.cnf语法: grep [选项] 搜索字符串 文件
功能: 在文件中搜素字符串,将找到的行打印出来
常用选项:
举例:
grep 搜索字符串 文本:搜索该字符串,并打印
[root@VM-16-5-centos ~]# cat my.txtdasddqbbbbbccccccccdddddddddDDDDDDDDDCCCCCCCAAAAAABBBBBBBBaaa[root@VM-16-5-centos ~]# grep 'aaa' my.txtaaagrep -v 搜索字符串 文本:打印除该字符串外的其他行
[root@VM-16-5-centos ~]# grep -v 'aaa' my.txt dasddqbbbbbccccccccdddddddddDDDDDDDDDCCCCCCCAAAAAABBBBBBBBgrep -i 搜索字符串 文本:无视大小写搜索并打印该字符串
[root@VM-16-5-centos ~]# grep -i 'aaa' my.txtAAAAAAaaa语法: zip 压缩文件.zip 目录或文件
功能: 将目录或文件压缩成zip格式
常用选项:
举例:
打包—目录名
[root@VM-16-5-centos bit]# zip lesson3.zip lesson3adding: lesson3/ (stored 0%)[root@VM-16-5-centos bit]# lltotal 12drwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3-rw-r--r-- 1 root root 166 Nov 29 13:02 lesson3.zipdrwxr-xr-x 2 root root 4096 Nov 22 19:09 lesson4解包—目录名
[root@VM-16-5-centos bit]# mv lesson3.zip .. //将压缩目录移至上一级目录[root@VM-16-5-centos bit]# lltotal 8drwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3drwxr-xr-x 2 root root 4096 Nov 22 19:09 lesson4[root@VM-16-5-centos bit]# cd ..[root@VM-16-5-centos ~]# lltotal 8drwxr-xr-x 4 root root 4096 Nov 29 13:05 bit-rw-r--r-- 1 root root 166 Nov 29 13:02 lesson3.zip[root@VM-16-5-centos ~]# unzip lesson3.zip //减压压缩文件Archive: lesson3.zipcreating: lesson3/[root@VM-16-5-centos ~]# lltotal 12drwxr-xr-x 4 root root 4096 Nov 29 13:05 bitdrwxr-xr-x 2 root root 4096 Nov 29 00:29 lesson3-rw-r--r-- 1 root root 166 Nov 29 13:02 lesson3.zip[root@VM-16-5-centos ~]# tree lesson3 //查看减压后,目录内的内容lesson3 //lesson3中什么都没有,那是因为我们打包压缩的只是它的目录,而不包含其中内容0 directories, 0 files减压完整压缩文件
[root@VM-16-5-centos bit]# mv lesson3.zip .. //移动压缩文件到上一级目录[root@VM-16-5-centos bit]# lltotal 8drwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3drwxr-xr-x 2 root root 4096 Nov 22 19:09 lesson4[root@VM-16-5-centos bit]# cd ..[root@VM-16-5-centos ~]# lltotal 12drwxr-xr-x 4 root root 4096 Nov 29 13:13 bit-rw-r--r-- 1 root root 4571 Nov 29 13:13 lesson3.zip[root@VM-16-5-centos ~]# unzip lesson3.zip //减压压缩文件Archive: lesson3.zipcreating: lesson3/inflating: lesson3/time.c inflating: lesson3/mylog.txt inflating: lesson3/test.txt inflating: lesson3/my.txt extracting: lesson3/file2.txt inflating: lesson3/tmp.txt creating: lesson3/dir1/extracting: lesson3/dir1/file.txt [root@VM-16-5-centos ~]# lltotal 16drwxr-xr-x 4 root root 4096 Nov 29 13:13 bitdrwxr-xr-x 3 root root 4096 Nov 29 00:29 lesson3-rw-r--r-- 1 root root 4571 Nov 29 13:13 lesson3.zip[root@VM-16-5-centos ~]# tree lesson3lesson3|-- dir1| `-- file.txt|-- file2.txt|-- mylog.txt|-- my.txt|-- test.txt|-- time.c`-- tmp.txt1 directory, 7 files减压到指定文件
[root@VM-16-5-centos bit]# unzip temp.zip -d lesson4Archive: temp.zipcreating: lesson4/temp/extracting: lesson4/temp/test.cnp extracting: lesson4/temp/test.c extracting: lesson4/temp/test.txt extracting: lesson4/temp/test.py extracting: lesson4/temp/test.sh功能: 打包/解包,不打开它,直接看内容
语法: tar [-cxtzjvf] 打包压缩后的包名 需要打包压缩的文件
常用选项:
常用选项讲解:
一些必须要说的废话,建议配合下面的案例学习:
-c参数用于创建压缩文件,-x参数用于减压文件,因此这两个参数不能同时使用。
-z参数指定使用gzip格式来压缩或减压文件,-j参数指定使用bzip2格式来压缩或减压文件。用户使用时根据文件的后缀来决定应使用何种参数继续减压。
一般后缀分为三种:
.tar:只使用tar不用其他格式,此时的文件只是被打包,没有被压缩。
.tgz/.tar.gz:t—tar,gz—gaip,表示使用Gzip格式压缩的文件。
.tbz/.tar.bz:t—tar,bz—bzip2,表示使用bzip2格式压缩的文件。
在执行某些压缩或减压操作时,可能需要花费数个小时,如果屏幕一直没有输出,一方面不好判断打包的进度情况,另一方面也会怀疑电脑死机了,因此推荐使用 -v 参数向用户不断显示压缩或减压过程。
-f参数特别重要,它必须放到参数的最后一位,代表要压缩或减压的软件包名称。
习惯上
压缩格式:tar -czvf 压缩包名称.tgz 要打包的目录
减压格式:tar -xzvf 压缩包名称.tgz
案例:
tar -czvf 压缩文件名 文件名:将文件压缩打包
[root@VM-16-5-centos lesson4]# tar -czvf temp.tgz temptemp/temp/test.cnptemp/test.ctemp/test.txttemp/test.pytemp/my.txttemp/test.sh[root@VM-16-5-centos lesson4]# lltotal 12drwxr-xr-x 2 root root 4096 Dec 19 18:25 temp-rw-r--r-- 1 root root 4763 Dec 19 20:28 temp.tgztar -xzvf 压缩文件名:将文件减压
[root@VM-16-5-centos lesson4]# rm -rf temp //删除temp文件,防止减压后文件覆盖,不好观察[root@VM-16-5-centos lesson4]# tar -xzvf temp.tgztemp/temp/test.cnptemp/test.ctemp/test.txttemp/test.pytemp/my.txttemp/test.sh[root@VM-16-5-centos lesson4]# tree temptemp|-- my.txt|-- test.c|-- test.cnp|-- test.py|-- test.sh`-- test.txt0 directories, 6 filestar -ztvf 压缩文件名:查看压缩文件内有那些文件
[root@VM-16-5-centos lesson4]# tar -ztvf temp.tgzdrwxr-xr-x root/root 0 2022-12-19 18:25 temp/-rw-r--r-- root/root 0 2022-11-29 14:17 temp/test.cnp-rw-r--r-- root/root 0 2022-11-29 14:17 temp/test.c-rw-r--r-- root/root 15 2022-11-29 14:18 temp/test.txt-rw-r--r-- root/root 0 2022-11-29 14:17 temp/test.py-rw-r--r-- root/root 19378 2022-11-29 16:37 temp/my.txt-rw-r--r-- root/root 0 2022-11-29 14:17 temp/test.shtar -xzf 压缩文件 -C 路径:将压缩文件减压到指定路径
[root@VM-16-5-centos lesson4]# tar -xzf temp.tgz -C ..[root@VM-16-5-centos lesson4]# lltotal 12drwxr-xr-x 2 root root 4096 Dec 19 18:25 temp-rw-r--r-- 1 root root 4763 Dec 19 18:27 temp.tgz[root@VM-16-5-centos lesson4]# cd ..[root@VM-16-5-centos course]# lltotal 12-rw-r--r-- 1 root root 0 Dec 14 16:33 hello.cdrwxr-xr-x 4 root root 4096 Dec 19 20:05 lesson3drwxr-xr-x 3 root root 4096 Dec 19 18:28 lesson4drwxr-xr-x 2 root root 4096 Dec 19 18:25 temp //减压到指定路径[root@VM-16-5-centos course]# tree temptemp|-- my.txt|-- test.c|-- test.cnp|-- test.py|-- test.sh`-- test.txt0 directories, 6 files语法: uname [选项]
功能: uname 用来获取电脑和操作系统的相关消息
补充说明: uname可显示linux主机所用的操作系统的版本号、硬件的名称等基本信息。
常用选项:
举例:
[root@VM-16-5-centos ~]# unameLinux[root@VM-16-5-centos ~]# uname -r3.10.0-1160.71.1.el7.x86_64[root@VM-16-5-centos ~]# uname -aLinux VM-16-5-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/Linuxcat /etc/redhat-release:查找当前系统版本的详细信息
如果要查看当前系统版本的详细信息,则需要查看redhat-release文件,其命令以及相应的结果如下:
[root@VM-16-5-centos ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core)语法: whoami
功能: 查看当前用户
举例:
[root@VM-16-5-centos ~]# whoamiroot [YX@VM-16-5-centos ~]$ whoamiYX语法: top
功能: 用于动态地监视进程活动与系统负载等信息。(完全可以看作Linux中的“强化版的Windows任务管理器”)
top指令执行结果的前5行为系统整体的统计信息,其所代表的含义如下:
语法: alias 别名=‘ 指令 ’
功能: 为指令其别名,当一条指令过于复杂时,就可以给它起个别名。
举例:
alias 别名 = ‘ls -l -a -i -n’:为指令起别名
[YX@VM-16-5-centos ~]$ alias lsl='ls -l -a -i -n'[YX@VM-16-5-centos ~]$ lsltotal 44656507 drwx------ 5 1002 1002 4096 Dec 15 03:43 .393721 drwxr-xr-x. 5 0 0 4096 Nov 11 19:59 ..656529 -rw------- 1 1002 1002 10037 Dec 20 00:17 .bash_history656515 -rw-r--r-- 1 1002 1002 18 Apr 1 2020 .bash_logout656517 -rw-r--r-- 1 1002 1002 193 Apr 1 2020 .bash_profile656513 -rw-r--r-- 1 1002 1002 231 Apr 1 2020 .bashrc656511 drwxrwxr-x 5 1002 1002 4096 Dec 9 17:12 bit656519 drwxrwxr-x 3 1002 1002 4096 Nov 11 20:08 .cache656525 drwxrwxr-x 3 1002 1002 4096 Nov 11 20:08 .configunalias 别名:取消别名
[YX@VM-16-5-centos ~]$ unalias lsl[YX@VM-16-5-centos ~]$ lsl-bash: lsl: command not found语法: clear
功能: 清屏
语法: history
功能: 显示历史上所有的指令
语法: bc
功能: bc命令可以方便的进行浮点运算
[root@VM-16-5-centos lesson4]# bcbc 1.06.95Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'. 1+232*364+3=7(standard_in) 3: syntax error2+35^C(interrupt) Exiting bc.[root@VM-16-5-centos lesson4]# echo "1+2+3+4" | bc10语法: shutdown [选项]
常见选项:
热键: 高频被使用的按键
功能1
功能2:
[YX@VM-16-5-centos ~]$ cd bit/lesson2[YX@VM-16-5-centos lesson2]$ cd testtest/ test1/在lesson目录下输入cd后按两下tab键,会自动显示可以输入的文件名。
[YX@VM-16-5-centos lesson2]$ cd ../lesson2/testtest/ test1/ [YX@VM-16-5-centos lesson2]$ cd ../../bit/ .cache/ .config/ [YX@VM-16-5-centos lesson2]$ cd ../../../lighthouse/ pangyilin/ YX/ [YX@VM-16-5-centos lesson2]$ cd ../../../../bin/ data/ etc/ lib/ lost+found/ mnt/ proc/ run/ srv/ tmp/ var/boot/ dev/ home/ lib64/ media/ opt/ root/ sbin/ sys/ usr/也可以这样使用,输入上级目录后按tab键查找。
只返回一个上级目录,无法出现选项,会自动补全剩余目录。
必须要输入两个上级目录才会有此效果,否则系统会先给出一个目录,再按tab键给出全部目录
中断目前程序
在历史命令中进行搜索
键盘输入结束;
退出xshell时,可以使用exit或一直 ctrl + d,表示退出当前用户。
翻阅历史命令