31
Command Line 初級寶典 Tom Chen @ IF+ TechArt

Command line 初級寶典

Embed Size (px)

DESCRIPTION

Command line 初級寶典 關於 Linux 常用的 command line commands 以及常用的工具如 vim & screen 的介紹

Citation preview

Page 1: Command line 初級寶典

Command Line 初級寶典Tom Chen @ IF+ TechArt

Page 2: Command line 初級寶典

http://zh.wikipedia.org/wiki/OS_X

Page 3: Command line 初級寶典

• cd (change directory)

• cd ~/Desktop

• passwd (change password)

• ls (list)

• ls -alh | more

• pwd (print working directory)

• w

• which [command] (find where the command is)

• man [command] (Manual)

• man ls

Page 4: Command line 初級寶典

• Ctrl-E 到最後⼀一個字

• Ctrl-A 到最前⼀一個字

• Ctrl-U 從游標所在位置消滅到最前⾯面

• Ctrl-D 中斷連線、終⽌止程式... 等

• (箭頭上) 上⼀一個指令

• (建投下) 下⼀一個指令

• ![command partial] 上⼀一個指令

• 例: $ telnet google.com.tw 80

• $ !telnet

• 通常 Ctrl 會簡寫為 ^。例如 Ctrl-E —> ^E

Page 5: Command line 初級寶典

檔案系統• mv file1 file2

• cp file1 file2

• cp -r dir1 dir2 (cp -a dir1 dir2)

• rm file1

• rm -r dir1 ⾮非常⾮非常⾮非常的危險!!!

• rm -rf dir1 ⾮非常⾮非常⾮非常的危險!!!

• ln -s file1 file2 (製作連結檔)

Page 6: Command line 初級寶典

檔案系統

• chmod 777 test.py

• chmod u+x test.py

• http://zh.wikipedia.org/wiki/Chmod

Page 7: Command line 初級寶典

• top 列出⺫⽬目前最⾼高的幾個 process

• uptime 開機多久 (也會列出 CPU loading)

• ps aux 列出⺫⽬目前所有 process

• ps ax | grep vi

• kill [pid] 把這個 process 殺死

• kill -9 [pid] 最暴⼒力

Page 8: Command line 初級寶典

• 找檔案內的字串

• grep midi *

• grep -r midi *

• 找檔名

• find . | grep meetup

Page 9: Command line 初級寶典

• more [filename] 顯⽰示檔案

• less [filename] 顯⽰示檔案

• cat [filename] 顯⽰示檔案 (⼀一股腦兒地)

• head [filename] 顯⽰示檔案的前幾⾏行

• tail [filename] 顯⽰示檔案的最後幾⾏行

• tail -F [filename] 會⾃自動更新 (看 log 檔很有⽤用)

• touch [filename] 產⽣生⼀一個空的檔案

Page 10: Command line 初級寶典

pipe / redirect• command1 | command2

• command1 的 output 導進 command2 的 input

• cat abc.txt | more

• cat file.txt > file2.txt 貼到 file2.txt

• echo ‘1234’ > file.txt

• cat file.txt >> file2.txt 貼在 file2.txt 後⾯面

Page 11: Command line 初級寶典

• diff file1.txt file2.txt

• diff -r dir1 dir2

Page 12: Command line 初級寶典

• df -h 看⺫⽬目前磁碟狀況 (空間)

• du 列出⺫⽬目前檔案結構的空間

• du -h -d 0 列出⺫⽬目前這個⺫⽬目錄底下佔了多少空間

• du -h -d 1 ⼀一樣意思但是... 你打打看就知了

• 參數 -h 有 human readable 的意思, 例如 16384B —> 16KB

Page 13: Command line 初級寶典

• env 列出環境變數

• env | grep python

• sudo [command] 以 root 執⾏行 command

• sudo su - 變⾝身為 root

• sudo 與 su 的不同

• sudo 打的是⾃自⼰己的密碼, 使⽤用者要列在 /etc/sudoers 才⾏行

• su 打的是 root 的密碼

• 共⽤用密碼其實很危險, 不推薦直接使⽤用 su

Page 14: Command line 初級寶典

網路相關• ifconfig 網卡的相關資訊 (interface config)

• nslookup 查 DNS

• nslookup google.com.tw

• nslookup 60.199.175.31

• dig google.com.tw ⼀一樣是查 DNS

Page 15: Command line 初級寶典

網路相關• telnet ptt.cc

• telnet google.com 80

• GET / HTTP/1.1

• ssh xxx.com

• 暗⿊黑⼤大法ssh xxx.com -D8080

Page 16: Command line 初級寶典

網路相關

• wget URL

• wget http://www.google.com.tw/

• curl URL

• https://gist.github.com/caspyin/2288960 (some tutorial)

Page 17: Command line 初級寶典

網路相關

• scp [email protected]:~/test.py ./

• scp ./test.py [email protected]:~/

Page 18: Command line 初級寶典

• gzip test.txt

• gunzip test.txt.gz

• tar zcvf xxx.tgz asdf asdf asdf

