Linux shell用法和技巧
使用Linux shell是我每天的基本工作,但我經常會忘記一些有用的shell命令和l技巧。當然,命令我能記住,但我不敢說能記得如何用它執行某個特定任務。於是,我開始在一個文字檔案裡記錄這些用法,並放在我的Dropbox裡,現在,向大家分享這個檔案。這個檔案我會不斷的更新。需要注意一點的是,有些用法需要在你的Linux系統裡安裝額外的軟體。
UPDATE: November 25, 2013
檢查遠端埠是否對bash開放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
讓程式轉入後臺:
Ctrl + z
將程式轉到前臺:
fg
產生隨機的十六進位制數,其中n是字元數:
openssl rand -hex n
在當前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 ' ' =
相關文章
- Linux shell 用法和技巧Linux
- Linux shell用法和技巧(轉)Linux
- Linux Shell常用技巧(三)Linux
- 常用的shell命令和使用技巧
- Linux Shell 程式設計實戰技巧Linux程式設計
- Linux Shell 陣列建立及使用技巧Linux陣列
- Linux Shell中各種括號用法總結!Linux
- linux中#和## 用法Linux
- Shell除錯有什麼技巧?Linux運維除錯Linux運維
- Linux中vim和shellLinux
- shell有哪些面試簡答題技巧?Linux面試題Linux面試題
- linux shell 動態生成 陣列系列 seq使用技巧Linux陣列
- Linux Shell 動態生成 陣列系列 Seq 使用技巧Linux陣列
- linux shell 指令碼之深入淺出的grep的用法Linux指令碼
- shell 的一些實用 demo 和 技巧分析
- hbase基本概念和hbase shell常用命令用法
- Linux 中高效編寫 Shell 指令碼的 10 個技巧Linux指令碼
- 常用的shell變數用法變數
- [ORM 小技巧] withDefault() 用法ORM
- Shell-變數高階用法變數
- Shell中Read用法簡介
- Linux命令和shell指令碼學習Linux指令碼
- 你不知道的shell用法和指令碼,簡單&強大指令碼
- 來學一學LINUX系統中SHELL環境的搭建技巧Linux
- Linux中&&和&,|和||用法及區別詳解!Linux
- 理解Linux Shell和基本Shell指令碼語言的小貼士Linux指令碼
- 【轉載】SHELL字串處理技巧(${}、##、%%)字串
- linux shellLinux
- 在Linux中,Linux核心和Shell有何區別?Linux
- shell程式設計中shift的用法程式設計
- 美化Linux終端和Shell提示符Linux
- [29期] [Linux常用]Shell 命令和語句Linux
- 理解Linux Shell和基本Shell指令碼語言的小貼士(一)Linux指令碼
- 2020-12-31 Linux Shell指令碼if else 與或的用法Linux指令碼
- linux 中getopt和getopt_long 用法解析Linux
- 【系統】Linux下 zip 和 unzip的用法Linux
- awk小技巧之執行shell命令
- 【Shell】【Tips】Linux的.bash_profile和.bashrc與子Shell的關係Linux