Linux那些冷僻但實用的命令

lsblk命令

"lsblk"就是列出塊設備。除了RAM外,以標準的樹狀輸出格式,整齊地顯示塊設備。

01 root@tecmint:~# lsblk
02  
03 NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
04 sda      8:0    0 232.9G  0 disk
05 ├─sda1   8:1    0  46.6G  0 part /
06 ├─sda2   8:2    0     1K  0 part
07 ├─sda5   8:5    0   190M  0 part /boot
08 ├─sda6   8:6    0   3.7G  0 part [SWAP]
09 ├─sda7   8:7    0  93.1G  0 part /data
10 └─sda8   8:8    0  89.2G  0 part /personal
11 sr0     11:0    1  1024M  0 rom

lsblk -l”命令以列表格式顯示塊設備(而不是樹狀格式)。

01 root@tecmint:~# lsblk -l
02  
03 NAME MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
04 sda    8:0    0 232.9G  0 disk
05 sda1   8:1    0  46.6G  0 part /
06 sda2   8:2    0     1K  0 part
07 sda5   8:5    0   190M  0 part /boot
08 sda6   8:6    0   3.7G  0 part [SWAP]
09 sda7   8:7    0  93.1G  0 part /data
10 sda8   8:8    0  89.2G  0 part /personal
11 sr0   11:0    1  1024M  0 rom

注意:lsblk是最有用和最簡單的方式來了解插入的USB設備的名字,特別是當你在終端上處理磁盤/塊設備時。


md5sum命令

md5sum”就是計算和檢驗MD5信息簽名。md5 checksum(通常叫做哈希)使用匹配或者驗證文件的文件的完整性,因爲文件可能因爲傳輸錯誤,磁盤錯誤或者無惡意的干擾等原因而發生改變。

1 root@tecmint:~# md5sum teamviewer_linux.deb
2  
3 47790ed345a7b7970fc1f2ac50c97002  teamviewer_linux.deb

注意:用戶可以使用官方提供的和md5sum生成簽名信息匹對以此檢測文件是否改變。Md5sum沒有sha1sum安全,這點我們稍後討論。


dd命令

dd”命令代表了轉換和複製文件。可以用來轉換和複製文件,大多數時間是用來複制iso文件(或任何其它文件)到一個usb設備(或任何其它地方)中去,所以可以用來製作USB啓動器。

1 root@tecmint:~# dd if=/home/user/Downloads/debian.iso of=/dev/sdb1 bs=512M; sync

注意:在上面的例子中,usb設備就是sdb1(你應該使用lsblk命令驗證它,否則你會重寫你的磁盤或者系統),請慎重使用磁盤的名,切忌。

dd 命令在執行中會根據文件的大小和類型 以及 usb設備的讀寫速度,消耗幾秒到幾分鐘不等。


uname命令

"uname"命令就是Unix Name的簡寫。顯示機器名,操作系統和內核的詳細信息。

1 root@tecmint:~# uname -a
2  
3 Linux tecmint 3.8.0-19-generic #30-Ubuntu SMP Wed May 1 16:36:13 UTC 2013 i686 i686 i686 GNU/Linux

注意: uname顯示內核類別, uname -a顯示詳細信息。上面的輸出詳細說明了uname -a

  1. Linux“: 機器的內核名
  2. tecmint“: 機器的節點名
  3. 3.8.0-19-generic“: 內核發佈版本
  4. #30-Ubuntu SMP“: 內核版本
  5. i686“: 處理器架構
  6. GNU/Linux“: 操作系統名

history命令

history”命令就是歷史記錄。它顯示了在終端中所執行過的所有命令的歷史。

01 root@tecmint:~# history
02  
03  1  sudo add-apt-repository ppa:tualatrix/ppa
04  2  sudo apt-get update
05  3  sudo apt-get install ubuntu-tweak
06  4  sudo add-apt-repository ppa:diesch/testing
07  5  sudo apt-get update
08  6  sudo apt-get install indicator-privacy
09  7  sudo add-apt-repository ppa:atareao/atareao
10  8  sudo apt-get update
11  9  sudo apt-get install my-weather-indicator
12  10 pwd
13  11 cd && sudo cp -r unity/6 /usr/share/unity/
14  12 cd /usr/share/unity/icons/
15  13 cd /usr/share/unity

注意:按住“CTRL + R”就可以搜索已經執行過的命令,它可以在你寫命令時自動補全。