• tar zxvf xxx.tgz

• xxx.tar.gz, xxx.tgz

Page 19: Command line 初級寶典

screen ⼤大法• 先到 https://github.com/yychen/settings/blob/master/screenrc 把內容貼到 ~/.screenrc

• $ screen

• Ctrl-A c 製造新的視窗

• Ctrl-A [spcae] 跳到下⼀一個視窗

• Ctrl-A [backspace] 跳到前⼀一個視窗

• Ctrl-A ## 跳到那⼀一個號碼的視窗

• Ctrl-A d 跳離 (但是東⻄西都還在)

• Ctrl-A k 把視窗殺死

• $ screen -r -d 列出有哪些 session

• $ screen -r -d [PID]

Page 20: Command line 初級寶典

screen ⼤大法• Ctrl-A ESC (或 Ctrl-A [) 凍結畫⾯面可以上下卷

• q 跳回

• h, j, k, l —> 左, 下, 上, 右

• Ctrl-B 上⼀一⾴頁

• Ctrl-F 下⼀一⾴頁

• $ 游標到這⾏行的最後⼀一個字元

• | 游標到這⾏行的第⼀一個字元

Page 21: Command line 初級寶典

神編輯器 vi (vim)• $ vi

• .vimrc

• https://gist.github.com/yychen/5848216

• 威猛神指南

• http://blog.vgod.tw/2009/12/08/vim-cheat-sheet-for-programmers/

• http://c9s.blogspot.tw/2009/08/vim-hacks-coscup.html

Page 22: Command line 初級寶典

神編輯器 vi (vim)• 指令模式 (移動)

• h, j, k, l —> 左, 下, 上, 右

• ^ 這⼀一⾏行⾮非空⽩白的⾏行頭

• | 這⼀一⾏行的第⼀一個字元

• $ ⾏行尾

• :1 第⼀一⾏行 (:## 第幾⾏行)

• G 最後⼀一⾏行

• Ctrl-F 下⼀一⾴頁

• Ctrl-B 上⼀一⾴頁

• w 下⼀一個詞

• b 上⼀一個詞

是否看起來很眼熟? j, k 為下、上普遍使⽤用

ptt (BBS), Facebook 都是使⽤用這些熱鍵 !

^ 以及 $ 也會在正規表⽰示式 (regular expression) 看到

Page 23: Command line 初級寶典

神編輯器 vi (vim)

• 指令模式 (搜尋)

• / 搜尋

• n 下⼀一個

• N 上⼀一個

• ? 反向搜尋

很多程式搜尋的快捷建都是 / 例如: ptt, Firefox

Page 24: Command line 初級寶典

神編輯器 vi (vim)• 指令模式 (插⼊入 INSERT MODE)

• i 插⼊入

• a 從下⼀一個字元插⼊入

• I 從⾏行頭插⼊入

• o 從下⼀一⾏行插⼊入

• O 從上⼀一⾏行插⼊入

• 插⼊入模式進⼊入後按 ESC 退出

Page 25: Command line 初級寶典

神編輯器 vi (vim)• 指令模式 (刪除)

• dw 刪除這個字

• dd 刪除這⼀一⾏行

• 10dd 刪除⼗十⾏行

• dG 刪除到整個檔案的最後

• d:1 刪除到整個檔案的第⼀一⾏行

• d$ 從游標刪除到⾏行尾

• x 刪除這個字元

• 10x 刪除⼗十個字元

Page 26: Command line 初級寶典

神編輯器 vi (vim)

• 指令模式 (剪貼)

• u 回復

• p 貼上

• yy 把這⼀一⾏行貼到 vi 的剪貼簿

Page 27: Command line 初級寶典

神編輯器 vi (vim)• 指令模式 (圈選模式 VISUAL MODE)

• V 進⼊入圈選模式 (整⾏行)

• Ctrl-V 進⼊入圈選模式 (區塊)

• 圈選後

• y 把圈選的區域 copy 到剪貼簿

• x 刪除

• d 刪除

Page 28: Command line 初級寶典

神編輯器 vi (vim)• 指令模式

• :x (:wq) 存檔並結束

• :w 存檔

• :w [filename] 存成另外⼀一個檔案

• :q 跳出

• :q! 不存檔跳出

• :edit 重新載⼊入

• :![shell command] 執⾏行 shell 指令

Page 29: Command line 初級寶典

看起來超強的⽅方式

1. ⽤用 screen (或 tmux), 下⾯面⼀一定要有⼀一條

2. irssi

3. 會⽤用 vim

4. 會⽤用 python or ruby

Page 30: Command line 初級寶典

irssi

• IRC client

• $ irssi -c irc.freenode.net

• /join python.tw

Page 31: Command line 初級寶典

不⼩小⼼心按到的鍵• 不⼩小⼼心按到 Ctrl-S (只有 Linux 才會這樣)

• 你的電腦會完全的 hang 住

• 按下 Ctrl-Q 解開

• 不⼩小⼼心按到 Ctrl-Z

• 你的程式跑到背景了

• $ fg 或

• $ %