Linux系統(tǒng)發(fā)行版本
基礎(chǔ)命令
Linux系統(tǒng)的命令通常都是如下所示的格式:
命令名稱 [命名參數(shù)] [命令對(duì)象]
-
獲取登錄信息 - w / who / last皇型。
[root@izwz97tbgo9lkabnat2lo8z ~]# w 23:31:16 up 12:16, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 182.139.66.250 23:03 4.00s 0.02s 0.00s w jackfrue pts/1 182.139.66.250 23:26 3:56 0.00s 0.00s -bash [root@izwz97tbgo9lkabnat2lo8z ~]# who root pts/0 2018-04-12 23:03 (182.139.66.250) jackfrued pts/1 2018-04-12 23:26 (182.139.66.250) [root@izwz97tbgo9lkabnat2lo8z ~]# who am i root pts/0 2018-04-12 23:03 (182.139.66.250)
-
查看自己使用的Shell - ps导俘。
Shell也被稱為“殼”徒溪,它是用戶與內(nèi)核交流的翻譯官严拒,簡(jiǎn)單的說(shuō)就是人與計(jì)算機(jī)交互的接口。目前很多Linux系統(tǒng)默認(rèn)的Shell都是bash(Bourne Again SHell),因?yàn)樗梢允褂肨ab鍵進(jìn)行命令補(bǔ)全、可以保存歷史命令拧略、可以方便的配置環(huán)境變量以及執(zhí)行批處理操作等。
[root@izwz97tbgo9lkabnat2lo8z ~]# ps PID TTY TIME CMD 3531 pts/0 00:00:00 bash 3553 pts/0 00:00:00 ps
-
查看命令的說(shuō)明 - whatis瘪弓。
[root@izwz97tbgo9lkabnat2lo8z ~]# whatis ps ps (1) - report a snapshot of the current processes. [root@izwz97tbgo9lkabnat2lo8z ~]# whatis python python (1) - an interpreted, interactive, object-oriented programming language
-
查看命令的位置 - which / whereis垫蛆。
[root@izwz97tbgo9lkabnat2lo8z ~]# whereis ps ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz [root@izwz97tbgo9lkabnat2lo8z ~]# whereis python python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz [root@izwz97tbgo9lkabnat2lo8z ~]# which ps /usr/bin/ps [root@izwz97tbgo9lkabnat2lo8z ~]# which python /usr/bin/python
-
查看幫助文檔 - man / info / apropos。
[root@izwz97tbgo9lkabnat2lo8z ~]# ps --help Usage: ps [options] Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text. For more details see ps(1). [root@izwz97tbgo9lkabnat2lo8z ~]# man ps PS(1) User Commands PS(1) NAME ps - report a snapshot of the current processes. SYNOPSIS ps [options] DESCRIPTION ... [root@izwz97tbgo9lkabnat2lo8z ~]# info ps ...
-
切換用戶 - su。
[root@izwz97tbgo9lkabnat2lo8z ~]# su hellokitty [hellokitty@izwz97tbgo9lkabnat2lo8z root]$
-
以管理員身份執(zhí)行命令 - sudo袱饭。
[jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ ls /root ls: cannot open directory /root: Permission denied [jackfrued@izwz97tbgo9lkabnat2lo8z ~]$ sudo ls /root [sudo] password for jackfrued: calendar.py code error.txt hehe hello.c index.html myconf result.txt
說(shuō)明:如果希望用戶能夠以管理員身份執(zhí)行命令川无,用戶必須被添加到sudoers名單中,該文件在
/etc
目錄下虑乖。 -
登入登出相關(guān) - logout / exit / adduser / userdel / passwd / ssh懦趋。
[root@izwz97tbgo9lkabnat2lo8z ~]# adduser hellokitty [root@izwz97tbgo9lkabnat2lo8z ~]# passwd hellokitty Changing password for user jackfrued. New password: Retype new password: passwd: all authentication tokens updated successfully. [root@izwz97tbgo9lkabnat2lo8z ~]# ssh hellokitty@1.2.3.4 hellokitty@1.2.3.4's password: Last login: Thu Apr 12 23:05:32 2018 from 10.12.14.16 [hellokitty@izwz97tbgo9lkabnat2lo8z ~]$ logout Connection to 1.2.3.4 closed. [root@izwz97tbgo9lkabnat2lo8z ~]#
-
查看系統(tǒng)和主機(jī)名 - uname / hostname。
[root@izwz97tbgo9lkabnat2lo8z ~]# uname Linux [root@izwz97tbgo9lkabnat2lo8z ~]# hostname izwz97tbgo9lkabnat2lo8z [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core)
重啟和關(guān)機(jī) - reboot / init 6 / shutdown / init 0疹味。
查看歷史命令 - history仅叫。
```source-shell
[root@iZwz97tbgo9lkabnat2lo8Z ~]# history
...
452 ls
453 cd Python-3.6.5/
454 clear
455 history
[root@iZwz97tbgo9lkabnat2lo8Z ~]# !454
```
> 說(shuō)明:查看到歷史命令之后,可以用`!歷史命令編號(hào)`來(lái)重新執(zhí)行該命令糙捺;通過(guò)`history -c`可以清除歷史命令诫咱。
實(shí)用程序
文件和文件夾操作
-
創(chuàng)建/刪除目錄 - mkdir / rmdir。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir abc [root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir -p xyz/abc [root@iZwz97tbgo9lkabnat2lo8Z ~]# rmdir abc
-
創(chuàng)建/刪除文件 - touch / rm洪灯。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# touch readme.txt [root@iZwz97tbgo9lkabnat2lo8Z ~]# touch error.txt [root@iZwz97tbgo9lkabnat2lo8Z ~]# rm error.txt rm: remove regular empty file ‘error.txt’? y [root@iZwz97tbgo9lkabnat2lo8Z ~]# rm -rf xyz
- touch命令用于創(chuàng)建空白文件或修改文件時(shí)間坎缭。在Linux系統(tǒng)中一個(gè)文件有三種時(shí)間:
- 更改內(nèi)容的時(shí)間 - mtime。
- 更改權(quán)限的時(shí)間 - ctime婴渡。
- 最后訪問(wèn)時(shí)間 - atime幻锁。
- rm的幾個(gè)重要參數(shù):
- -i:交互式刪除,每個(gè)刪除項(xiàng)都會(huì)進(jìn)行詢問(wèn)边臼。
- -r:刪除目錄并遞歸的刪除目錄中的文件和目錄。
- -f:強(qiáng)制刪除假消,忽略不存在的文件柠并,沒(méi)有任何提示。
- touch命令用于創(chuàng)建空白文件或修改文件時(shí)間坎缭。在Linux系統(tǒng)中一個(gè)文件有三種時(shí)間:
-
切換和查看當(dāng)前工作目錄 - cd / pwd富拗。
說(shuō)明:
cd
命令后面可以跟相對(duì)路徑(以當(dāng)前路徑作為參照)或絕對(duì)路徑(以/
開(kāi)頭)來(lái)切換到指定的目錄臼予,也可以用cd ..
來(lái)返回上一級(jí)目錄。 -
查看目錄內(nèi)容 - ls啃沪。
- -l:以長(zhǎng)格式查看文件和目錄粘拾。
- -a:顯示以點(diǎn)開(kāi)頭的文件和目錄(隱藏文件)。
- -R:遇到目錄要進(jìn)行遞歸展開(kāi)(繼續(xù)列出目錄下面的文件和目錄)创千。
- -d:只列出目錄缰雇,不列出其他內(nèi)容。
- -S/-t:按大小/時(shí)間排序追驴。
-
查看文件內(nèi)容 - cat / head / tail / more / less械哟。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://www.sohu.com/ -O sohu.html --2018-06-20 18:42:34-- http://www.sohu.com/ Resolving www.sohu.com (www.sohu.com)... 14.18.240.6 Connecting to www.sohu.com (www.sohu.com)|14.18.240.6|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 212527 (208K) [text/html] Saving to: ‘sohu.html’ 100%[==================================================>] 212,527 --.-K/s in 0.03s 2018-06-20 18:42:34 (7.48 MB/s) - ‘sohu.html’ saved [212527/212527] [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sohu.html ... [root@iZwz97tbgo9lkabnat2lo8Z ~]# head -10 sohu.html <!DOCTYPE html> <html> <head> <title>搜狐</title> <meta name="Keywords" content="搜狐,門戶網(wǎng)站,新媒體,網(wǎng)絡(luò)媒體,新聞,財(cái)經(jīng),體育,娛樂(lè),時(shí)尚,汽車,房產(chǎn),科技,圖片,論壇,微博,博客,視頻,電影,電視劇"/> <meta name="Description" content="搜狐網(wǎng)為用戶提供24小時(shí)不間斷的最新資訊,及搜索殿雪、郵件等網(wǎng)絡(luò)服務(wù)暇咆。內(nèi)容包括全球熱點(diǎn)事件、突發(fā)新聞、時(shí)事評(píng)論爸业、熱播影視劇其骄、體育賽事、行業(yè)動(dòng)態(tài)扯旷、生活服務(wù)信息年栓,以及論壇、博客薄霜、微博某抓、我的搜狐等互動(dòng)空間。" /> <meta name="shenma-site-verification" content="1237e4d02a3d8d73e96cbd97b699e9c3_1504254750"> <meta charset="utf-8"/> <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/> [root@iZwz97tbgo9lkabnat2lo8Z ~]# tail -2 sohu.html </body> </html> [root@iZwz97tbgo9lkabnat2lo8Z ~]# less sohu.html ... [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat -n sohu.html | more ...
-
拷貝/移動(dòng)文件 - cp / mv惰瓜。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mkdir backup [root@iZwz97tbgo9lkabnat2lo8Z ~]# cp sohu.html backup/ [root@iZwz97tbgo9lkabnat2lo8Z ~]# cd backup [root@iZwz97tbgo9lkabnat2lo8Z backup]# ls sohu.html [root@iZwz97tbgo9lkabnat2lo8Z backup]# mv sohu.html sohu_index.html [root@iZwz97tbgo9lkabnat2lo8Z backup]# ls sohu_index.html
-
查找文件和查找內(nèi)容 - find / grep否副。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# find / -name "*.html" /root/sohu.html /root/backup/sohu_index.html [root@izwz97tbgo9lkabnat2lo8z ~]# find . -atime 7 -type f -print [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -size +2k [root@izwz97tbgo9lkabnat2lo8z ~]# find . -type f -name "*.swp" -delete [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep "<script>" sohu.html -n 20:<script> [root@iZwz97tbgo9lkabnat2lo8Z ~]# grep -E \<\/?script.*\> sohu.html -n 20:<script> 22:</script> 24:<script src="http://statics.itc.cn/web/v3/static/js/es5-shim-08e41cfc3e.min.js"></script> 25:<script src="http://statics.itc.cn/web/v3/static/js/es5-sham-1d5fa1124b.min.js"></script> 26:<script src="http://statics.itc.cn/web/v3/static/js/html5shiv-21fc8c2ba6.js"></script> 29:<script type="text/javascript"> 52:</script> ...
說(shuō)明:
grep
在搜索字符串時(shí)可以使用正則表達(dá)式,如果需要使用正則表達(dá)式可以用grep -E
或者直接使用egrep
崎坊。 -
鏈接 - ln备禀。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 1 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 2 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln /root/sohu.html /root/backup/sohu_backup2 [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sohu.html -rw-r--r-- 3 root root 212131 Jun 20 19:15 sohu.html [root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /etc/centos-release sysinfo [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls -l sysinfo lrwxrwxrwx 1 root root 19 Jun 20 19:21 sysinfo -> /etc/centos-release [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat sysinfo CentOS Linux release 7.4.1708 (Core) [root@iZwz97tbgo9lkabnat2lo8Z ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core)
說(shuō)明:鏈接可以分為硬鏈接和軟鏈接(符號(hào)鏈接)。硬鏈接可以認(rèn)為是一個(gè)指向文件數(shù)據(jù)的指針奈揍,就像Python中對(duì)象的引用計(jì)數(shù)曲尸,每添加一個(gè)硬鏈接,文件的對(duì)應(yīng)鏈接數(shù)就增加1男翰,只有當(dāng)文件的鏈接數(shù)為0時(shí)另患,文件所對(duì)應(yīng)的存儲(chǔ)空間才有可能被其他文件覆蓋。我們平常刪除文件時(shí)其實(shí)并沒(méi)有刪除硬盤上的數(shù)據(jù)蛾绎,我們刪除的只是一個(gè)指針昆箕,或者說(shuō)是數(shù)據(jù)的一條使用記錄,所以類似于“文件粉碎機(jī)”之類的軟件在“粉碎”文件時(shí)除了刪除文件指針租冠,還會(huì)在文件對(duì)應(yīng)的存儲(chǔ)區(qū)域填入數(shù)據(jù)來(lái)保證文件無(wú)法再恢復(fù)鹏倘。軟鏈接類似于Windows系統(tǒng)下的快捷方式,當(dāng)軟鏈接鏈接的文件被刪除時(shí)顽爹,軟鏈接也就失效了纤泵。
-
壓縮/解壓縮和歸檔/解歸檔 - gzip / gunzip / xz / tar。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz --2018-06-20 19:29:59-- http://download.redis.io/releases/redis-4.0.10.tar.gz Resolving download.redis.io (download.redis.io)... 109.74.203.151 Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1738465 (1.7M) [application/x-gzip] Saving to: ‘redis-4.0.10.tar.gz’ 100%[==================================================>] 1,738,465 70.1KB/s in 74s 2018-06-20 19:31:14 (22.9 KB/s) - ‘redis-4.0.10.tar.gz’ saved [1738465/1738465] [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* redis-4.0.10.tar.gz [root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-4.0.10.tar.gz [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* redis-4.0.10.tar [root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-4.0.10.tar redis-4.0.10/ redis-4.0.10/.gitignore redis-4.0.10/00-RELEASENOTES redis-4.0.10/BUGS redis-4.0.10/CONTRIBUTING redis-4.0.10/COPYING redis-4.0.10/INSTALL redis-4.0.10/MANIFESTO redis-4.0.10/Makefile redis-4.0.10/README.md redis-4.0.10/deps/ redis-4.0.10/deps/Makefile redis-4.0.10/deps/README.md ... [root@iZwz97tbgo9lkabnat2lo8Z ~]# ls redis* redis-4.0.10.tar redis-4.0.10: 00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests BUGS deps MANIFESTO runtest sentinel.conf utils CONTRIBUTING INSTALL README.md runtest-cluster src
其他工具 - sort / uniq / diff / tr / cut / paste / file / wc镜粤。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat foo.txt
grape
apple
pitaya
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat bar.txt
100
200
300
400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt
grape 100
apple 200
pitaya 300
400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# paste foo.txt bar.txt > hello.txt
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cut -b 4-8 hello.txt
pe 10
le 20
aya 3
0
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cat hello.txt | tr '\t' ','
grape,100
apple,200
pitaya,300
,400
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.baidu.com/img/bd_logo1.png
--2018-06-20 18:46:53-- https://www.baidu.com/img/bd_logo1.png
Resolving www.baidu.com (www.baidu.com)... 220.181.111.188, 220.181.112.244
Connecting to www.baidu.com (www.baidu.com)|220.181.111.188|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7877 (7.7K) [image/png]
Saving to: ‘bd_logo1.png’
100%[==================================================>] 7,877 --.-K/s in 0s
2018-06-20 18:46:53 (118 MB/s) - ‘bd_logo1.png’ saved [7877/7877][root@iZwz97tbgo9lkabnat2lo8Z ~]# file bd_logo1.png
bd_logo1.png: PNG image data, 540 x 258, 8-bit colormap, non-interlaced
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wc sohu.html
2979 6355 212527 sohu.html
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wc -l sohu.html
2979 sohu.html