1 (reverse-i-search)`if': ifconfig

cmp命令

比較兩個任意類型的文件並將結果輸出至標準輸出。如果兩個文件相同, ‘cmp‘默認返回0;如果不同,將顯示不同的字節數和第一處不同的位置。

以下面兩個文件爲例:

file1.txt
1 root@tecmint:~# cat file1.txt
2  
3 Hi My name is Tecmint
file2.txt
1 root@tecmint:~# cat file2.txt
2  
3 Hi My name is tecmint [dot] com

比較一下這兩個文件,看看命令的輸出。

1 root@tecmint:~# cmp file1.txt file2.txt
2  
3 file1.txt file2.txt differ: byte 15, line 1

service命令

service‘命令控制服務的啓動、停止和重啓,它讓你能夠不重啓整個系統就可以讓配置生效以開啓、停止或者重啓某個服務。

在Ubuntu上啓動apache2 server:

1 root@tecmint:~# service apache2 start
2  
3  * Starting web server apache2                                                                                                                                 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName
4 httpd (pid 1285) already running                        [ OK ]

重啓apache2 server:

1 root@tecmint:~# service apache2 restart
2  
3 * Restarting web server apache2                                                                                                                               apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName
4  ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName  [ OK ]

停止apache2 server:

1 root@tecmint:~# service apache2 stop
2  
3  * Stopping web server apache2                                                                                                                                 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 forServerName
4  ... waiting                                                                [ OK ]

注意:要想使用service命令,進程的腳本必須放在‘/etc/init.d‘,並且路徑必須在指定的位置。

如果要運行“service apache2 start”實際上實在執行“service /etc/init.d/apache2 start”.


sudo !!命令

以管理員權限運行最後一個命令

1 $ apt-get update
2  
3 E: Could not open lock file /var/lib/apt/lists/lock - open(13: Permission denied)
4 E: Unable to lock directory /var/lib/apt/lists/
5 E: Could not open lock file /var/lib/dpkg/lock - open(13: Permission denied)
6 E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
1 sudo !!
2  
3 sudoapt-get update
4 [sudo] password forserver:
5
6 ..
7 Fetched 474 kB in16s (28.0 kB/s)
8 Reading package lists... Done
9 server@localhost:~$

 python命令

下面的命令生產一個通過HTTP顯示文件夾結構樹的簡單網頁,可以通過瀏覽器在端口8000訪問,直到發出中斷信號。

1 # python -m SimpleHTTPServer




Ctrl+x+e命令

這個命令對於管理員和開發者非常有用。爲了使每天的任務自動化,管理員需要通過輸入vivimnano等打開編輯器。

僅僅從命令行快速的敲擊“Ctrl-x-e”,就可以在編輯器中開始工作了。


 nl命令

nl命令”添加文件的行數。一個叫做'one.txt'的文件,其每行的內容是(FedoraDebianArchSlackSuse),給每行添加行號。首先使用cat命令顯示“one.txt”的文件內容。

1 # cat one.txt
2  
3 fedora
4 debian
5 arch
6 slack
7 suse
現在運行“nl命令”,以添加行號的方式來顯示。
1 # nl one.txt
2  
3 1 fedora
4 2 debian
5 3 arch
6 4 slack


shuf命令

“Shut”命令隨機從一個文件文件夾中選擇行/文件/文件夾。首先使用ls命令來顯示文件夾的內容。

1 # ls
2  
3 Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
01 #  ls | shuf (shuffle Input)
02  
03 Music
04 Documents
05 Templates
06 Pictures
07 Public
08 Desktop
09 Downloads
10 Videos
1 #  ls | shuf -n1 (pick on random selection)
2  
3 Public
1 # ls | shuf -n1
2  
3 Videos
1 # ls | shuf -n1
2  
3 Templates
1 # ls | shuf -n1
2  
3 Downloads
注意:你可以把‘ n1’替換成‘ n2’來輸出兩個隨機選擇或者使用 n3、 n4等數字輸出其他任意的隨機選擇。

ss命令

ss”表示socket統計。這個命令調查socket,顯示類似netstat命令的信息。它可以比其他工具顯示更多的TCP和狀態信息。

1 # ss
2  
3 State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port  
4 ESTAB      0      0           192.168.1.198:41250        *.*.*.*:http   
5 CLOSE-WAIT 1      0               127.0.0.1:8000             127.0.0.1:41393  
6 ESTAB      0      0           192.168.1.198:36239        *.*.*.*:http   
7 ESTAB      310    0               127.0.0.1:8000             127.0.0.1:41384  
8 ESTAB      0      0           192.168.1.198:41002       *.*.*.*:http   
9 ESTAB      0      0               127.0.0.1:41384            127.0.0.1:8000

 last命令

last”命令顯示的是上次登錄用戶的歷史信息。這個命令通過搜索文件“/var/log/wtmp”,顯示logged-inlogged-out及其tty‘s的用戶列表。

01 #  last
02 server   pts/0        :0               Tue Oct 22 12:03   still logged in  
03 server   tty8         :0               Tue Oct 22 12:02   still logged in  
04
05 ...
06 (unknown tty8         :0               Tue Oct 22 12:02 - 12:02  (00:00)   
07 server   pts/0        :0               Tue Oct 22 10:33 - 12:02  (01:29)   
08 server   tty7         :0               Tue Oct 22 10:05 - 12:02  (01:56)   
09 (unknown tty7         :0               Tue Oct 22 10:04 - 10:05  (00:00)   
10 reboot   system boot  3.2.0-4-686-pae  Tue Oct 22 10:04 - 12:44  (02:39)   
11  
12 wtmp begins Fri Oct  4 14:43:17 2007

 tree命令

以樹式的格式得到當前文件夾的結構。

1 # tree
01 .
02 |-- Desktop
03 |-- Documents
04 |   `-- 37.odt
05 |-- Downloads
06 |   |-- attachments.zip
07  
08 |   |-- ttf-indic-fonts_0.5.11_all.deb
09 |   |-- ttf-indic-fonts_1.1_all.deb
10 |   `-- wheezy-nv-install.sh
11 |-- Music
12 |-- Pictures
13 |   |-- Screenshot from 2013-10-22 12:03:49.png
14 |   `-- Screenshot from 2013-10-22 12:12:38.png
15 |-- Public
16 |-- Templates
17 `-- Videos
18  
19 10 directories, 23 files

 pstree

這個命令顯示當前運行的所有進程及其相關的子進程,輸出的是類似‘tree’命令的樹狀格式。

01 # pstree
02 init─┬─NetworkManager───{NetworkManager}
03      ├─accounts-daemon───{accounts-daemon}
04      ├─acpi_fakekeyd
05      ├─acpid
06      ├─apache2───10*[apache2]
07      ├─at-spi-bus-laun───2*[{at-spi-bus-laun}]
08      ├─atd
09      ├─avahi-daemon───avahi-daemon
10      ├─bluetoothd
11      ├─colord───{colord}
12      ├─colord-sane───2*[{colord-sane}]
13      ├─console-kit-dae───64*[{console-kit-dae}]
14      ├─cron
15      ├─cupsd
16      ├─2*[dbus-daemon]
17      ├─dbus-launch
18      ├─dconf-service───2*[{dconf-service}]
19      ├─dovecot─┬─anvil
20      │         ├─config
21      │         └─log
22      ├─exim4
23      ├─gconfd-2
24      ├─gdm3─┬─gdm-simple-slav─┬─Xorg
25      │      │                 ├─gdm-session-wor─┬─x-session-manag─┬─evolution-a+
26      │      │                 │                 │                 ├─gdu-notific+
27      │      │                 │                 │                 ├─gnome-scree+
28      │      │                 │                 │                 ├─gnome-setti+
29      │      │                 │                 │                 ├─gnome-shell+++
30      │      │                 │                 │                 ├─nm-applet──+++
31      │      │                 │                 │                 ├─ssh-agent
32      │      │                 │                 │                 ├─tracker-min+
33      │      │                 │                 │                 ├─tracker-sto+
34      │      │                 │                 │                 └─3*[{x-sessi+
35      │      │                 │                 └─2*[{gdm-session-wor}]
36      │      │                 └─{gdm-simple-slav}
37      │      └─{gdm3}
38      ├─6*[getty]
39      ├─gnome-keyring-d───9*[{gnome-keyring-d}]
40      ├─gnome-shell-cal───2*[{gnome-shell-cal}]
41      ├─goa-daemon───{goa-daemon}
42      ├─gsd-printer───{gsd-printer}
43      ├─gvfs-afc-volume───{gvfs-afc-volume}

檢查遠程端口是否對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 [email protected]: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'

創建臨時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 "[email protected]"

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 ' ' =

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章