前言
使用Linux shell是一些程式設計師每天的基本工作,但我們常常忘記一些有用的shell指令和技巧。當然,命令我能記住,但我不敢說我能記得如何用它執行某個特定任務。要注意一點的是,有些用法需要在你的Linux系統裡安裝額外的軟體。下面話不多說了,來看看詳細的內容吧。
檢查遠程端口是否對bash開放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
讓進程轉入後台:
Ctrl + z
將進程轉到前台:
fg
產生隨機的十六進位數,其中n是字元數:
openssl rand -hex n
在目前shell/home/ user/file.name
截取前5個字元:
${variable:0:5}
SSH debug 模式:
ssh -vvvv userip_address 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.imcountg bs=8k ; rm -rf /tmp/output.img
測試硬碟讀取速度:
hdparm -Tt /dev/sda
取得文字的md5 hash:
echom -檢查xml格式:
xmllint --noout file.xml
tar zxvf package.tar.gz -C new_dir
修改檔案或目錄的時間戳(YYMMDDhhmm):
用wget指令執行//username:password@hostname
產生隨機密碼(例子裡是16個字元長):
LANG=c
快速備份一個檔案:
cp some_file_name{,.bkp}
存取Windows共用目錄:
smbclient -U "MAIN
存取Windows共用目錄:
執行歷史記錄裡的命令(這裡是第100行):
解壓縮:
netstat顯示所有tcp4監聽埠:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
qcow2鏡像檔轉換:
-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img precise-server-cloudimg-amd64-disk1.raw
所有使用者清單:
getent passwd
Mount root in read/write mode:
mount -o remount,rw /
情況):
mount --bind /source /destination
nsupdate
顯示剩餘記憶體(MB):
打開Vim並跳到檔案結束:
Git 克隆指定分支(master):
Append text to a file when privileges need to be elevated:
echo "some text" | sudo tee -a /path/file
List all kill Signal parameter:
kill -l
Disable recording of the last session in bash history:
kill -9 $$
Scan the network for open ports:
nmap -p 8081 172.20.0.0/16
Set git email:
git config --global user. email "me@example.com"
To sync with master if you have unpublished commits:
git pull --rebase origin master
will All files containing "txt" in their file names are moved into the /home/user directory:
find -iname "*txt*" -exec mv -v {} /home/user \;
Display files side by side:
paste test.txt test1.txt
Progress bar in shell:
pv data.log
Use netcat to send data to Graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
Convert tabs to spaces:
expand test.txt > test1.txt
Skip bash history:
< space >cmd
Go to the previous working directory:
cd -
Split the large tar.gz files (100MB each) and merge them back:
split –b 100m /path/to/large/archive /path/to/output/files cat files* > archive
Use curl to get HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
Set root password to strengthen MySQL security installation:
/usr/bin/mysql_secure_installation
When Ctrl + c does not work:
Ctrl + \
Get the file owner:
stat -c %U file.txt
block device list:
lsblk -f
Find files with spaces at the end of their file names:
find . -type f -exec egrep -l " +$" {} \;
Find the files whose file names have tab indentation
find . -type f -exec egrep -l $'\t' {} \;
Use "=" to print out horizontal lines: select all and copy them into notes
printf '%100s\n' | tr ' ' =
Summary
The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. And convenient, if you have any questions, you can leave a message to communicate.
For more related articles summarizing the 73 commonly used commands in Linux shell, please pay attention to the PHP Chinese website!