Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Linux_Command-line_指令與基本操作.pdf
Search
Alex
February 06, 2019
0
73
Linux_Command-line_指令與基本操作.pdf
Alex
February 06, 2019
Tweet
Share
More Decks by Alex
See All by Alex
How to integrate GitLab CICD into B2B service.pdf
alexsu
0
200
Jenkins Quick Guide
alexsu
0
89
Vim 基本操作
alexsu
1
91
Top IDEs & Code Editors for Data Science
alexsu
0
34
Shell scripting 基本教學
alexsu
0
32
Introduction to Git for Data Science
alexsu
0
230
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
21
6.4k
Into the Great Unknown - MozCon
thekraken
14
1k
Side Projects
sachag
451
41k
Being A Developer After 40
akosma
66
580k
Agile that works and the tools we love
rasmusluckow
325
20k
Code Review Best Practice
trishagee
56
15k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
46k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
21
1.4k
GraphQLの誤解/rethinking-graphql
sonatard
55
9.3k
Six Lessons from altMBA
skipperchong
22
3k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
Transcript
蘇民弦 (Alex) Linux Command-line 基本指令與操作
Why UN*X • 免費 • 很多軟體都是基於 Linux 系統開發 • 架伺服器、嵌入式系統
• 自動化排程容易 • UTF-8 編碼 • 很多發行版 • 高度客製化 • 多工、多使用者
各式各樣的 Linux 發行版
在安全的環境操作 - 使用虛擬機器 (VMs) VirtualBox Vagrant Docker • 人性化介面 •
中文教學多 • 不管新手老手都很適合 • 基於 VirtualBox • 使用命令列操作 • 輕量 (相較於傳統 VM) • 容量小 • 應用程式打包帶著走 推薦!
系統執行的必要 程式、函式庫 Linux 的檔案系統階層標準 (FHS) /bin /sbin /lib(64) /etc /opt
/usr /boot /home /root /srv /dev /proc /sys /run /var /tmp /mnt /media / 配置檔、第三 方軟體、系統 相關軟體 開機相關檔案、家 /根目錄 www, 硬體裝置檔案、與 kernal 相關的檔案 變數、記錄檔、暫存 檔、掛載的磁碟目錄
Kernal Shell Hardware Shell User • 與電腦的核心 (kernal) 溝通。 •
在沒有桌面系統 (X window) 幫忙的時候, 只能依靠 shell 操作電腦。 • 可以加快工作效率,早點下班。 • 三種常使用的 shell:Bash、Zsh、Fish。
Terminal (終端機) • 操作 Shell 的介面。 • Linux 系統可以使用 gnome-terminal,
Terminator。 • Mac 系統推薦 iTerm2。
查詢指令的四種方式 $ man [指令] # 在終端機查詢 Dash Zeal Google 推薦!
下載範例資料 https://github.com/siansiansu/example-data
$ 指令名稱 + 參數 + [目標對象] # 備註 Basic Command
line
小試身手 在終端機查詢日期時間
查詢日期時間 $ date # 顯示當前日期 $ date +"%Y-%m-%d" # format
日期 $ cal # 顯示月曆 $ cal -m 4 # 顯示四月份的月曆 $ cal -B3 -A3 # 顯示前後三個月的月曆 $ cal 2020 # 顯示 2020 的月曆 $ ncal # 垂直顯示月曆
檔案目錄操作 - 切換目錄、查看 $ ls # 列出檔案目錄 $ pwd #
列出目前路徑 $ cd /Desktop # 移動到桌面 $ cd .. # 回到上一層目錄 $ cat Birdname.txt # 查看 Birdname.txt 的文字內容 $ less iris.csv # 查看 Birdname.txt 的文字內容 $ echo "Hello World!" # 印出 "Hello World!"
檔案目錄操作 - 建立、刪除 $ mkdir ebird_data # 建立 ebird_data 目錄
$ touch hello.r # 建立 hello.r 空檔案 $ rm hello.r # 刪除 hello.r 檔案 $ rm -rf ./Desktop # 刪除桌面所有的資料夾
檔案目錄操作 - 複製、移動 $ cp Birdname.txt /Desktop # 複製檔案到桌面 $
scp Birdname.txt 35.231.64.131:/home # 複製檔案到 35.231.64.131 主機的 home $ mv iris.csv /Desktop/iris.csv # 移動檔案到桌面
檔案打包 / 壓縮 $ tar -czvf iris.tar iris.csv # c:
打包, z: gzip, v: 顯示過程, f: 指定檔案名稱 $ tar -xzvf iris.tar # x: 解壓縮, z: gzip, v: 顯示過程, f: 指定檔案名稱 $ 7z a iris.7z iris.csv # 壓縮 ( 需要先安裝 7z ) $ 7z x iris.7z # 解壓縮 $ zip -r iris.zip iris.csv # 壓縮 $ unzip iris.zip # 解壓縮
檔案搜尋 $ find . -iname "*.txt" # 尋找副檔名為 .txt 檔案
$ find . -iname "code" -type d # 尋找 code 目錄 $ find . -type f -name "*.java" # 尋找副檔名為 .java $ find . -perm 0644 ! -path "./.git/*" # 尋找權限為 644 的檔案,並且忽略 git 目錄 $ find . -size +1M -type f # 尋找大小超過 1M 的檔案 $ find . -atime -3 -type f # 尋找 3 天內修改過的檔案 $ find . -iname "code.*" -exec grep -H "Solution" {} \; # 尋找含有 Solution 字串的檔案
檔案中的文字搜索 $ grep "Gull" Birdname.txt # 顯示 Birdname.txt 中,含有 Gull
字串的那幾行 $ grep -E '^"9.*XX' Birdname.txt --color # 顯示 Birdname.txt 中,含有 regex 規則的那幾行 $ grep -rl "Gull" . --exclude Activ.txt # 搜尋含有 Gull 字串的檔案,但排除 Activ.txt
計算行數 $ wc Birdname.txt # 顯示行數、字數、字元數 $ wc -l Birdname.txt
# 顯示行數 $ wc -w Birdname.txt # 顯示字數 $ wc -m Birdname.txt # 顯示字元數 $ wc -c Birdname.txt # 顯示位元組數
指令的串接 $ echo
[email protected]
| grep "minsiansu" --color # 搜尋字串
$ top | grep iTerm2 # 列出 iTerm2 程式的進程 $ last | grep Wed # 列出禮拜三的登入者 $ head Birdname.txt | grep "Gull" --color # 顯示前 10 行,含有 Gull 的字串 $ man ps | grep "Sort" --color # 列出 ps 指令中含有 Sort 字串的說明
指令的串接 # 第一個命令正確被執行後,才可以執行第二個命令 $ echo "
[email protected]
" && echo "hello world!"
# 第一個命令如果發生錯誤,則第二個命令不被執行 $ ls tmp || echo "You are Fired!"
I/O Redirection $ echo “Hello World!” > tmp.txt # 輸出字串
“Hello World!” 至 tmp.txt 檔案 $ echo “Hello World!” >> tmp.txt # 輸出字串 “Hello World!” 附加至 tmp.txt 檔案最末行 $ ls > tmp.txt # 將指令 ls 的內容輸出至 tmp.txt $ ls non_exist > output.txt 2>&1 # 將 ls 指令的錯誤訊息導向至 output.txt $ cat < tmp.txt # 將 tmp.txt 的內容導向至 cat 指令輸出 $ ps aux | grep "iTerm2" # 搜尋 “iTerm2” 程式是否有在執行 STDIN: 0; STDOUT: 1; STDERR: 2
檔案的字串處理 (進階)
字串處理 - sed $ sed -e "1, 10d" Birdname.txt #
刪除並顯示 Birdname.txt 前十行 $ sed -e "s/台北/天龍國/" City.txt # 將「台北」全部取代成「天龍國」 $ sed -n '/台北/ p' City.txt # 印出含有「台北」的那幾行 $ sed -e '/台北/a 這一行是新增的' City.txt # 在含有「台北」這行的下一行加上「 這一行是新增的」 $ sed -e '/台北/i 這一行是新增的' City.txt # 在含有「台北」這行的上一行加上「 這一行是新增的」 $ sed -e '/台北/d' City.txt # 刪除含有 Setosa 字串的那一行
$ awk 'BEGIN { FS = "," } ; {
print $5 }' iris.csv # 取出第五欄 $ cat iris.csv | awk 'BEGIN { FS = "," } ; { print $5 }'| sort | uniq # 取出不重複的字串 $ awk -F, ' $1 >= 7 { print } ' iris.csv # 取出第一欄位中大於 7 的數字 $ awk '/Setosa/ { print } ' iris.csv # 取出含有 Setosa 的那幾欄 $ awk -F, ' { print NF; exit } ' iris.csv # 統計有幾欄 字串處理 - awk
系統管理 $ top # 查看目前運行的程式 $ du -sh # 查看目錄檔案大小
$ df -h # 硬碟容量 $ sudo [command] # 使用最高權限操作接下來的指令 $ su # 使用 root 身份操作 $ kill [ Job_ID ] # 停止某個程式 $ diskutil list # 查看磁碟分區狀態 (Mac) $ lsblk # 查看磁碟分區狀態 (Linux)
檔案權限操作 -rwxrwxr-- 1 2 3 4 5 6 7 8
910 位置 1: 類型:"-"為檔案、"d" 為目錄。 位置 234: 檔案擁有者權限:rwx (可讀寫執行) 位置 567: 群組權限:rwx (可讀寫執行) 位置 8910: 其他使用者權限:r-- (只可讀取) # rwx分別使用數字表示為 4 + 2 + 1 = 7 $ chmod 777 tmp.md # tmp.md 檔案的權限全開 $ chmod 644 tmp.md # 其他人只允許讀取檔案
網路連線操作 $ ping www.google.com # 測試 www.google.com 是否有回應 $ traceroute
www.google.com # 測試中間經過的節點 $ nslookup www.google.com # 顯示 ip, domain 位址 $ nmap www.google.com # 查看 port
將工作放到背景執行 $ jobs -l # 列出目前在執行的工作 $ jobs -p #
列出目前在執行的工作的 ID $ python example-jobs.py & # 將 Python Script 丟到背景執行 $ sleep 100 & $ fg %1 # 將工作 1 號帶到前景執行 $ bg %1 # 在背景執行工作 1 號的命令
例行性工作排程 * * * * * echo “hello” 分 時
日 月 週 要執行的指令 $ crontab -e # 進入排程編輯頁面。 $ crontab -l # 查詢目前排程。
寄信 $ mail -s "Hello Alex"
[email protected]
# 寄空白信 $
echo "You are fired!" |mail -s "Hello Alex"
[email protected]
# 寄送含內容的信 $ mail -s "Hello Alex"
[email protected]
< file # 將信件內容先寫好至 file
套件管理工具 • Homebrew ---------------- MacOS / Linux • apt-get ---------------------
Ubuntu / Debian • yum ------------------------- CentOS / Fedora • DNF ------------------------ CentOS / Fedora • pacman -------------------- Arch Linux • AUR ------------------------ Arch Linux • Snapcraft ------------------ for all Linux
常用終端機小工具介紹 $ brew install [ package ] # for Mac
$ apt-get install [ package ] # for Ubuntu / Debian 搭配套件管理器安裝
列印系統資訊 - neofetch
檔案搜尋- ranger
系統監控 - htop
系統監控 - gotop
顯示檔案目錄結構- tree
顯示語法高亮的文件內容 (取代cat) - bat
Youtube 下載器 - you-get
檔案模糊查找 - fzf
檔案搜尋 (取代find) - fd
使用 SQL 語法處理文字檔 - q
在終端機產生文字框 - boxes
練習 • 練習敲打 Command-line 指令 • 練習查看文檔,使用別的參數試試看 • 學習使用套件管理器下載小工具 •
客製化自己的終端機 • 寫一封在自己生日當天,會自動寄「祝我生日快樂」的信給自己
Reference • 鳥哥的 Linux 私房菜 • Linux 技術手札 • My
Command-line tools