常見Linux命令行指令

cd

cd # 回到跟目錄
cd .. # 回到上一頁
cd ~ # 回到家目錄
cd - # 回到cd前一個目錄

ls & pwd

ls # 列出目錄下文件
ls -a # 列出所有文件包含隱藏.file文件
ls -l # 列出目錄下文件(條列狀)
ls -al # 列出所有文件包含隱藏.file文件(條列狀)

pwd # 顯示當前目錄位置

mkdir

mkdir tmp # 創建tmp目錄
mkdir -p a/b/c/ # 創建a目錄其中a目錄包含b目錄,而b目錄又包含c目錄

rmdir

rmdir tmp # 刪除空目錄
rm -d tmp # 刪除空目錄
rm -r tmp # 刪除tmp目錄以及其下所有檔案

創建文件

touch tmp.txt # 創建txt文件
vim tmp.txt # 創建tmp文件並且開啟vim編輯

cp

cp tmp.txt /home/zach/tmp/files # 複製tmp檔到特定路徑
cp tmp2 -r /home/zach/tmp/files # 複製tmp目錄到其他路徑下

mv

mv hello.c test.c # 把hello改名為test
mv hello.c /home/zach/tmp/files # 把hello檔移動到指定目錄下

cat

cat hello.c # 查看hello.c內容
cat -n hello.c # 查看hello.c內容(含行號)

more

# more與cat指令類似,其為一種類似vi的文本編輯器,以全終端全螢幕顯示文本內容,並具有快捷鍵
more hello.c # 查看hello.c內容

= # 顯示行號
space # 下一頁
enter # 向下一行
ctrl + f # 向下滾動頁面
ctrl + b # 向前滾動頁面
q # 離開
:f # 輸出文件名與當前行號

less

# 與more指令類似,不過less更適用於查看大型檔案內容
less hello.c

page down # 向下滾動翻頁
page up # 向上滾動翻頁
/ string # 向下查詢字串  -->n/N 向下/向上查詢
? string # 向上查詢字串  -->N/n 向上/向下查詢

echo

echo $PATH # 輸出系統環境變量
echo "hello" # 輸出hello到終端機上
head hello.c # 輸出檔案前10行
head -5 hello.c # 輸出檔案前5行

tail

tail hello.c # 輸出檔案後10行
tail -5 hello.c # 輸出檔案後5行
tail -f hello.c # 實時監控檔案更新輸出

輸出重定向指令

ls -al > test.txt # 將指令輸出結果寫入test.txt中(覆寫)
ls -al >> test.txt # 將指令輸出結果寫入test.txt中(從尾端開始)

cat test.txt > test2.txt # 將test內容覆寫到test2中
cat test.txt >> test2.txt # 將test內容新增到test2尾部

echo "hello" > test.txt # 將hello寫入到test.txt中
echo "hello" >> test.txt # 將hello添加到test.txt中 

ln

# 類似新增快捷鍵
ln -s /home/zach/tmp t2 # t2是一個指向/home/zach/tmp的快捷地址

history

history  # 查看所有指令的歷史紀錄
history 6 # 查看最新的6條指令
!647 # 執行編號為647的指令
history | less or more # 執行history並用lsee/more開啟

date

date # 顯示時間資訊
date +%Y # 年
date +%m # 月
date +%d # 日
date "+%Y %m %d %H %M %S" # 詳細時間訊息

cal

cal # 顯示日曆
cal 2021 # 顯示2021日曆

find

find [地址] [查詢方式] "查詢對象"

find /home/zach -name "hello.c" # 在特定目錄下案檔名搜尋
find / -user "zach" # 在特定目錄下搜尋屬於zach用戶的所有檔案
find /home -size +200M # 在特定目錄下搜尋大於200M的所有檔案(+:大於 -:小於 單位:G M k)

grep

# 查詢檔案中的指定字串,通常與 | 一起使用
cat hello.c | grep -n line # 在hello.c中查詢"line"並顯示行號
cat hello.c | grep -ni line # 在hello.c中查詢"line"並顯示行號(忽略大小寫限制)

|

# 將左側指令當作輸入輸出給右側指令
cat hello.c | grep -ni line | grep -n Line 

gzip & gunzip

# 將單一檔案壓縮或解壓
gzip hello.c # 將hello.c加入壓縮檔
gzip -r a # 將檔案a中的每一個檔案都壓縮
gunzip hello.c.gz # 將hello.c.gz解壓

zip & unzip

zip mypackage a # 將a壓縮並將壓縮檔命名為mypackage
unzip mypackage.zip # 解壓縮mypackage
unzip -d /home/zach mypackage.zip  # 解壓縮mypackage到特定目錄

tar

# 解壓與壓縮指令
tar -[czv][f] a.tar.gz a1.txt a2.txt # 將a1,a2加到壓縮檔
tar -[czv][f] home.tar.gz /home # 將home目錄下所有文件加到壓縮檔
# -c 產生tar打包文件
# -z 打包同時壓縮
# -v 顯示訊息
# [czv]內的順序可以自由互換
# -f 重新命名壓縮檔 一定要放在最後
tar -[xzv][f] a.tar.gz # 將a1,a2解壓縮
tar -[xzv][f] a.tar.gz -C /home/zach/tmp # 將a.tar.gz解壓縮結果放到tmp目錄下

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

%d 位部落客按了讚: