--- tags: Linux Usage --- # Linux memo ## linux shell or shell script ### network related commands refer to: https://hackmd.io/@ebCv20MXS0y-pa8XsRnrOw/B1c7d53xt ### brctl linux bridge操作相關指令 ```bash= brctl show #show出機器上所有的bridge brctl addbr "brname" #新增名為"brname"的bridge brctl delbr "brname" #刪除名為"brname"的bridge brctl addif "brname" "ifname" #將名為"ifname"的網路界面新增到bridge"brname" brctl delif "brname" "ifname" #將名為"ifname"的網路界面新增到bridge"brname" ``` ### $HOME/.bashrc檔案 決定了進入shell預設的行為(意思即為開機必做的行為) ### mount linux掛USB:mount -f -t auto /dev/那個設備 /要掛載的那個資料夾 (-f:顯示訊息,-t:掛載的file system,auto表示自動調整FS) linux卸載USB:umount /掛載的那個資料夾 ### grep 較常用於在一段輸出內找出某寫特定關鍵字 例:在/home/yhwu裡面找出檔案格式為.txt的檔案 ```bash= ls /home/yhwu | grep ".txt" ``` 但,若grep加上-v參數,表示含有特定關鍵字的東西不顯示 ### sed 較常使用於指定特定行數的狀況(使用-n參數) 例:列印出test.txt第二行的值 ```bash= cat test.txt | sed -n '2p' ``` ### awk 較常使用於列印特定列數的狀況 例: 若只需要第9列怎麼辦 ```bash= ll | awk '{print $9}' ``` 若是倒數第二欄,則為$NF-1 若該文件是由逗號隔開(.csv),需以逗號為準找出第9列的值 ```bash= ll | awk -F ',' '{print $9}' ``` awk取最後一欄的值 ```bash= ll | awk '{print $NF}' ``` ### head tail 看到資料的頭/尾幾行 只看前五行(-n) ```bash= ll | head -n 5 ``` 只看最後五行(-n) ```bash= ll | tail -n 5 ``` ### wc 印出某檔案字數、行數或是byte數 ```bash= wc -c [filename] #算byte數 wc -w [filename] #算word數 wc -l [filename] #算line數 ``` ### ipmitool ```bash= ipmitool sel list # get a list sel records ipmitool sel elist # get detial sel ipmitool sel clear # 將information清除 ipmitool sensor # 查詢ipmitool sensor六標 ipmitool sdr # 查詢ipmitool sensor讀值 ``` ### -eq(equal)、-ne(not equal)、-gt(greater than)、-ge(greater or equal)、-lt(less than)、-le(less or equal) 此部分在shell script的if-else判斷式時常用 ### dd dd指令備份 1. 備份整個硬碟,以sda複製到sdb為例: dd if=/dev/sda of=/dev/sdb (if:input file,of:output file) 1. 備份整個硬碟成img檔案,以sda複製到usb mount到的 /root/usb,img檔名為FRC-400F_OS為例 dd if=/dev/sda of=/root/usb/FRC-400F_OS.img**** 1. dd可以設定複製多少sectors 可fdisk-l看共用了多少sectors,然後下指令 dd if=/dev/sdb of=HI.img bs=512(512 bytes per sector) count=45646465(total 45646465 sectors) #### dd 刪除RAID dd if=/dev/zero of=/dev/sdc bs=512 seek=$(( $(blockdev --getsz /dev/sdc) - 1024 )) count=1024 ### lspci 查看系統上pci device 若是要看某個位置詳細情況:lspci -s bus:device.function -vvvxxx //-s => 特定位置(例:86:00.0),-vvv => 一些詳細資訊,xxx => 該pci device 256 byte的資訊(ru上看到的那東西) 假設我是要看86:00.0的位置資訊: ```bash= lspci -s 86:00.0 -vvvxxx ``` ### find 尋找檔案的時候用 例:在/home底下找出所有的.zip檔案 ```bash= find /home -name *.zip ``` ### ping 檢查連線時使用 常用參數: -c (count), -I (interface) ### du 看檔案大小時使用 ```bash= du [file_or_folder] du -h [file_or_folder] ``` ### 預設GUI界面開機或是CLI界面開機 (須預先安裝GUI套件) (Ubuntu OS) 預設CLI ```bash= sudo systemctl set-default multi-user.target ``` 預設GUI ```bash= sudo systemctl set-default graphical.target ``` 單次設定 ```bash= init 0 #shutdown init 3 #CLI Mode init 5 #GUI Mode init 6 #reboot ``` ### shell特殊變數 ``` $$:Shell Script本身的PID $?:return value $#:添加到Shell的參數個數 $0:Shell本身的檔案名 $1、$2 ... $n:添加到Shell的各參數值 $*:所有參數以"$1 $2 ... $n"的形式輸出 $@:所有參數以"$1" "$2" ... "$n"的形式輸出 ``` ```bash= vim test.sh ``` 程式碼: ```bash= #!/bin/bash echo "pid : $$" echo "amount of parameter : $#" echo "file name : $0" echo "parameter1 : $1" echo "parameter2 : $2" echo "parameter3 : $3" echo "parameter4 : $4" echo "parameter5 : $5" echo "$*" echo "$@" echo "$?" ``` 執行腳本: ```bash= bash test.sh 11 12 13 14 15 ``` 執行結果: ``` pid : 9236 amount of parameter : 5 file name : test.sh parameter1 : 11 parameter2 : 12 parameter3 : 13 parameter4 : 14 parameter5 : 15 11 12 13 14 15 11 12 13 14 15 0 ``` ### UID相關變數 (擱置) https://www.cnblogs.com/limingluzhu/p/5702486.html https://www.itread01.com/content/1539193090.html ### taskset 可指定process執行在特定的CPU core 指定已執行的process要bind在哪個core ```bash= sudo taskset -cp 14 6020 #taskset -cp (core process) core_ID PID ``` 執行某個process的時候順便一起指定綁定的core ```bash= sudo taskset -c 14 ./TX_taskcase.sh #taskset -c (core) coreID executing_the_process_command ``` ### which which [command] 看是否有該command,有的話,該執行檔放置於哪 ### /dev/null & /dev/zero ### snmp ```bash= # usage [snmpcmd] [options] [agent] [oid] # snmpcmd: such as snmpset, snmpget, snmpwalk, snmpbulkwalk, snmptable, snmptrapd # options: ``` ### 7zip ```bash= sudo add-apt-repository universe sudo apt-get update sudo apt-get install p7zip-full p7zip-rar 7z a filename.7z [file_want_to_packed_to_7zip] # create .7z file 7z e filename.7z # untar a .7z file ``` ### source: https://www.itread01.com/p/173088.html https://wizardforcel.gitbooks.io/llthw/content/ex8.html https://blog.csdn.net/aywb1314/article/details/52239281 https://blog.csdn.net/hjxzb/article/details/46761601 https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/ https://www.opencli.com/linux/shell-script-if-else-elseif https://blog.longwin.com.tw/2018/10/awk-get-nf-last-next-value-2018/ https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/376358/ https://blog.gtwang.org/linux/dd-command-examples/ https://blog.gtwang.org/linux/dd-command-examples/ https://raspberrypi.stackexchange.com/questions/47325/copy-multiple-partitions-to-one-img-file https://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html https://blog.gtwang.org/linux/run-program-process-specific-cpu-cores-linux/ https://zh.wikipedia.org/wiki/%E7%AE%80%E5%8D%95%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86%E5%8D%8F%E8%AE%AE#cite_note-1
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up