Shell是一種命令直譯器
<- 最常用的,對sh向下兼容
<- POSIX standard
終端 (介面)
-> Shell
-> Kernel執行指令
終端 (介面)
-> Shell
- 內部指令
-> Sys call
- 外部指令
-> Kernel 載入/執行程式
-> 外部程式Sys call
內部指令 (Shell提供)
外部指令 (獨立的可執行檔案)
/bin/ls
/bin/cp
/usr/bin/grep
/usr/bin/find
基本上有bash能用就行
帳號: student
密碼: verystrongpassword
ssh student@<ip>
輸入密碼時不會顯示
帳號: student
密碼: verystrongpassword
command [options] [arguments]
- 或 -- 開頭
ls -l /home/andy
tar -c -v -z -f archive.tar.gz
tar -cvzf archive.tar.gz
mv volumes /var/lib/docker/
cat myfile
help 顯示說明
verbose 顯示詳細訊息
recursive 遞迴操作 (目錄)
force 強制執行
all 顯示全部
time 時間
long 長格式
an interface to the system reference manuals
使用手冊
看到什麼不會的指令,打
man <command>
就對了 !
檔案系統以 / 為根目錄
是一個樹狀結構
/home/andy/Documents/report.txt
/etc/nginx/nginx.conf
/var/log/syslog
../../etc/nginx/nginx.conf
./Documents
Documents
假設你人在 /home/student/
/etc/nginx/nginx.conf
/home/student/Documents
/home/student/Documents
~ 代表目前使用者的家目錄
通常會位於 /home/<user>
Ctrl + C
Ctrl + D
發送SIGINT (中斷訊號) 給進程
告訴Shell: 輸入結束
輸出文字
display a line of text
echo <string>...
註: <string>...可以是字串
清空終端
clear the terminal screen
execute a command as another user
以root或其他用戶的身份執行命令
sudo <command>
print name of current/working directory
輸出工作目錄
pwd
列出資料夾內容
list directory contents
ls
列出工作目錄所有內容
ls <dir>
列出<dir>內的所有內容
ls -l [dir]
-l 使用long list格式
ls -a [dir]
-a 顯示所有檔案 (包含. 與 ..)
ls -la [dir]
long list 顯示所有檔案
change directory
切換工作目錄
cd <dir>
切換工作目錄
更新檔案時間 / 建立檔案
change file timestamps
建立檔案
註: 可以一次多個
touch <file>...
更新檔案時間
(Access/Modify)
touch <file>...
-a 更新檔案時間
(Access)
touch -a <file>...
-m 更新檔案時間
(Modify)
touch -m <file>...
cat head tail
串接 / 輸出檔案到stdout
concatenate files and print on the standard output
輸出
cat <file>...
修改(建立) 檔案內容
註: 可以一次多個
註: Ctrl + D 傳送EOF
cat > <file>
輸出檔案前部分
output the first part of files
輸出前10行
head <file>...
輸出前幾行
head -n <lines> <file>...
輸出檔案後部分
output the last part of files
輸出末10行
tail <file>...
輸出末幾行
tail -n <lines> <file>...
同步輸出檔案末尾
tail -f <file>...
(可以用於查看log file)
/var/log/kern.log
/var/log/syslog
mkdir rmdir
建立目錄
make directories
建立資料夾
註: <dir>需要在已存在的目錄下
mkdir <dir>...
-p 必要時會自動建立parent dir
mkdir -p <dir>...
刪除"空"目錄
remove empty directories
刪除空目錄
rmdir <dir>...
-p 一路刪除parent dir
rmdir -p <dir>...
這東西多少有點沒用
rm mv cp
刪除檔案 or 目錄
remove files or directories
刪除檔案
rm <file>...
-r 刪除,遞迴 (多用於資料夾)
rm -r <file>...
-f 刪除,強制
rm -f <file>...
-rf 刪除,強制,遞迴
rm -rf <file>...
移動 (重新命名) 檔案
move (rename) files
移動
<destPath>不存在: 移動到<destPath>
註: <path2>需要在已存在的目錄下
mv <srcPath>... <destPath>
<destPath>是目錄: 移動到<destPath>內
移動
mv <srcPath>... <destPath>
移動
<destPath>是檔案: 覆蓋檔案(<srcPath>得是檔案)
mv <srcPath>... <destPath>
複製檔案 and 目錄
copy files and directories
複製
註: <path2>需要在已存在的目錄下
<destPath>不存在: 複製成<destPath>
cp <srcPath>... <destPath>
(-r 複製資料夾)
<destPath>是目錄: 複製到<destPath>內
複製
cp <srcPath>... <destPath>
(-r 複製資料夾)
註: 如果srcPath以/結尾,會把srcPath內的內容複製進destPath內
複製
<destPath>是檔案: 覆蓋檔案(<srcPath>得是檔案)
(-r 複製資料夾)
cp <srcPath>... <destPath>
前面看到參數有...的,代表可以有多項。
那他們通常也支援 通配符
ls /home/andy/*
ls /usr/bin/python*
代表/home/andy/內所有檔案
代表/usr/bin/內所有python開頭的檔案
2. 建立CRYCHIC目錄
1. 建立五個檔案:
mkdir
touch
3. 將前面那些檔案放進CRYCHIC目錄
mv
1. 把Saki移出去CRYCHIC
2. 把CRYCHIC內所有檔案移出去
3. 刪除CRYCHIC
mv
mv
rm / rmdir
1. 建立MyGO!!!!!目錄
2. 在MyGO!!!!!內建立Anon
mkdir
touch
mv
3. 將Soyo、Taki、Tomorin移進MyGO!!!!
4. 在MyGO!!!!!內建立Raana
touch
將前一個指令的輸出
給下一個指令當輸入
command1 | command2
範例
ls -1 | wc -l
把ls -1輸出結果
拿去wc -l輸入
指令的輸出 寫入 檔案
command > file
指令的輸出 追加寫入 檔案
command >> file
指令的輸入 用 檔案內容
command < file
指令的輸出 寫入 檔案
command > file
echo "Hello World" > hello.txt
sort < file.txt
指令的輸入 用 檔案內容
command < file
指令的輸出 追加寫入 檔案
command >> file
echo "Hello World" > hello.txt
echo "Hello World" >> hello.txt
> / >>
2> / 2>>
&> / &>>
關於stdout / stderr
stdout
stderr
stdout + stderr (我都要)
簡單講,就是把指令/邏輯 寫成檔案。
file extension 是 sh
執行:
(指定使用/bin/bash)
(指定使用/bin/sh)
如果沒有x權限,請 chmod +x <file>
echo "Hello World"file: test.sh
如果沒有x權限,請 chmod +x <file>
#!
首行可以宣告,告訴系統要用哪個解譯器
#!/bin/bash#!/bin/shbash
sh
#!/bin/bash
# 變數
NAME="Andy"
COUNT=3
# 函數
greet() {
echo "Hello, $1!"
}
# 條件判斷
if [ $COUNT -gt 0 ]; then
echo "Count is greater than 0"
else
echo "Count is 0 or less"
fi
# 迴圈
for i in $(seq 1 $COUNT); do
greet $NAME
done
# 讀取使用者輸入
read -p "Enter your favorite color: " COLOR
echo "Your favorite color is $COLOR"
# 檢查檔案是否存在
FILE="test.txt"
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist."
fi變數、條件判斷、迴圈、函數
這些都能用。
vim <file>
任何模式 按 ESC 都會來這個模式
h / j / k / l
0 / $
^
w / b
e
gg / G
左 / 下 / 上 / 右 (方向鍵也行)
移到行首
移到非空白行首
跳到 下 / 上 一個單字頭
跳到單詞尾
移到文件頭 / 尾
普通模式: 移動
x X
dd
dw
d$ (D)
yy
y$
p P
刪除光標 下/前 字元
刪除整行
刪除光標 到 下一個單詞頭
刪除光標 到 行尾
複製整行
複製光標 到 行尾
貼在光標 後 / 前
普通模式: 刪/複製/貼上
u
Ctrl + R
Undo
Redo
普通模式: undo / redo
/文字 ?文字
n N
f<char>
F<char>
t<char>
T<char>
向 下 / 上 搜尋文字
搜尋 下 / 上 一個match
跳到當前行 該字元位置
向左 跳到該字元位置
跳到 該字元 前一個位置
向左 跳到 該字元 後一個位置
普通模式: search / jump
i
:
a
o
O
輸入模式
底線指令模式
插入模式
下方插入一行,進入輸入模式
上方插入一行,進入輸入模式
普通模式: 進入其他模式
(還有非常多指令...)
就很正常的編輯模式,方向鍵移動
w
wa
q
qa
wq
wqa
Write File
Write All Files
Quit
Quit All Files
Write and Quit
Write and Quit All
加一個!可以強制
top
kill
df
實時查看系統運行狀態
向進程發送信號
查看磁碟分區使用情況
ping
curl
wget
ssh
nslookup
發送 ICMP Request,監測往返時間
傳送請求 (支援多種協議)
非互動式下載工具
使用 SSH 協議加密連線
正反解析Domain