linux 指令
基礎指令
1. ls指令:list
用法1: # ls
含意:列出當前工作目錄下的所有文件/文件夾的名稱
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
用法2:# ls 路徑
含意:列出指定路徑下的所有文件/文件夾名稱
-
相對路徑:首先得有一個參照物(一般都是當前工作路徑)
相對路徑的寫法:在相對路徑中通常會用到2個符號"./"(表示當前目錄下)、"../"(表示上一級目錄下)
-
絕對路徑:不需要參照物,直接從根"/"開始尋找
用法3: #ls 選項 路徑
含意:在列出指定路徑下的文件/文件夾的名稱,並以指定的格式進行顯示
常見的語法:
#ls -l 路徑
#ls -la 路徑
選項解釋:
-l:表示list,表示以詳系列表的形式進行展示
-a:表示顯示所有文件/文件夾(包含了隱藏文件/文件夾)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
表示文件/文件夾的權限訊息
其中"-"表示該行對應的文檔類行為文件,"d"表示文檔類行為文件夾。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
用戶組
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
大小
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在linux中隱藏文檔一般都是以"."開頭
用法4: #ls -lh 路徑
含意:列出指定路徑下的所有文件/文件夾的名稱,以列表的形式並且顯示文檔大小的時候以可讀性較高的形式顯示
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在linux裡,目錄都是4K
2. pwd指令
用法: #pwd (print working directory)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
3.cd命令
命令: #cd (change directory,改變目錄)
作用:用於切換當前的工作目錄
語法:#cd 路徑
案例:當前在"/"下,需要使用絕對路徑切換到/下載。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
案例:當前在"/下載"下,需要使用絕對路徑切換到"test/test1"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在linux中有一個特殊符號"~",表示當前用戶的家目錄。
切換方式:#cd ~
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在這裡的"~"代表的是home/alan 目錄 並非是root,所以使用絕對路徑時開頭必須是 cd /home/alan/路徑
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
4. mkdir指令
指令:mkdir (make directory,創建目錄)
語法:#mkdir 路徑 [路徑,可以市文件夾名稱也可以是包含名稱的完整路徑]
案例:在當前目錄下創建出目錄"test"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
注意:ls列出的結果顏色說明,其中藍色的名稱表示文件夾,黑色表示文件,綠色的表示齊全限為擁有所有權限(rwx)
案例:在指定目錄下創建出目錄"test"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
語法2: #mkdir -p 路徑
含意:當一次性創建多層不存在的目錄的時候,添加-p參數,否則會報錯
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
語法3: #mkdir 路徑1 路徑2 路徑3…
含意:一次性創建多個目錄
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
5. touch指令
指令:touch
作用:創建文件
語法:#touch 文件路徑 [路徑可以是直接的文件明也可以是路徑]
案例:使用touch來創建一個文件,命名為linux.txt
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
案例:使用touch來同時創建多個文件
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
案例:使用touch來在"alan"用戶的家目錄中創建文件,linux.txt
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
6. cp指令
指令:cp(copy,複製)
作用:複製文件/文件夾到指定的位置
語法:#cp 被複製的文檔路徑 文檔被複製到的路徑
案例:使用cp命令來複製一個文件
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
注意:linux在複製過程中是可以重新對心位置的文件進行重命名的,但如果不是必須的,建議保持前後名稱一致
案例:使用cp命令來複製一個文件夾
注意:當使用cp命令進行文件夾複製操作的時候需要添加選項"-r"[表示遞歸複製],否則會被目錄忽略
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
7.mv指令
指令:mv (move,移動)
作用:移動文檔到新的位置
語法:#mv 需要移動的文檔路徑 需要保存的位置路徑
確認:移動之後原始的文件是否還在原來的位置
案例:使用mv命令移動一個文件
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
案例:使用mv命令移動一個文件夾
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
補充:在linux中重命名的命令也是mv,語法和移動語法一樣。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
8.rm命令
指令:rm(remove,移除,刪除)
作用:移除/刪除文檔
語法:#rm 選項 需要移除的文檔路徑
選項:
-f:強制刪除,不提示是否刪除
-r:表示遞歸
案例:刪除一個文件

注意:如果在刪除的時候不想頻繁確認,則可以在指令中添加選項"-f",表示force(強制)。

案例:刪除一個文件夾

注意:刪除一個目錄的時候需要做遞歸刪除,並且一般也不需要進行刪除確認詢問,所以移動目錄的時候一般需要使用-rf選項。
案例:刪除多個文檔

案例:要刪除一個目錄下有公共特性的文檔,例如都以test開頭

其中稱之為通配符,意思表示任意的字符,test,則表示只要文件以test開頭,後續字符則不管。
9. vim命令
指令:vim (vim是一款文本編輯器)
語法:# vim 文件的路徑
作用:打開一個文件(可以不存在,也可以存在)
案例:使用vim來打開文件

