shell常用命令

shell常用命令

下面為大家總結Linux shell的常用命令

前言

使用Linux shell是一些程序員每天的基本工作,但我們經常會忘記一些有用的shell命令和技巧。當然,命令我能記住,但我不敢說能記得如何用它執行某個特定任務。需要注意一點的是,有些用法需要在你的Linux系統里安裝額外的軟件。下面話不多說了,來看看詳細的內容吧。

檢查遠程端口是否對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?<p><strong>快速備份一個文件:</strong></p><pre class="brush:php;toolbar:false">cp?some_file_name{,.bkp}

訪問windows共享目錄:

smbclient?-U?"DOMAINuser"?//dc.domain.com/share/test/dir

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

!100

解壓:

unzip?package_name.zip?-d?dir_name

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

cat?&gt;?test.txt

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

&gt;?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?<p><strong>遞歸grep所有目錄:</strong></p><pre class="brush:php;toolbar:false">grep?-r?"some_text"?/path/to/dir

列出前10個最大的文件:

lsof?/?|?awk?'{?if($7?&gt;?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行內容:

<p><strong>運行最后一個命令(如果最后一個命令是mkdir /root/test, 下面將會運行: sudo mkdir /root/test):</strong></p><pre class="brush:php;toolbar:false">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?&gt;?test1.txt

Skip bash history:

cmd

去之前的工作目錄:

cd?-

拆分大體積的tar.gz文件(每個100MB),然后合并回去:

split?–b?100m?/path/to/large/archive?/path/to/output/files?cat?files*?&gt;?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?$'	'?{}?;

用”=”打印出橫線:全選復制放進筆記

printf?'%100s '?|?tr?'?'?=

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享