Linux下基礎指令
ls 指令
語法: ls [ 選項 ] [ ?錄或?件 ]
功能:對于?錄,該命令列出該?錄下的所有??錄與?件。對于?件,將列出?件名以及其他信
息。
常?選項:
-a 列出?錄下的所有?件,包括以 . 開頭的隱含?件。
-d 將?錄像?件?樣顯?,?不是顯?其下的?件。 如:ls ?d 指定?錄
-i 輸出?件的 i 節點的索引信息。 如 ls ?ai 指定?件
-k 以 k 字節的形式表??件的??。ls ?alk 指定?件
-l 列出?件的詳細信息
-n ?數字的 UID,GID 代替名稱。 (介紹 UID, GID)
-F 在每個?件名后附上?個字符以說明該?件的類型,“*”表?可執?的普通?件;“/”表?
?錄;“@”表?符號鏈接;“|”表?FIFOs;“=”表?套接字(sockets)。(?錄類型識別)
-r 對?錄反向排序
-t 以時間排序
-s 在l?件名后輸出該?件的??。(??排序,如何找到?錄下最?的?件)
-R 列出所有??錄下的?件。(遞歸)
-1 ??只輸出?個?件。
舉例:
[root@VM-8-16-centos ~]# ls #列出root目錄下的文件
repos
[root@VM-8-16-centos ~]# ls -l #詳細列出root目錄下的文件,包括文件大小 創建時間等
total 4
drwxr-xr-x 2 root root 4096 May 20 14:02 repos
[root@VM-8-16-centos ~]# ls -a #詳細列出root目錄下包括隱藏的文件,帶.開頭的文件都是隱藏文件
. .bash_logout .cache .lesshst .pydistutils.cfg .tcshrc
.. .bash_profile .config .pip repos .viminfo
.bash_history .bashrc .cshrc .pki .ssh
[root@VM-8-16-centos ~]# ls -al #指令可以組合使用,即列出詳細信息和隱藏文件
total 68
dr-xr-x---. 8 root root 4096 May 20 14:20 .
dr-xr-xr-x. 19 root root 4096 May 23 08:35 ..
-rw------- 1 root root 1071 May 23 08:35 .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 May 20 03:10 .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 35 May 20 14:20 .lesshst
drwxr-xr-x 2 root root 4096 Jun 13 2024 .pip
drwxr----- 3 root root 4096 Jun 13 2024 .pki
-rw-r--r-- 1 root root 73 Jun 13 2024 .pydistutils.cfg
drwxr-xr-x 2 root root 4096 May 20 14:02 repos
drwx------ 2 root root 4096 May 19 23:18 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw------- 1 root root 911 May 20 14:17 .viminfo
[root@VM-8-16-centos ~]# ll #簡寫指令==ls -l
total 4
drwxr-xr-x 2 root root 4096 May 20 14:02 repos
[root@VM-8-16-centos ~]#
pwd 命令
語法: pwd
功能:顯???當前所在的?錄
常?選項:
無
舉例:
[root@VM-8-16-centos repos]# pwd
/root/repos
cd 指令
Linux理論知識:路徑的認識
Linux系統中,磁盤上的?件和?錄被組成?棵?錄樹,每個節點都是?錄或?件
其中普通?件?定是?錄樹的葉?節點
?錄可能是葉?(空?錄), 也可能是路上節點
理解路徑存在的意義: 樹狀組織?式,都是為了保證快速定位查找到指定的?件,?定位?件就
需要具有唯?性的?案來進?定位?件。其中任何?個節點,都只有?個?節點,所以,從根?
錄開始,定位指定?件,路徑具有唯?性
絕對路徑:?般從/開始,不依賴其他?錄的定位?件的?式
相對路徑:相對于當前??所處?錄,定位?件的路徑?式
絕對路徑?般不會隨著??的路徑變化?喪失唯?性,?般在特定服務的配置?件中經常被使?
相對路徑因為它的便捷性,?般在命令?中使?較多
語法: cd ?錄名
功能:改變?作?錄。將當前?作?錄改變到指定的?錄下
舉例:
[root@VM-8-16-centos repos]# pwd
/root/repos
[root@VM-8-16-centos repos]# cd .. #使用相對路徑返回上級目錄
[root@VM-8-16-centos ~]# pwd
/root
[root@VM-8-16-centos ~]# cd /root/repos #使用絕對路徑轉到目錄
[root@VM-8-16-centos repos]# pwd
/root/repos
[root@VM-8-16-centos repos]# cd ~ #返回家目錄
[root@VM-8-16-centos ~]# pwd
/root
[root@VM-8-16-centos ~]# cd - #返回最近一次所處目錄
/root/repos
[root@VM-8-16-centos repos]#
touch 指令
Linux理論知識:?件類型的認識
語法: touch [ 選項 ]... ?件 ...
功能:touch命令參數可更改?檔或?錄的?期時間,包括存取時間和更改時間,或者新建?個不存在的?件。
常?選項:
-a : 僅更改訪(access)問時間
?
-c : 僅更改修改(modify)時間
舉例:
[root@VM-8-16-centos repos]# ls
test.txt
[root@VM-8-16-centos repos]# touch Newtest.txt #創建一個普通文件
[root@VM-8-16-centos repos]# ls
Newtest.txt test.txt
[root@VM-8-16-centos repos]# stat Newtest.txt #查看文件詳情,包括access時間和modify時間File: ‘Newtest.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 661227 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-05-23 08:59:26.050370194 +0800
Modify: 2025-05-23 08:59:26.050370194 +0800
Change: 2025-05-23 08:59:26.050370194 +0800Birth: -
[root@VM-8-16-centos repos]# touch -a Newtest.txt #修改access時間
[root@VM-8-16-centos repos]# stat Newtest.txt File: ‘Newtest.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 661227 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-05-23 09:01:14.791166007 +0800
Modify: 2025-05-23 08:59:26.050370194 +0800
Change: 2025-05-23 09:01:14.791166007 +0800Birth: -
[root@VM-8-16-centos repos]# touch -m Newtest.txt #修改modify時間
[root@VM-8-16-centos repos]# stat Newtest.txt File: ‘Newtest.txt’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 661227 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2025-05-23 09:01:14.791166007 +0800
Modify: 2025-05-23 09:01:56.621087369 +0800
Change: 2025-05-23 09:01:56.621087369 +0800Birth: -
mkdir 指令
語法: mkdir [ 選項 ] dirname...
功能:在當前?錄下創建?個名為 “dirname”的?錄
常?選項:
-p/--parents: 可以是?個路徑名稱。此時若路徑中的某些?錄尚不存在,加上此選項后,系統將?
動建?好那些尚不存在的?錄,即?次可以建?多個?錄
舉例:
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# mkdir mydir #創建一個空目錄
[root@VM-8-16-centos repos]# ll
total 4
drwxr-xr-x 2 root root 4096 May 23 09:07 mydir
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# mkdir -p path1/path2/path3 #遞歸建?多個?錄,創建指定路徑
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:07 mydir
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
drwxr-xr-x 3 root root 4096 May 23 09:07 path1
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# tree #tree命令是?個遞歸以樹狀結構顯?指定?錄的命令
.
├── mydir
├── Newtest.txt
├── path1
│?? └── path2
│?? └── path3
└── test.txt
rmdir 指令 && rm 指令
rmdir是?個與mkdir相對應的命令,mkdir是建??錄,?rmdir是刪除命令
語法: rmdir [-p] [dirName]
適?對象:具有當前?錄操作權限的所有使?者
功能:刪除空?錄
常?選項:
-p 當??錄被刪除后如果??錄也變成空?錄的話,就連帶??錄?起刪除。
[root@VM-8-16-centos repos]# rmdir mydir #刪除空目錄
[root@VM-8-16-centos repos]# ll
total 4
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
drwxr-xr-x 3 root root 4096 May 23 09:07 path1
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
[root@VM-8-16-centos repos]# rmdir -p path1/path2/path3 #刪除路徑,指定路徑中有不為空的路徑。?法刪除
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 20 14:02 test.txt
rm 命令可以同時刪除?件或?錄
語法: rm [-f-i-r-v] [dirName/dir]
適?對象:所有使?者
功能:刪除?件或?錄
常?選項:
-f 即使?件屬性為只讀(即寫保護),亦直接刪除
-i 刪除前逐?詢問確認
-r 刪除?錄及其下所有?件
舉例:
[root@VM-8-16-centos repos]# ll
total 4
drwxr-xr-x 2 root root 4096 May 23 09:16 mydir
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
-rw-r--r-- 1 root root 0 May 23 09:15 test.txt #直接刪除普通文件
[root@VM-8-16-centos repos]# rm -rf test.txt
[root@VM-8-16-centos repos]# rm -rf mydir #遞歸刪除目錄文件
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
man 指令
Linux的命令有很多參數,我們不可能全記住,可以通過查看聯機?冊獲取幫助
語法: man [ 選項 ] 命令
常?選項
-k 根據關鍵字搜索聯機幫助
num 只在第num章節查找
-a 將所有章節的都顯?出來,?如 man printf 它缺省從第?章開始搜索,知道就停?,?a選
項,當按下q退出,他會繼續往后?搜索,直到所有章節都搜索完畢
解釋?下:man?冊分為9章(不同系統可能會有差別)
1 是普通的命令
2 是系統調?,如open,write之類的(通過這個,?少可以很?便的查到調?這個函數,需要加什么
頭?件)
3 是庫函數,如printf,fread4是特殊?件,也就是/dev下的各種設備?件
4 略
5 是指?件的格式,?如passwd, 就會說明這個?件中各個字段的含義
6 是給游戲留的,由各個游戲??定義
7 是附件還有?些變量,?如像environ這種全局變量在這?就有說明
8 是系統管理?的命令,這些命令只能由root使?,如ifconfig
9 略
舉例:
[root@VM-8-16-centos repos]# man ls
[root@VM-8-16-centos repos]# man rm
[root@VM-8-16-centos repos]# man mkdir
cp 指令
語法: cp [ 選項 ] 源?件或?錄 ?標?件或?錄
功能: 復制?件或?錄
說明:
cp指令?于復制?件或?錄
如同時指定兩個以上的?件或?錄,且最后的?的地是?個已經存在的?錄,則它會把前?指定
的所有?件或?錄復制到此?錄中
常?選項
-f 或 --force 強?復制?件或?錄, 不論?的?件或?錄是否已經存在
-i 或 --interactive 覆蓋?件之前先詢問??
-r 遞歸處理,將指定?錄下的?件與??錄?并處理。若源?件或?錄的形態,不屬于?錄或符
號鏈接,則?律視為普通?件處理
[root@VM-8-16-centos repos]# ll
total 0
-rw-r--r-- 1 root root 0 May 23 09:01 Newtest.txt
[root@VM-8-16-centos repos]# echo "hello world">Newtest.txt
[root@VM-8-16-centos repos]# cat Newtest.txt
hello world
[root@VM-8-16-centos repos]#
[root@VM-8-16-centos repos]# cp Newtest.txt tset.txt #復制文件
[root@VM-8-16-centos repos]# ll
total 8
-rw-r--r-- 1 root root 12 May 23 09:27 Newtest.txt
-rw-r--r-- 1 root root 12 May 23 09:28 tset.txt
[root@VM-8-16-centos repos]# cat tset.txt
hello world
[root@VM-8-16-centos repos]# mkdir mydir
[root@VM-8-16-centos repos]# cp *.txt mydir #復制多個文件到指定目錄下,*號是通配符
[root@VM-8-16-centos repos]# tree mydir
mydir
├── Newtest.txt
└── tset.txt
mv 指令
mv命令是move的縮寫,可以?來移動?件或者將?件改名(move (rename) files,經常?來備份?件或者?錄
語法: mv [ 選項 ] 源?件或?錄 ?標?件或?錄
功能:
1. 視mv命令中第?個參數類型的不同(是?標?件還是?標?錄),mv命令將?件重命名或將其
移??個新的?錄中。
2. 當第?個參數類型是?件時,mv命令完成?件重命名,此時,源?件只能有?個(也可以是源?錄名),它將所給的源?件或?錄重命名為給定的?標?件名。
3. 當第?個參數是已存在的?錄名稱時,源?件或?錄參數可以有多個,mv命令將各參數指定的源?件均移??標?錄中。
常?選項:
-f :force 強制的意思,如果?標?件已經存在,不會詢問?直接覆蓋
-i :若?標?件 (destination) 已經存在時,就會詢問是否覆蓋!
舉例:
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:34 mydir
-rw-r--r-- 1 root root 12 May 23 09:27 Newtest.txt
[root@VM-8-16-centos repos]# mv Newtest.txt test.txt #重命名文件
[root@VM-8-16-centos repos]# ll
total 8
drwxr-xr-x 2 root root 4096 May 23 09:34 mydir
-rw-r--r-- 1 root root 12 May 23 09:27 test.txt
[root@VM-8-16-centos repos]# mv test.txt mydir #移動文件到指定目錄下
[root@VM-8-16-centos repos]# tree mydir
mydir
└── test.txt
cat 指令
語法: cat [ 選項 ] [ ?件 ]
功能: 查看?標?件的內容
常?選項:
-b 對?空輸出?編號
-n 對輸出的所有?編號
-s 不輸出多?空?
舉例:


more 指令
語法: more [ 選項 ]
功能:more命令,功能類似 cat
常?選項:
-n 指定輸出?數
-q 退出more

less 指令
less ?具也是對?件或其它輸出進?分?顯?的?具,應該說是linux正統查看?件內容的?具,
功能極其強?
less 的?法?起 more 更加的有彈性,在 more 的時候,我們并沒有辦法向前?翻, 只能往后?
看但若使?了 less 時,就可以使? [pageup] [pagedown] 等按鍵的功能來往前往后翻看?件,更
容易?來查看?個?件的內容除此之外,在 less ?頭可以擁有更多的搜索功能,不?可以向下搜,也可以向上搜。
語法: less [ 參數 ] ?件
功能:less與more類似,但使?less可以隨意瀏覽?件,?more僅能向前移動,卻不能向后移動,?且less在查看之前不會加載整個?件。
選項:
-i 忽略搜索時的??寫
-N 顯?每?的?號
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n:重復前?個搜索(與 / 或 ? 有關)
N:反向重復前?個搜索(與 / 或 ? 有關)
q:quit
舉例:
less -N test.txt? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

grep 指令
語法: grep [ 選項 ] 搜尋字符串 ?件
功能:在?件中搜索字符串,將找到的?打印出來
常?選項:
-i :忽略??寫的不同,所以??寫視為相同
-n :順便輸出?號
-v :反向選擇,亦即顯?出沒有 '搜尋字符串' 內容的那??
舉例:
head 指令
head 與 tail 就像它的名字?樣的淺顯易懂,它是?來顯?開頭或結尾某個數量的?字區塊,head ?來顯?檔案的開頭?標準輸出中,? tail 想當然爾就是看檔案的結尾。
語法:
head [參數]... [?件]...
功能:
head ?來顯?檔案的開頭?標準輸出中,默認head命令打印其相應?件的開頭10?。
選項:
-n<?數> 顯?的?數

tail 指令
tail 命令從指定點開始將?件寫到標準輸出.使?tail命令的-f選項可以?便的查閱正在改變的?志?
件,tail -f filename會把filename?最尾部的內容顯?在屏幕上,并且不斷刷新,使你看到最新的?件內容.
語法: tail 必要參數 [ ?件 ]
功能:?于顯?指定?件末尾內容,不指定?件時,作為輸?信息進?處理。常?查看?志?件。
選項:
-f 循環讀取
-n<?數> 顯??數
date 指令
指定格式顯?時間: date +%Y:%m:%d
?法: date [OPTION]... [+FORMAT]
1. 在顯???,使?者可以設定欲顯?的格式,格式設定為?個加號后接數個標記,其中常?的標記
列表如下
%H : ?時(00..23)
%M : 分鐘(00..59)
%S : 秒(00..61)
%X : 相當于 %H:%M:%S
%d : ? (01..31)
%m : ?份 (01..12)
%Y : 完整年份 (0000..9999)
%F : 相當于 %Y-%m-%d
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. 時間戳
時間->時間戳:date +%s
時間戳->時間:date -d@1508749502
Unix時間戳(英?為Unix epoch, Unix time, POSIX time 或 Unix timestamp)是從1970年1?1
?(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒
舉例:
[root@VM-8-16-centos mydir]# date #獲取當前時間
Fri May 23 13:18:28 CST 2025
[root@VM-8-16-centos mydir]# date +%Y-%m-%d-%H:%M:%S #以特定格式獲取當前時間
2025-05-23-13:19:43
[root@VM-8-16-centos mydir]# date +%s #獲取當前時間戳
1747977614
[root@VM-8-16-centos mydir]# date -d @10000000 #獲取時間戳時間
Mon Apr 27 01:46:40 CST 1970
find 指令
Linux下find命令在?錄結構中搜索?件,并執?指定的操作。
Linux下find命令提供了相當多的查找條件,功能很強?。由于find具有強?的功能,所以它的選
項也很多,其中?部分選項都值得我們花時間來了解?下。
即使系統中含有?絡?件系統( NFS),find命令在該?件系統中同樣有效,只要你具有相應的權
限。
在運??個?常消耗資源的find命令時,很多?都傾向于把它放在后臺執?,因為遍歷?個?的
?件系統可能會花費很?的時間(這?是指30G字節以上的?件系統)。
語法: find pathname -options
功能:?于在?件樹中查找?件,并作出相應的處理(可能訪問磁盤)
常?選項
-name 按照?件名查找?件

which 指令
功能:搜索系統指定的命令

alias 指令
功能:設置命令的別名