退出打開的文件:先按esc,再按下ahift+英文冒號(:),輸入wq,按下enter即可
10. 輸出重新定向
一般命令的輸出都會顯示在終端中,有些時候需要將一些命令的執行結果想要保存到文件中進行後續的分析/統計,則這時候需要使用到的輸出重定向技術
:覆蓋輸出,會覆蓋掉原先的文件內容
:追加輸出,不會覆蓋原始文件內容,會在原始內容末尾繼續添加
語法:#正常執行指令 >/>> 文件的路徑
注意:文件可以不存在,不存在則新建
案例:使用覆蓋重定向,保存ls -la的執行結果,保存到當前目錄下的test.txt

案例:使用追加重定向,保存ls -la的執行結果到test.txt中

11. cat指令
作用1:cat有直接打開一個文件的功能。
語法1:# cat 文件的路徑

作用2:cat 還可以對文件進行合併
語法2:#cat 待合併的文件路徑1 待合併的文件路徑2 … 文件路徑n > 合併之後的文件路徑
例如:合併2個文件,並儲存到一個文件中『搭配輸出重定向使用』

進階指令
1. df指令
作用:查看磁盤空間
語法:#df -h -h表示以可讀性較高的形式展示大小

2. free指令
作用:查看內存使用情況
語法:#free -m -m表示以mb為單位查看

剩餘的真實可用內存為28424mb。
swap:用於臨時內存,當系統真實內存不夠用時可以臨時使用磁盤空間來充當內存
3. head指令
作用:查看一個文件的前n行,如果不指定n,則默認顯示前10行。
語法:# head -n 文件路徑 『n表示數字』

4. tail指令
作用:查看一個文件的末n行,如果n不指定,默認顯示末10行

作用2:可以通過tail指令來查看一個文件的動態變化內容『變化的內容不能是用戶手動增加的』
語法:#tail -f 文件路徑
該命令一般用於查看系統的日誌比較多

5. less指令
作用:查看文件,以較少的內容進行輸出,按下輔助功能鍵(數字+enter、空格鍵+上下方向鍵)查看更多
語法:#less 需要查看的文件路徑
退出時按q鍵即可。

6.wc指令(word count)
作用:統計文件內容信息(包含行數、單詞數、字節數)
語法:#wc -lwc 需要統計的文件路徑
-l:表示lines,行數(最常用)
-w:表示words,單詞數,依照空格來判斷單詞數量
-c:表示bytes,字節數

7. date指令(重點)
作用:表示操作時間日期(讀取、設置)
語法1:#date

語法2:#date +%F
和#date '+%Y-%m-%d'結果一樣

語法3:#date +%F\ %T
和#date '+%Y-%m-%d %H:%M:%S'結果一樣
也和#date +%Y-%m-%d\ %H:%M:%S結果一樣

語法4:獲取之前或者之後的某個時間(備份)
#date -d '+/- day/year' +%F\ %T
符號可選值:+(之後) 或者 -(之前)
單位可選值:day(天)、month(月)、year(年)
- %F:表示完整的年月日
- %T:表示完整的時分秒
- %Y:表示四位年份
- %m:表示兩位月份
- %d:表示日期
- %H:表示小時
- %M:表示分鐘
- %S:表示秒數
8. cal指令
作用:操作日曆
語法1:#cal 等價於 #cal -1 直接輸出當前月份日曆
語法2:#cal -3 輸出上個月+本月+下個月的日曆
語法3:#cal -y 年份 輸出某年份的日曆

9. clear/ctrl+L 指令
作用:清除終端中已經存在的命令和結果(信息)。
語法:clear 或者快捷鍵:ctrl+L
需要注意,該命令並非真的清除之前信息,而是把之前信息隱藏到最上面,通過滾動條可查看之前輸入的指令
在kubuntu中,clear會清除之前訊息,但不會清除指令,而快捷鍵則是隱藏之前信息到最上面
10. 管道(重要)
管道符:|
作用:一般用於"過濾","特殊","擴展處理"。
語法:管道不能單獨使用,必須配合一些指令一起使用,輔助作用。
過濾案例(100%使用):需要通過管道查詢出當前目錄下包含"o"字母的文檔名稱
#ls | grep o

針對上面命令說明:
- 以管道作為分界線,前面的命令有個輸出,後面需要先輸入,然後再過濾,最後再輸出,通俗的講就是管道前面的輸出是後面指令的輸入
- grep指令:主要用於過濾
特殊用法案例:通過管道的操作方法來實現less的等價效果(了解就好)
通過less查看一個文件,可以#less 路徑
現在通過管道可以寫成這樣:#cat 路徑 | less
擴展處理:使用學過的命令,統計某個目錄下的文檔總個數
答:#ls | wc -l