UNIX/Linux
Command lines
- Minimal safe Bash script template - Better Dev
- Mosky slide: Get Power From Command Line - Speaker Deck
Shell
帳號的shell設定都是寫在 /etc/passwd
看目前的shell
列出系統的shells
file descriptor:- 0: stdin
- 1: stdout
- 2: stderr
2>&1
就是把stderr redirect 到 stdout
Editing file
examples:test.csv
A,B,C,D,E,F,G,H
cut -d, -f 1 < test.csv # A
cut -d, -f 1-5 < test.csv # A,B,C,D,E
cut -d, -f 1-3,6- < test.csv # A,B,C,F,G,H
找東西
find in file (grep-like):
```bash titil="find files create in 5mins" find . -cmin -5
find . -name ".DS_Store" -delete
find . -type f -name '._*' -delete
find . -type f -not -name '._*' -size -5k
找出一年前的檔案,並計算出大小
grep
- o, --only-matching: 只印出找到的字串,不會印整行(這個在minified檔案裡很煩)
- P, --perl-regexp: 可以用regex
- l, 只有引檔名
- A 3, 後3行
- B 3, 前3行
- C 3, 前後3行
count, sum
via: https://stackoverflow.com/a/39622947/644070du -a
: list all files include directory ( 416 ./images/path/to => 416 is size)cut -d/ -f2
: remove sections from each line of files, -d: delimiter, -f: select only these fields (把下一層目錄,這邊是image
取出)uniq -c
: -c: countsort -nr
: -n: numeric-sort, -r: reverse
find . -type f | cut -d/ -f2 | sort | uniq -c
du -a | cut -d/ -f2 | sort | uniq -c | sort -nr
# du -d 1 |sort -nr | cut -f2- | xargs du -hs # mac
du --max-depth=1 |sort -nr | cut -f2- | xargs du -hs
找出某種目錄名稱,統計總共多大
via: chatgptprint0
prints the full directory name on the standard output, followed by a null character (instead of a newline). This is useful to handle directory names with spaces.xargs -0
takes the null-separated output fromfind
and passes it todu
.
process data (awk, sed, grep, tr)
sed -i '1 i\foo' path/to/file # insert "foo" string to head of the file
sed -i '2 i\bar' path/to/file # insert "foo" string to line 2
cat foo.csv | awk -F, '{print "curl -O https://url.to/"$3".jpg"}' | bash
#3-Let It Go,https://www.youtube.com/watch?v=L0MK7qz13bU
awk -F, '{print "youtube-dl " $2 " -x --audio-format mp3 -o " $1 ".mp3"}' my-mp3-list.txt > mp3-cmds.sh
./mp3-cmds.sh
# single quote
grep -rho 'data-ga-category="[^"]*"' ./path/to | sed 's/data-ga-category="//' | sed 's/"$//' | uniq > ga-category.txt
# double quote
grep -rho 'data-ga-category="[^"]*"' ./path/to | sed 's/data-ga-category="//' | sed 's/"$//' | uniq >> ga-category.txt
test.csv
A,B,C,D,E,F,G,H
cut -d, -f 1 < test.csv # A
cut -d, -f 1-5 < test.csv # A,B,C,D,E
cut -d, -f 1-3,6- < test.csv # A,B,C,F,G,H
拿到Excel資料是1個欄位,很多列(Row),要變成1列很多欄(Column)的形狀
split big file to chunk (1000 line per file), prefix will gose to prefixaa, prefixab, prefixac...
Process modern data (jq for JSON)
jq
extract json string data, ex:
File Coding
- -b brief output (ignore file name) - -i include MIME-type information but maybe wrong, ex: file indicated "ISO-8859-1", but it's cp950.Networks
telnet & openssl:
openssl s_client -connect my-host.com:443
GET / HTTP/1.1
Host: my-host.com
Connection: close (自動關閉連線)
sudo mount -t cifs //some-ip/path my-local/ -o username=my-username,password=my-password
smbmount
or smbfs
seems deprecated, use cifs
instead (Debian package: cifs-utils
)
很多網路指令都有新的選擇(ifconfig, netstat, route, arp) => ip
nixCraft - Ugh 😤 ip command is the most significant change in... | Facebook
System Admin
su my-user
跟su - my-user
的差別,多了一個-
就是會把該user的環境變數跟shell配置代入,如果沒有-
的話,就是沿用目前用戶的環境變數。
test HD Performance, use the dd command to measure server throughput (write speed):
Linux and Unix Test Disk I/O Performance With dd Command - nixCrafthtop explained | peteris.rocks
Device
check usb device
check CPU
Multimedia
Other examples
#!/bin/bash
output_file="nonexistent_keys.txt"
while IFS= read -r key; do
if aws s3 ls "s3://my-bucket/path/to/$key".jpg > /dev/null 2>&1; then
echo "$key exists"
else
echo "$key not exists"
echo "$key" >> "$output_file"
fi
done < keys.txt
echo "done"