下面為大家總結(jié)Linux shell的常用命令
前言
使用Linux shell是一些程序員每天的基本工作,但我們經(jīng)常會(huì)忘記一些有用的shell命令和技巧。當(dāng)然,命令我能記住,但我不敢說(shuō)能記得如何用它執(zhí)行某個(gè)特定任務(wù)。需要注意一點(diǎn)的是,有些用法需要在你的Linux系統(tǒng)里安裝額外的軟件。下面話不多說(shuō)了,來(lái)看看詳細(xì)的內(nèi)容吧。
檢查遠(yuǎn)程端口是否對(duì)bash開放:
echo?>/dev/tcp/8.8.8.8/53?&&?echo?"open"
讓進(jìn)程轉(zhuǎn)入后臺(tái):
Ctrl?+?z
將進(jìn)程轉(zhuǎn)到前臺(tái):
fg
產(chǎn)生隨機(jī)的十六進(jìn)制數(shù),其中n是字符數(shù):
openssl?rand?-hex?n
在當(dāng)前shell里執(zhí)行一個(gè)文件里的命令:
source?/home/user/file.name
截取前5個(gè)字符:
${variable:0:5}
SSH debug 模式:
ssh?-vvv?user@ip_address
SSH with pem key:
ssh?user@ip_address?-i?key.pem
用wget抓取完整的網(wǎng)站目錄結(jié)構(gòu),存放到本地目錄中:
wget?-r?--no-parent?--reject?"index.html*"?http://hostname/?-P?/home/user/dirs
一次創(chuàng)建多個(gè)目錄:
mkdir?-p?/home/user/{test,test1,test2}
列出包括子進(jìn)程的進(jìn)程樹:
ps?axwef
創(chuàng)建 war 文件:
jar?-cvf?name.war?file
測(cè)試硬盤寫入速度:
dd?if=/dev/zero?of=/tmp/output.img?bs=8k?count=256k;?rm?-rf?/tmp/output.img
測(cè)試硬盤讀取速度:
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
修改文件或目錄的時(shí)間戳(YYMMDDhhmm):
touch?-t?0712250000?file
用wget命令執(zhí)行ftp下載:
wget?-m?ftp://username:password@hostname
生成隨機(jī)密碼(例子里是16個(gè)字符長(zhǎng)):
LANG=c?<p><strong>快速備份一個(gè)文件:</strong></p><pre class="brush:php;toolbar:false">cp?some_file_name{,.bkp}
訪問windows共享目錄:
smbclient?-U?"DOMAINuser"?//dc.domain.com/share/test/dir
執(zhí)行歷史記錄里的命令(這里是第100行):
!100
解壓:
unzip?package_name.zip?-d?dir_name
輸入多行文字(CTRL + d 退出):
cat?>?test.txt
創(chuàng)建空文件或清空一個(gè)現(xiàn)有文件:
>?test.txt
與Ubuntu NTP server同步時(shí)間:
ntpdate?ntp.ubuntu.com
用netstat顯示所有tcp4監(jiān)聽端口:
netstat?-lnt4?|?awk?'{print?$4}'?|?cut?-f2?-d:?|?grep?-o?'[0-9]*'
qcow2鏡像文件轉(zhuǎn)換:
qemu-img?convert?-f?qcow2?-O?raw?precise-server-cloudimg-amd64-disk1.img?precise-server-cloudimg-amd64-disk1.raw
重復(fù)運(yùn)行文件,顯示其輸出(缺省是2秒一次):
watch?ps?-ef
所有用戶列表:
getent?passwd
Mount root in read/write mode:
mount?-o?remount,rw?/
掛載一個(gè)目錄(這是不能使用鏈接的情況):
mount?--bind?/source?/destination
動(dòng)態(tài)更新DNS server:
nsupdate?<p><strong>遞歸grep所有目錄:</strong></p><pre class="brush:php;toolbar:false">grep?-r?"some_text"?/path/to/dir
列出前10個(gè)最大的文件:
lsof?/?|?awk?'{?if($7?>?1048576)?print?$7/1048576?"MB?"$9?}'?|?sort?-n?-u?|?tail
顯示剩余內(nèi)存(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 刪除遠(yuǎn)程分支
git?push?origin?:branchName
Git 將新分支推送到遠(yuǎn)程服務(wù)器:
git?push?-u?origin?mynewfeature
打印歷史記錄中最后一次cat命令:
!cat:p
運(yùn)行歷史記錄里最后一次cat命令:
!cat
找出/home/user下所有空子目錄:
find?/home/user?-maxdepth?1?-type?d?-empty
獲取test.txt文件中第50-60行內(nèi)容:
<p><strong>運(yùn)行最后一個(gè)命令(如果最后一個(gè)命令是mkdir /root/test, 下面將會(huì)運(yùn)行: sudo mkdir /root/test):</strong></p><pre class="brush:php;toolbar:false">sudo?!!
創(chuàng)建臨時(shí)RAM文件系統(tǒng) – ramdisk (先創(chuàng)建/tmpram目錄):
mount?-t?tmpfs?tmpfs?/tmpram?-o?size=512m
Grep whole words:
grep?-w?"name"?test.txt
在需要提升權(quán)限的情況下往一個(gè)文件里追加文本:
echo?"some?text"?|?sudo?tee?-a?/path/file
列出所有kill signal參數(shù):
kill?-l
在bash歷史記錄里禁止記錄最后一次會(huì)話:
kill?-9?$$
掃描網(wǎng)絡(luò)尋找開放的端口:
nmap?-p?8081?172.20.0.0/16
設(shè)置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里的進(jìn)度條:
pv?data.log
使用netcat將數(shù)據(jù)發(fā)送到Graphite server:
echo?"hosts.sampleHost?10?`date?+%s`"?|?nc?192.168.200.2?3000
將tabs轉(zhuǎn)換成空格:
expand?test.txt?>?test1.txt
Skip bash history:
cmd
去之前的工作目錄:
cd?-
拆分大體積的tar.gz文件(每個(gè)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
設(shè)置root密碼,強(qiáng)化MySQL安全安裝:
/usr/bin/mysql_secure_installation
當(dāng)Ctrl + c不好使時(shí):
Ctrl?+?
獲取文件owner:
stat?-c?%U?file.txt
block設(shè)備列表:
lsblk?-f
找出文件名結(jié)尾有空格的文件:
find?.?-type?f?-exec?egrep?-l?"?+$"?{}?;
找出文件名有tab縮進(jìn)符的文件
find?.?-type?f?-exec?egrep?-l?$' '?{}?;
用”=”打印出橫線:全選復(fù)制放進(jìn)筆記
printf?'%100s '?|?tr?'?'?=