发布时间:2026-07-23阅读(1)
「@Author: Runsen」
基础命令Linux系统的命令通常都是如下所示的格式:
命令名称[命名参数][命令对象]
获取登录信息 - 「w」 / 「who」 / 「last」/ 「lastb」。
maoli@ubuntu:~$w08:07:38up2min,1user,loadaverage:0.83,0.64,0.27USERTTYFROMLOGIN@IDLEJCPUPCPUWHATmaolitty7:008:062:382.29s0.31s/sbin/upstart-maoli@ubuntu:~$whomaolitty72020-05-2808:06(:0)maoli@ubuntu:~$lastmaolitty7:0ThuMay2808:06gone-nologoutrebootsystemboot4.15.0-99-generiThuMay2808:04stillrunningmaolitty7:0FriMay115:20-down(01:26)rebootsystemboot4.15.0-99-generiFriMay115:20-16:47(01:26)maolitty7:0FriMay110:12-crash(05:08)wtmpbeginsFriMay110:12:222020maoli@ubuntu:~$sudolastb[sudo]maoli的密码:btmpbeginsFriMay115:20:292020
查看自己使用的Shell - 「ps」。
Shell也被称为“壳”或“壳程序”,它是用户与操作系统内核交流的翻译官,简单的说就是人与计算机交互的界面和接口。目前很多Linux系统默认的Shell都是bash(Bourne Again SHell),因为它可以使用tab键进行命令和路径补全、可以保存历史命令、可以方便的配置环境变量以及执行批处理操作。
maoli@ubuntu:~$psPIDTTYTIMECMD3621pts/100:00:00bash5082pts/100:00:00ps
查看命令的说明和位置 - 「whatis」 / 「which」 / 「whereis」。
maoli@ubuntu:~$whatispsps(1)-reportasnapshotofthecurrentprocesses.maoli@ubuntu:~$whatispythonpython(1)-aninterpreted,interactive,object-orientedprogrammi...maoli@ubuntu:~$whereispsps:/bin/ps/usr/share/man/man1/ps.1.gzmaoli@ubuntu:~$whereispythonpython:/usr/bin/python3.5m-config/usr/bin/python/usr/bin/python3.5/usr/bin/python3.5m/usr/bin/python2.7/usr/bin/python3.5-config/usr/lib/python3.5/usr/lib/python2.7/etc/python/etc/python3.5/etc/python2.7/usr/local/lib/python3.5/usr/local/lib/python2.7/usr/include/python3.5/usr/include/python3.5m/usr/share/python/usr/share/man/man1/python.1.gzmaoli@ubuntu:~$whichps/bin/psmaoli@ubuntu:~$whichpython/usr/bin/python
清除屏幕上显示的内容 - 「clear」。
查看帮助文档查看帮助文档 - 「man」 / 「info」 / 「help」 / 「apropos」。
maoli@ubuntu:~$ps--helpUsage:ps[options]Tryps--help<simple|list|output|threads|misc|all>orps--help<s|l|o|t|m|a>foradditionalhelptext.Formoredetailsseeps(1).maoli@ubuntu:~$manpsPS(1)UserCommandsPS(1)NAMEps-reportasnapshotofthecurrentprocesses.SYNOPSISps[options]DEscriptION...
查看系统和主机名 - 「uname」 / 「hostname」。
maoli@ubuntu:~$unameLinuxmaoli@ubuntu:~$hostnameubuntu
时间和日期 - 「date」 / 「cal」。
maoli@ubuntu:~$date2020年05月28日星期四08:13:25CSTmaoli@ubuntu:~$cal五月2020日一二三四五六12345678910111213141516171819202122232425262728293031maoli@ubuntu:~$cal52020五月2020日一二三四五六12345678910111213141516171819202122232425262728293031
maoli@ubuntu:~$shutdown-h 5#五分钟关机Shutdownscheduledfor四2020-05-2808:19:24CST,useshutdown-ctocancel.[root~]#maoli@ubuntu:~$shutdown-cmaoli@ubuntu:~$shutdown-r12:00Shutdownscheduledfor四2020-05-2812:00:00CST,useshutdown-ctocancelmaoli@ubuntu:~$shutdown-c
说明:在执行shutdown命令时会向登录系统的用户发出警告,可以在命令后面跟上警告消息来替换默认的警告消息,也可以在-h参数后通过now来表示立刻关机。
退出登录退出登录 - 「exit」 / 「logout」。
查看历史命令查看历史命令 - 「history」。
maoli@ubuntu:~$history...625date626cal627cal52020628shutdown-c629shutdown-r12:00630shutdown-c631historymaoli@ubuntu:~$!631
说明:查看到历史命令之后,可以用!历史命令编号来重新执行该命令;通过history -c可以清除历史命令。
实用程序文件和文件夹操作创建/删除空目录创建/删除空目录 - 「mkdir」 / 「rmdir」。
[root~]#mkdirrunsen[root~]#mkdir-pabc/runsen[root~]#rmdirrunsen
创建/删除文件 - 「touch」 / 「rm」。
maoli@ubuntu:~$touchreadme.txtmaoli@ubuntu:~$rmreadme.txtrm:removeregularemptyfile‘readme.txt’?y[root~]#rm-rfxyz
说明:cd命令后面可以跟相对路径(以当前路径作为参照)或绝对路径(以/开头)来切换到指定的目录,也可以用cd ..来返回上一级目录。返回到上上一级目录应该给cd ../../命令。
查看目录内容查看目录内容 - 「ls」。
查看文件内容 - 「cat」 / 「tac」 / 「head」 / 「tail」 / 「more」 / 「less」 / 「rev」 / 「od」。
maoli@ubuntu:~$wgethttps://www.csdn.net/--2020-05-2808:25:01--https://www.csdn.net/正在解析主机www.csdn.net(www.csdn.net)...47.95.164.112正在连接www.csdn.net(www.csdn.net)|47.95.164.112|:443...已连接。已发出HTTP请求,正在等待回应...200OK长度:未指定[text/html]正在保存至:“index.html”index.html[<=>]420.39K1.53MB/sin0.3s2020-05-2808:25:02(1.53MB/s)-“index.html”已保存[430482]maoli@ubuntu:~$catindex.html<!DOCTYPEhtml>...maoli@ubuntu:~$head-10index.html<!DOCTYPEhtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><metaname="viewport"content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"><metaname="apple-mobile-web-app-status-bar-style"content="black"><metaname="referrer"content="always"><metaname="msvalidate.01"content="3189512127C34C46BC74BED5852D45E4"/><title>CSDN-专业IT技术社区</title>maoli@ubuntu:~$tail-2index.html<scriptsrc="https://g.csdnimg.cn/side-toolbar/2.0/side-toolbar.js"></script></html>maoli@ubuntu:~$lessindex.html#相当于catmaoli@ubuntu:~$cat-nindex.html|more...
说明:上面用到了一个名为wget的命令,它是一个网络下载器程序,可以从指定的URL下载资源。
拷贝/移动文件拷贝/移动文件 - 「cp」 / 「mv」。
maoli@ubuntu:~$mkdirbackupmaoli@ubuntu:~$cpindex.htmlbackup/maoli@ubuntu:~$cdbackup/maoli@ubuntu:~/backup$lsindex.htmlmaoli@ubuntu:~/backup$mvindex.htmlcsdn.htmlmaoli@ubuntu:~/backup$lscsdn.html
文件重命名 - 「rename」。
查找文件和查找内容查找文件和查找内容 - 「find」 / 「grep」。
maoli@ubuntu:~/backup$ find ./ -name "*.html"./csdn.htmlmaoli@ubuntu:~/backup$ find / -name "*.html"/usr/local/java/jdk-11.0.6/README.html/usr/local/python3/lib/python3.6/idlelib/help.html/usr/local/python3/lib/python3.6/test/sgml_input.html/usr/local/python3/lib/python3.6/test/test_difflib_expect.html...maoli@ubuntu:~/backup$ find . -type f -size 2k./csdn.htmlmaoli@ubuntu:~/backup$ find . -type f -name "*.swp" -deletemaoli@ubuntu:~/backup$ grep "<script>" csdn.html -n3192: <script>maoli@ubuntu:~/backup$ grep -E \<\/?script.*\> csdn.html -n12: <script src=//g.csdnimg.cn/tingyun/1.8.3/www.js type=text/javascript></script>18: <script src="//csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>19: <script src="//g.csdnimg.cn/??login-box/1.0.7/login-box.js,login-box/1.0.7/login-auto.js" type="text/javascript"></script>...
说明:grep在搜索字符串时可以使用正则表达式,如果需要使用正则表达式可以用grep -E或者直接使用egrep。
创建链接和查看链接创建链接和查看链接 - 「ln」 / 「readlink」。
maoli@ubuntu:~/backup$ls-lcsdn.html-rw-rw-r--1maolimaoli4304825月2808:30csdn.htmlmaoli@ubuntu:~/backup$ln/home/maoli/backup/csdn.html/home/maoli/csdnmaoli@ubuntu:~/backup$ls-lcsdn.html-rw-rw-r--2maolimaoli4304825月2808:30csdn.html
说明:链接可以分为硬链接和软链接(符号链接)。硬链接可以认为是一个指向文件数据的指针,就像Python中对象的引用计数,每添加一个硬链接,文件的对应链接数就增加1,只有当文件的链接数为0时,文件所对应的存储空间才有可能被其他文件覆盖。我们平常删除文件时其实并没有删除硬盘上的数据,我们删除的只是一个指针,或者说是数据的一条使用记录,所以类似于“文件粉碎机”之类的软件在“粉碎”文件时除了删除文件指针,还会在文件对应的存储区域填入数据来保证文件无法再恢复。软链接类似于Windows系统下的快捷方式,当软链接链接的文件被删除时,软链接也就失效了。
压缩/解压缩和归档/解归档.压缩/解压缩和归档/解归档 - 「gzip」 / 「gunzip」 / 「xz」。
maoli@ubuntu:~$lsredis-4.0.10.tar.tar.gzmaoli@ubuntu:~$gunzipredis-4.0.10.tar.tar.gzmaoli@ubuntu:~$lsredis-4.0.10.tar.tar
归档和解归档 - 「tar」。
maoli@ubuntu:~$tar-xvfredis-4.0.10.tarredis-4.0.10/redis-4.0.10/.gitignoreredis-4.0.10/00-RELEASENOTESredis-4.0.10/BUGSredis-4.0.10/CONTRIBUTINGredis-4.0.10/COPYINGredis-4.0.10/INSTALLredis-4.0.10/MANIFESTOredis-4.0.10/Makefileredis-4.0.10/README.mdredis-4.0.10/deps/redis-4.0.10/deps/Makefileredis-4.0.10/deps/README.md...
说明:归档(也称为创建归档)和解归档都使用tar命令,通常创建归档需要-cvf三个参数,其中c表示创建(create),v表示显示创建归档详情(verbose),f表示指定归档的文件(file);解归档需要加上-xvf参数,其中x表示抽取(extract),其他两个参数跟创建归档相同。
将标准输入转成命令行参数将标准输入转成命令行参数 - 「xargs」。
下面的命令会将查找当前路径下的html文件,然后通过xargs将这些文件作为参数传给rm命令,实现查找并删除文件的操作。
maoli@ubuntu:~$find.-typef-name"*.html"|xargsrm-f
下面的命令将a.txt文件中的多行内容变成一行输出到b.txt文件中,其中<表示从a.txt中读取输入,>表示将命令的执行结果输出到b.txt中。
maoli@ubuntu:~$xargs<a.txt>b.txt
显示文件或目录 - 「basename」 / 「dirname」。
其他相关工具其他相关工具。
maoli@ubuntu:~$catfoo.txtgrapeapplepitayamaoli@ubuntu:~$catbar.txt100200300400maoli@ubuntu:~$pastefoo.txtbar.txtgrape100apple200pitaya300400maoli@ubuntu:~$pastefoo.txtbar.txt>hello.txtmaoli@ubuntu:~$cut-b4-8hello.txtpe10le20aya30maoli@ubuntu:~$cathello.txt|tr\t,grape,100apple,200pitaya,300,400maoli@ubuntu:~$split-l100sohu.htmlhellomaoli@ubuntu:~$wgethttps://www.baidu.com/img/bd_logo1.pngmaoli@ubuntu:~$filebd_logo1.pngbd_logo1.png:PNGimagedata,540x258,8-bitcolormap,non-interlacedmaoli@ubuntu:~$wcindex.html382018696430482index.htmlmaoli@ubuntuwgethttp://www.qq.com-Oqq.htmlmaoli@ubuntuiconv-fgb2312-tutf-8qq.html
管道的使用 - 「|」。
例子:查找当前目录下文件个数。
maoli@ubuntu:~$find./|wc-l80801
例子:列出当前路径下的文件和文件夹,给每一项加一个编号。
maoli@ubuntu:~$ls|cat-n1abc2backup
例子:查找record.log中包含AAA,但不包含BBB的记录的总数
maoli@ubuntu:~$catrecord.log|grepAAA|grep-vBBB|wc-l
输出重定向和错误重定向 - 「>」 / 「>>」 / 「2>」。
maoli@ubuntu:~$catreadme.txtbananaapplegrapeapplegrapewatermelonpearpitayamaoli@ubuntu:~$catreadme.txt|sort|uniq>result.txtmaoli@ubuntu:~$catresult.txtapplebananagrapepearpitayawatermelon
输入重定向 - 「<」。
maoli@ubuntu:~$echohello,world!>hello.txtmaoli@ubuntu:~$echoIwillshowyousomecode.>>hello.txtmaoli@ubuntu:~$cathello.txthello,world!Iwillshowyousomecode.
多重定向 - 「tee」。
下面的命令除了在终端显示命令ls的结果之外,还会追加输出到ls.txt文件中。
maoli@ubuntu:~$ls|tee-als.txtmaoli@ubuntu:~$catls.txtabcbackup
「alias」创建别名
maoli@ubuntu:~$aliasll=ls-lmaoli@ubuntu:~$aliasfrm=rm-rfmaoli@ubuntu:~$ll量144920drwxrwxr-x3maolimaoli40965月2808:20abcmaoli@ubuntu:~$frmabc
「unalias」删除别名
maoli@ubuntu:~$funaliasfrmmaoli@ubuntu:~$frmindex.html-bash:frm:commandnotfound
字符流编辑器 - 「sed」。
sed是操作、过滤和转换文本内容的工具。假设有一个名为fruit.txt的文件,内容如下所示。
maoli@ubuntu:~$cat-nfruit.txt1banana2grape3apple4watermelon5orange
接下来,我们在第2行后面添加一个pitaya。
maoli@ubuntu:~$sed2apitayafruit.txtbananagrapepitayaapplewatermelonorange
在第2行前面插入一个waxberry。
maoli@ubuntu:~$sed2iwaxberryfruit.txtbananawaxberrygrapeapplewatermelonorange
删除第3行。
maoli@ubuntu:~$sed3dfruit.txtbananagrapewatermelonorange
删除第2行到第4行。
maoli@ubuntu:~$sed2,4dfruit.txtbananaorange
将文本中的字符a替换为@。
maoli@ubuntu:~$seds#a#@#fruit.txtb@nanagr@pe@pplew@termelonor@nge
将文本中的字符a替换为@,使用全局模式。
maoli@ubuntu:~$seds#a#@#gfruit.txtb@n@n@gr@pe@pplew@termelonor@nge
模式匹配和处理语言 - 「awk」。
awk是一种编程语言,也是Linux系统中处理文本最为强大的工具,它的作者之一和现在的维护者就是之前提到过的Brian Kernighan(ken和dmr最亲密的伙伴)。通过该命令可以从文本中提取出指定的列、用正则表达式从文本中取出我们想要的内容、显示指定的行以及进行统计和运算,总之它非常强大。
假设有一个名为fruit2.txt的文件,内容如下所示。
\maoli@ubuntu:~$catfruit2.txt1banana1202grape5003apple12304watermelon805orange400
显示文件的第3行。
maoli@ubuntu:~$awkNR==3fruit2.txt3apple1230
显示文件的第2列。
maoli@ubuntu:~$awk{print$2}fruit2.txtbananagrapeapplewatermelonorange
显示文件的最后一列。
maoli@ubuntu:~$awk{print$NF}fruit2.txt120500123080400
输出末尾数字大于等于300的行。
maoli@ubuntu:~$awk{if($3>=300){print$0}}fruit2.txt2grape5003apple12305orange400
创建和删除用户 - 「useradd」 / 「userdel」。需要用root账号创建
maoli@ubuntu:~$suroot密码:root@ubuntu:/home/maoli#useraddRunsenroot@ubuntu:/home/maoli#userdelRunsen
创建和删除用户组 - 「groupadd」 / 「groupdel」。
用户组主要是为了方便对一个组里面所有用户的管理。
修改密码修改密码 - 「passwd」。
root@ubuntu:/home/maoli#passwdmaoliNewpassword:Retypenewpassword:passwd:allauthenticationtokensupdatedsuccessfully.
如果使用passwd命令时没有指定命令作用的对象,则表示要修改当前用户的密码。如果想批量修改用户密码,可以使用chpasswd命令。
查看和修改密码有效期 - 「chage」。
设置maoli用户100天后必须修改密码,过期前15天通知该用户,过期后15天禁用该用户。
root@ubuntu:/home/maoli#chage-M100-W15-I15maoli
root@ubuntu:/home/maoli#sumaolimaoli@ubuntu:~$
以管理员身份执行命令 - 「sudo」。
maoli@ubuntu:~$ls/rootls:cannotopendirectory/root:Permissiondeniedmaoli@ubuntu:~$sudols/root[sudo]passwordformaoli:
「说明」:如果希望用户能够以管理员身份执行命令,用户必须要出现在sudoers名单中,sudoers文件在 /etc目录下,如果希望直接编辑该文件也可以使用下面的命令。
编辑sudoers文件编辑sudoers文件 - 「visudo」。
这里使用的编辑器是vim,关于vim的知识在前面有讲解。
五十二、ViM的使用 该文件的部分内容如下所示:
##AllowroottorunanycommandsanywhererootALL=(ALL)ALL##Allowsmembersofthesysgrouptorunnetworking,software,##servicemanagementappsandmore.#%sysALL=NETWORKING,SOFTWARE,SERVICES,STORAGE,DELEGATING,PROCESSES,LOCATE,DRIVERS##Allowspeopleingroupwheeltorunallcommands%wheelALL=(ALL)ALL##Samethingwithoutapassword#%wheelALL=(ALL)NOPASSWD:ALL##Allowsmembersoftheusersgrouptomountandunmountthe##cdromasroot#%usersALL=/sbin/mount/mnt/cdrom,/sbin/umount/mnt/cdrom##Allowsmembersoftheusersgrouptoshutdownthissystem#%userslocalhost=/sbin/shutdown-hnow
显示用户与用户组的信息 - 「id」。
root@ubuntu:/home/maoli#iduid=0(root)gid=0(root)组=0(root)
给其他用户发消息 -「write」 / 「wall」。
发送方:
root@ubuntu#writemaoliHelloMaoliEOF
键入EOF表示信息结束,用Crtl D组合键发送信息。输入内容会出现在用户的屏幕上,同时通信中止。 接收方:
maoli@ubuntu:~$Messagefromrootonpts/0at9:41...HelloMaoliEOF
maoli@ubuntu:~$mesgisymaoli@ubuntu:~$mesgnmaoli@ubuntu:~$mesgisn
如果想要发送一条信息给系统中所有用户,可以使用wall命令,wall表示:write all。输入wall,然后编辑信息,如果shell支持可以使用中文。然后使用Crtl D组合键发送信息。这样系统所有登录用户的桌面会收到信息。如 果在网络上,可以使用rwall命令把信息发送到局域网上所有的用户。
文件系统文件和路径maoli@ubuntu:~/backup$ls-l总用量424-rw-rw-r--2maolimaoli4304825月2808:30csdn.htmlmaoli@ubuntu:~/backup$chmodg w,o wcsdn.htmlmaoli@ubuntu:~/backup$ls-l总用量424-rw-rw-rw-2maolimaoli4304825月2808:30csdn.htmlmaoli@ubuntu:~/backup$chmod644csdn.htmlmaoli@ubuntu:~/backup$ls-l总用量424-rw-r--r--2maolimaoli4304825月2808:30csdn.html
说明:通过上面的例子可以看出,用chmod改变文件模式比特有两种方式:一种是字符设定法,另一种是数字设定法。除了chmod之外,可以通过umask来设定哪些权限将在新文件的默认权限中被删除。

maoli@ubuntu:~/backup$ls-l总用量424-rw-r--r--2maolimaoli4304825月2808:30csdn.htmlmaoli@ubuntu:~/backup$sudochownrootcsdn.html[sudo]maoli的密码:maoli@ubuntu:~/backup$ls-l总用量424-rw-r--r--2rootmaoli4304825月2808:30csdn.html
❝
本文已收录 GitHub,传送门~[1] ,里面更有大厂面试完整考点,欢迎 Star。
❞
Reference[1]
传送门~: https://github.com/MaoliRUNsen/runsenlearnpy100
上一篇:哪些人适合读博士
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图