Which
which <program> # 查program實際位置
# which date / which echo
File/Folder
touch filename
touch test-{a, b, c}.txt # test-a.txt test-b.txt test-c.txt
mdkir foldername
du
du -sh foldername/filename
Move
cd - : go to previous directory
cd / cd ~ : go to home directory
Type
C-u: delete all lineC-w: delete word
Search 搜尋
- ctrl + r
type keyword to search history command !! <Enter>
grep -r "keyword" .
brew install ripgrep
rg <word>
在多層目錄中找到filename.txt的路徑
find /usr/bin -type f -name "filename"
find ~/Downloads -type f -mtime +30 # 30天以上的file
find ~/Downloads -type f -size +100M -exec ls -lh {} \;
find . -name "*.md" -exec grep -l "TODO" {} \;
Awk
# cat data
# a b c
# d e f
# g h i
awk '{print $2}' data
# b
# e
# h
awk -F , '{print $2}' data # 用','區分
Kill process running port
lsof -i:3000
kill <PID>
目錄大小
du -sh <path>
For Loop
for varname in a b c d; do echo "$varname"; done
# a
# b
# c
# d
for varname in $(seq 1 5); do echo "$varname"; done
# 1
# 2
# 3
# 4
# 5
If
if [ "hello" = "world" ]; then echo "equal"; else echo "not equal"; fi
# not equal
ssh
ssh {url} -l {login_name} -p {port}
scp {filename} {user}@{ip}:{path} # 從本地端複製檔案到遠端機器上
wget
wget {url} 把檔案抓下來
netcat
TCP/IP 瑞士刀 (Swiss Army Knife)
nc <host> <port>
nc example.com 12345
import socket
s = socket.socket()
s.connect(("example.com", 12345))
while True:
data = input("> ")
s.send(data.encode())
response = s.recv(4096)
print(response.decode())
strings
把binary file -> string
strings -a -t x file
-a : all
-t x : 十六進位顯示
讀檔
open('dictionary.txt', 'rb').read()
open('dictionary.txt', 'r').read().splitlines()
open('dictionary.txt', 'rb').read().decode('utf-8')
更改權限
chmod +x filename
# read r 4
# write w 2
# execute x 1
Encode/Decode
echo "xxxxx" | base64 -d
查看檔案
xxd filename # 二進位資料和十六進位之間互相轉換
exiftool filename # 看 Metadata
strings filename # 從二進位檔案中找出「可列印文字」的工具
Tmux
| shortcut | description |
|---|---|
| C-b ? | search command |
| C-b , | rename window |
| C-b c | create new window |
| C-b n | go to next window |
| C-b p | go to prev window |
| C-b z | zoom pane |
Resource
Lecture 1: Course Overview + Introduction to the Shell - YouTube