Bash Code Snippets
- 拿 csv 的第 1 個 col
bash
cut -d',' -f1
- 拔掉 stdout 第一行
bash
sed 1d
- multiprocess
bash
python3 main.py &
proc1_id=$!
python3 main.py &
proc2_id=$!
trap_ctrlc() {
kill $proc1_id
kill $proc2_id
wait $proc1_id
wait $proc2_id
}
trap trap_ctrlc INT
wait
- Find and Replace in file:
bash
sed -i 's/old-text/new-text/g' filename
- Find files which size larger than 1M:
bash
find . -type f -size +1M
- Find files:
bash
find -L . -name '*.png'
- Unzip .tar.gz:
bash
tar zxvf {tar filename}
- Zip .tar.gz:
bash
tar zcvf {tar filename} {dir}
- Page Size
bash
getconf PAGESIZE
- CPU Info
bash
lscpu
- Get OOM Log Source: Finding which process was killed by Linux OOM killer
bash
dmesg -T | egrep -i 'killed process'
- Replace strings in a file Source: How to replace a string in multiple files in linux command line Mac
bash
sed -i '.bak' 's/foo/bar/g' *
Linux
bash
sed -i 's/foo/bar/g' *
jq List the values of a field in json files under a folder
bash
jq -r '.field_name | select(.!=null)' *