Linux shell用法和技巧

使用Linux shell是我天天的基本工做,但我常常會忘記一些有用的shell命令和l技巧。固然,命令我能記住,但我不敢說能記得如何用它執行某個特定任務。因而,我開始在一個文本文件裏記錄這些用法,並放在個人Dropbox裏,如今,向你們分享這個文件。這個文件我會不斷的更新。須要注意一點的是,有些用法須要在你的Linux系統裏安裝額外的軟件。html


UPDATE: November 25, 2013mysql


檢查遠程端口是否對bash開放:linux


echo >/dev/tcp/8.8.8.8/53 && echo "open"git


讓進程轉入後臺:github


Ctrl + zweb


將進程轉到前臺:sql


fgshell


產生隨機的十六進制數,其中n是字符數:ubuntu


openssl rand -hex nvim


在當前shell裏執行一個文件裏的命令:


source /home/user/file.name


截取前5個字符:


${variable:0:5}


SSH debug 模式:


ssh -vvv user@ip_address


SSH with pem key:


ssh user@ip_address -i key.pem


用wget抓取完整的網站目錄結構,存放到本地目錄中:


wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs


一次建立多個目錄:


mkdir -p /home/user/{test,test1,test2}


列出包括子進程的進程樹:


ps axwef


建立 war 文件:


jar -cvf name.war file


測試硬盤寫入速度:


dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img


測試硬盤讀取速度:


hdparm -Tt /dev/sda


獲取文本的md5 hash:


echo -n "text" | md5sum


檢查xml格式:


xmllint --noout file.xml


將tar.gz提取到新目錄裏:


tar zxvf package.tar.gz -C new_dir


使用curl獲取HTTP頭信息:


curl -I http://www.example.com


修改文件或目錄的時間戳(YYMMDDhhmm):


touch -t 0712250000 file


用wget命令執行ftp下載:


wget -m ftp://username:password@hostname


生成隨機密碼(例子裏是16個字符長):


LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;


快速備份一個文件:


cp some_file_name{,.bkp}


訪問Windows共享目錄:


smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir


執行歷史記錄裏的命令(這裏是第100行):


!100


解壓:


unzip package_name.zip -d dir_name


輸入多行文字(CTRL + d 退出):


cat > test.txt


建立空文件或清空一個現有文件:


> test.txt


與Ubuntu NTP server同步時間:


ntpdate ntp.ubuntu.com


用netstat顯示全部tcp4監聽端口:


netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'


qcow2鏡像文件轉換:


qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \

precise-server-cloudimg-amd64-disk1.raw


重複運行文件,顯示其輸出(缺省是2秒一次):


watch ps -ef


全部用戶列表:


getent passwd


Mount root in read/write mode:


mount -o remount,rw /


掛載一個目錄(這是不能使用連接的狀況):


mount --bind /source /destination


動態更新DNS server:


nsupdate < <EOF

update add $HOST 86400 A $IP

send

EOF


遞歸grep全部目錄:


grep -r "some_text" /path/to/dir


列出前10個最大的文件:


lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail


顯示剩餘內存(MB):


free -m | grep cache | awk '/[0-9]/{ print $4" MB" }'


打開Vim並跳到文件末:


vim + some_file_name


Git 克隆指定分支(master):


git clone git@github.com:name/app.git -b master


Git 切換到其它分支(develop):


git checkout develop


Git 刪除分支(myfeature):


git branch -d myfeature


Git 刪除遠程分支:


git push origin :branchName


Git 將新分支推送到遠程服務器:


git push -u origin mynewfeature


打印歷史記錄中最後一次cat命令:


!cat:p


運行歷史記錄裏最後一次cat命令:


!cat


找出/home/user下全部空子目錄:


find /home/user -maxdepth 1 -type d -empty


獲取test.txt文件中第50-60行內容:


< test.txt sed -n '50,60p'


運行最後一個命令(若是最後一個命令是mkdir /root/test, 下面將會運行: sudo mkdir /root/test):


sudo !!


建立臨時RAM文件系統 – ramdisk (先建立/tmpram目錄):


mount -t tmpfs tmpfs /tmpram -o size=512m


Grep whole words:


grep -w "name" test.txt


在須要提高權限的狀況下往一個文件裏追加文本:


echo "some text" | sudo tee -a /path/file


列出全部kill signal參數:


kill -l


在bash歷史記錄裏禁止記錄最後一次會話:


kill -9 $$


掃描網絡尋找開放的端口:


nmap -p 8081 172.20.0.0/16


設置git email:


git config --global user.email "me@example.com"


To sync with master if you have unpublished commits:


git pull --rebase origin master


將全部文件名中含有」txt」的文件移入/home/user目錄:


find -iname "*txt*" -exec mv -v {} /home/user \;


將文件按行並列顯示:


paste test.txt test1.txt


shell裏的進度條:


pv data.log


使用netcat將數據發送到Graphite server:


echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000


將tabs轉換成空格:


expand test.txt > test1.txt


Skip bash history:


< space >cmd


去以前的工做目錄:


cd -


拆分大致積的tar.gz文件(每一個100MB),而後合併回去:


split –b 100m /path/to/large/archive /path/to/output/files

cat files* > archive


使用curl獲取HTTP status code:


curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null


設置root密碼,強化MySQL安全安裝:


/usr/bin/mysql_secure_installation


當Ctrl + c很差使時:


Ctrl + \


獲取文件owner:


stat -c %U file.txt


block設備列表:


lsblk -f


找出文件名結尾有空格的文件:


find . -type f -exec egrep -l " +$" {} \;


找出文件名有tab縮進符的文件:


find . -type f -exec egrep -l $'\t' {} \;


用」=」打印出橫線:


printf '%100s\n' | tr ' ' =


原文出處: techbar

譯文出處: 外刊IT評論

譯文連接: http://www.vaikan.com/linux-shell-tips-and-tricks/

相關文章
相關標籤/搜索