天天看點

鮮為人知的Linux指令

Linux指令行吸引了大多數Linux愛好者。一個正常的Linux使用者一般掌握大約50-60個指令來處理每日的任務。Linux指令和它們的轉換對于Linux使用者、Shell腳本程式員和管理者來說是最有價值的寶藏。有些Linux指令很少人知道,但不管你是新手還是進階使用者,它們都非常友善有用。

鮮為人知的Linux指令

  少有人知道的Linux指令

  這篇文章的目的是介紹一些少有人知的Linux指令,它們一定會高效地幫你管理你的桌面/伺服器。

  1. sudo !!指令

  沒有特定輸入sudo指令而運作,将給出沒有權限的錯誤。那麼,你不需要重寫整個指令,僅僅輸入'!!'就可以抓取最後的指令。

1 2 3 4 5 6

$ apt-get update

E: Could not

open

lock

file

/var/lib/apt/lists/lock

-

open

(13: Permission denied)

E: Unable to lock directory

/var/lib/apt/lists/

E: Could not

open

lock

file

/var/lib/dpkg/lock

-

open

(13: Permission denied)

E: Unable to lock the administration directory (

/var/lib/dpkg/

), are you root?

1 2 3 4 5 6 7 8 9

$

sudo

!!

sudo

apt-get update

[

sudo

] password

for

server:

..

Fetched 474 kB

in

16s (28.0 kB

/s

)

Reading package lists... Done

[email protected]:~$

  2. python指令

  下面的指令生産一個通過HTTP顯示檔案夾結構樹的簡單網頁,可以通過浏覽器在端口8000通路,直到發出中斷信号。

1

# python -m SimpleHTTPServer

鮮為人知的Linux指令

  3. mtr指令

  我們大多數都熟悉ping和traceroute。那對于把兩個指令的功能合二為一的mtr指令呢。如果mtr沒在你的機子上安裝,apt或者yum需要的包。

1

$

sudo

apt-get

install

mtr (On Debian based Systems)

1

# yum install mtr (On Red Hat based Systems)

  現在運作mtr指令,開始檢視mtr運作的主機和google.com直接的網絡連接配接。

1

# mtr google.com

鮮為人知的Linux指令

  mtr指令

 4. Ctrl+x+e指令

  這個指令對于管理者和開發者非常有用。為了使每天的任務自動化,管理者需要通過輸入vi、vim、nano等打開編輯器。

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

  5. nl指令

  “nl指令”添加檔案的行數。一個叫做'one.txt'的檔案,其每行的内容是(Fedora、Debian、Arch、Slack和Suse),給每行添加行号。首先使用cat指令顯示“one.txt”的檔案内容。

1 2 3 4 5 6 7

# cat one.txt

fedora

debian

arch

slack

suse

  現在運作“nl指令”,以添加行号的方式來顯示。

1 2 3 4 5 6 7

# nl one.txt

1 fedora

2 debian

3 arch

4 slack

5 suse

  6. shuf指令

  “Shut”指令随機從一個檔案或檔案夾中選擇行/檔案/檔案夾。首先使用ls指令來顯示檔案夾的内容。

1 2 3

# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

1 2 3 4 5 6 7 8 9 10

#  ls | shuf (shuffle Input)

Music

Documents

Templates

Pictures

Public

Desktop

Downloads

Videos

1 2 3

#  ls | shuf -n1 (pick on random selection)

Public

1 2 3

# ls | shuf -n1

Videos

1 2 3

# ls | shuf -n1

Templates

1 2 3

# ls | shuf -n1

Downloads

  注意:你可以把‘ n1’替換成‘ n2’來輸出兩個随機選擇或者使用 n3、 n4等數字輸出其他任意的随機選擇。

  7. ss指令

  “ss”表示socket統計。這個指令調查socket,顯示類似netstat指令的資訊。它可以比其他工具顯示更多的TCP和狀态資訊。

1 2 3 4 5 6 7 8 9

# ss

State      Recv-Q Send-Q      Local Address:Port          Peer Address:Port  

ESTAB      0      0           192.168.1.198:41250        *.*.*.*:http   

CLOSE-WAIT 1      0               127.0.0.1:8000             127.0.0.1:41393  

ESTAB      0      0           192.168.1.198:36239        *.*.*.*:http   

ESTAB      310    0               127.0.0.1:8000             127.0.0.1:41384  

ESTAB      0      0           192.168.1.198:41002       *.*.*.*:http   

ESTAB      0      0               127.0.0.1:41384            127.0.0.1:8000

  8. last指令

  “last”指令顯示的是上次登入使用者的曆史資訊。這個指令通過搜尋檔案“/var/log/wtmp”,顯示logged-in和logged-out及其tty‘s的使用者清單。

1 2 3 4 5 6 7 8 9 10 11 12

#  last

server   pts

/0

:0               Tue Oct 22 12:03   still logged

in

server   tty8         :0               Tue Oct 22 12:02   still logged

in

...

(unknown tty8         :0               Tue Oct 22 12:02 - 12:02  (00:00)   

server   pts

/0

:0               Tue Oct 22 10:33 - 12:02  (01:29)   

server   tty7         :0               Tue Oct 22 10:05 - 12:02  (01:56)   

(unknown tty7         :0               Tue Oct 22 10:04 - 10:05  (00:00)   

reboot   system boot  3.2.0-4-686-pae  Tue Oct 22 10:04 - 12:44  (02:39)   

wtmp begins Fri Oct  4 14:43:17 2007

  9. curl ifconfig.me

  那麼如何得到你的外部IP位址呢?使用google?那麼這個指令就在你的終端輸出你的外部IP位址。

1

# curl ifconfig.me

  注意:你可能沒有按照curl包,你需要 apt/yum來按照包。

  10. tree指令

  以樹式的格式得到目前檔案夾的結構。

1

# tree

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

.

|-- Desktop

|-- Documents

|   `-- 37.odt

|-- Downloads

|   |-- attachments.zip

|   |-- ttf-indic-fonts_0.5.11_all.deb

|   |-- ttf-indic-fonts_1.1_all.deb

|   `-- wheezy-nv-

install

.sh

|-- Music

|-- Pictures

|   |-- Screenshot from 2013-10-22 12:03:49.png

|   `-- Screenshot from 2013-10-22 12:12:38.png

|-- Public

|-- Templates

`-- Videos

10 directories, 23 files

  11. pstree

  這個指令顯示目前運作的所有程序及其相關的子程序,輸出的是類似‘tree’指令的樹狀格式。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

# pstree

init─┬─NetworkManager───{NetworkManager}

├─accounts-daemon───{accounts-daemon}

├─acpi_fakekeyd

├─acpid

├─apache2───10*[apache2]

├─at-spi-bus-laun───2*[{at-spi-bus-laun}]

├─atd

├─avahi-daemon───avahi-daemon

├─bluetoothd

├─colord───{colord}

├─colord-sane───2*[{colord-sane}]

├─console-kit-dae───64*[{console-kit-dae}]

├─

cron

├─cupsd

├─2*[dbus-daemon]

├─dbus-launch

├─dconf-service───2*[{dconf-service}]

├─dovecot─┬─anvil

│         ├─config

│         └─log

├─exim4

├─gconfd-2

├─gdm3─┬─gdm-simple-slav─┬─Xorg

│      │                 ├─gdm-session-wor─┬─x-session-manag─┬─evolution-a+

│      │                 │                 │                 ├─gdu-notific+

│      │                 │                 │                 ├─gnome-scree+

│      │                 │                 │                 ├─gnome-setti+

│      │                 │                 │                 ├─gnome-shell+++

│      │                 │                 │                 ├─nm-applet──+++

│      │                 │                 │                 ├─

ssh

-agent

│      │                 │                 │                 ├─tracker-min+

│      │                 │                 │                 ├─tracker-sto+

│      │                 │                 │                 └─3*[{x-sessi+

│      │                 │                 └─2*[{gdm-session-wor}]

│      │                 └─{gdm-simple-slav}

│      └─{gdm3}

├─6*[getty]

├─gnome-keyring-d───9*[{gnome-keyring-d}]

├─gnome-shell-

cal

───2*[{gnome-shell-

cal

}]

├─goa-daemon───{goa-daemon}

├─gsd-printer───{gsd-printer}

├─gvfs-afc-volume───{gvfs-afc-volume}

12. <空格> 指令

  你在終端上鍵入的每個指令都會記錄到history,也能用history指令重新調用。

  如何騙過history 指令呢?呵呵,你可以輕而易舉地做到。在終端,隻需要在鍵入指令之前輸入一個或多個空格,這樣你的指令就不會被記錄了。

  讓我們體驗一下吧,先在終端嘗試五個常見的 Linux 指令并在指令之前留個空(例如** ls, pwd, uname, echo “hi”** 和 who),然後檢查這些指令是不是記錄在曆史中。

[email protected]:~$  ls
[email protected]:~$  pwd
[email protected]:~$  uname
[email protected]:~$  echo “hi”
[email protected]:~$  who
      

  現在運作 

history

 指令來檢視上面已執行的指令是否已經被記錄了.

[email protected]:~$ history
   40  cd /dev/ 
   41  ls 
   42  dd if=/dev/cdrom1 of=/home/avi/Desktop/squeeze.iso 
   43  ping www.google.com 
   44  su
      

  你看到沒有最後執行的指令沒有被記錄。我們也可以用另一個指令

cat | bash

欺騙history,結果跟上面一樣。

  13. stat 指令

  Linux中的stat指令用來顯示檔案或檔案系統的狀态資訊。當用檔案名作為參數時,stat将會展示檔案的全部資訊。狀态資訊包括檔案 大小、塊、權限、通路時間、修改時間、狀态改變時間等。

[email protected]:~$ stat 34.odt 
  File: `34.odt'
  Size: 28822   Blocks: 64 IO Block: 4096   regular file 
Device: 801h/2049d  Inode: 5030293 Links: 1 
Access: (0644/-rw-r--r--)  Uid: ( 1000/ avi)   Gid: ( 1000/ avi) 
Access: 2013-10-14 00:17:40.000000000 +0530 
Modify: 2013-10-01 15:20:17.000000000 +0530 
Change: 2013-10-01 15:20:17.000000000 +0530
      

  14. <alt> + . 和 <esc> + .

  上面的組合鍵事實上不是一個指令,而是傳遞最後一個指令參數到提示符後的快捷鍵,以輸入指令的倒序方式傳遞指令。按住 Alt或Esc再按一下 “.”。

  15. pv 指令

  在電影裡尤其是好萊塢電影你可能已經看見過模拟文本了,像是在實時輸入文字,你可以用pv指令仿照任何類型模拟風的文本輸出,包括流水線輸出。pv可能沒有在你的系統上安裝,你需要用apt或yum擷取安裝包,然後安裝pv到你的機器。

[email protected]:# echo "Tecmint [dot] com is the world's best website for qualitative Linux article" | pv -qL 20
      

  輸出樣式

Tecmint [dot] com is the world''s best website for qualitative Linux article
      

  16. mount | colum -t

  上面的指令用一個很不錯的格式與規範列出了所有挂載檔案系統。

[email protected]:~$ mount | column -t
      

  輸出樣式

/dev/sda1on  / type  ext3 (rw,errors=remount-ro) 
tmpfson  /lib/init/rw  type  tmpfs(rw,nosuid,mode=0755) 
proc on  /proc type  proc (rw,noexec,nosuid,nodev) 
sysfson  /sys  type  sysfs(rw,noexec,nosuid,nodev) 
udev on  /dev  type  tmpfs(rw,mode=0755) 
tmpfson  /dev/shm  type  tmpfs(rw,nosuid,nodev) 
devpts   on  /dev/pts  type  devpts   (rw,noexec,nosuid,gid=5,mode=620) 
fusectl  on  /sys/fs/fuse/connections  type  fusectl  (rw) 
binfmt_misc  on  /proc/sys/fs/binfmt_misc  type  binfmt_misc  (rw,noexec,nosuid,nodev) 
nfsd on  /proc/fs/nfsd type  nfsd (rw)
      

  17. Ctr+l 指令

  在進行下一步之前,我先問一下,你是如何清理你的終端?呵呵,你會在提示符後鍵入 “clear”。好的。用上面的指令執行清理終端都将成為過去。你隻需要按下Ctr+l,看看它如何立即清理你的終端。

  18. curl 指令

  在指令行下如何檢查你的未讀郵件?這個指令對于工作在沒有圖形界面的伺服器的人佷有用。它會在運作期間再次要求輸入密碼,你不需要在上面一行寫死你的密碼,否則會有其它安全風險。

[email protected]:~$ curl -u [email protected] --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print \t if //; print "$2\n" if /<(title|name)>(.*)<\/\1>/;'
      

  輸出樣式

Enter host password for user '[email protected]': 
Gmail - Inbox for [email protected] 
People offering cars in Delhi - Oct 26 
    Quikr Alerts 
another dependency question 
    Chris Bannister 
    Ralf Mardorf 
    Reco 
    Brian 
    François Patte 
    Curt 
    Siard 
    berenger.morel 
Hi Avishek - Download your Free MBA Brochure Now... 
    Diya 
★Top Best Sellers Of The Week, Take Your Pick★ 
    Timesdeal 
aptitude misconfigure? 
    Glenn English 
Choosing Debian version or derivative to run Wine when resource poor 
    Chris Bannister 
    Zenaan Harkness 
    Curt 
    Tom H 
    Richard Owlett 
    Ralf Mardorf 
    Rob Owens
      

  19.screen 指令

   screen指令能斷開一個會話下的一個長時間運作的程序并能再次連接配接,如有需要,也提供了靈活的指令選項

 要運作一個長時間的程序,我們通常執行

[email protected]:~$ ./long-unix-script.sh
      

  缺乏靈活性,需要使用者持續目前的會話,但是如果我們執行上面的指令是:

[email protected]:~$ screen ./long-unix-script.sh
      

  它能在不同會話間斷開或重連。當一個指令正在執行時按“Ctrl + A”然後再按“d”來斷開。

  重新連接配接運作:

[email protected]:~$ screen -r 4980.pts-0.localhost
      

  注解:在這裡,這個指令的稍後的部分是screen id,你能用‘screen -ls’指令檢視。欲了解更多關于screen指令和它們的用法,請閱讀我們的一些幫助文章:10 個screen指令的示例。

  20. file

  No!寫上面的指令不是筆誤. ‘file’是一個能提供關于檔案類型資訊的指令。

[email protected]:~$ file 34.odt
34.odt: OpenDocument Text
      

  21. id

  上面的指令會列印真正的和有效的使用者群組的id。

[email protected]:~$ id
      

  輸出樣式

uid=1000(avi) gid=1000(avi) 
groups=1000(avi),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),109(netdev),111(bluetooth),117(scanner)      

22. ^foo^bar 指令

  在一個執行個體中運作修改後的最後一個指令。假設我需要運作一個指令‘ls -l‘來詳細列出‘Desktop’目錄下的内容。意外地,你打了‘lls -l‘。是以你需要重新打整個指令或者使用導航鍵編輯前面的指令。當你的指令很長時這個很痛苦。

1 2 3 4 5 6 7 8 9 10 11 12

[email protected]:~

/Desktop

$ lls -l

bash

: lls:

command

not found

[email protected]:~

/Desktop

$ ^lls^

ls

ls

-l

total 7489440

drwxr-xr-x 2 avi  avi       36864 Nov 13  2012 101MSDCF

-rw-r--r-- 1 avi  avi      206833 Nov  5 15:27 1.jpg

-rw-r--r-- 1 avi  avi      158951 Nov  5 15:27 2.jpg

-rw-r--r-- 1 avi  avi       90624 Nov  5 12:59 Untitled 1.doc

  注意:在上面的替換中我們使用“typo(被替換的)original_command(原始指令)”。警告!這個指令可能會非常危險!如果你有意或者無意地打錯了系統指令或者任何像rm -rf那樣的風險指令的話!

  23. > file.txt 指令

  這個指令會重新整理檔案的内容而不需删除然後建立相同的檔案。當我們需要反複輸出,或者在相同的檔案上記錄日志時,這個指令就非常有用。

  我有一個有很多文字的‘test.txt’檔案在我的‘Desktop‘上。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

[email protected]:~

/Desktop

$

cat

test

.txt

Linux

GNU

Debian

Fedora

kali

ubuntu

git

Linus

Torvalds

[email protected]:~

/Desktop

$ >

test

.txt

[email protected]:~

/Desktop

$

cat

test

.txt

  注意:再說一次,這個指令可能很危險!永遠不要嘗試重新整理系統檔案或者某篇日志檔案的内容。如果你這麼做了,你可能會遭遇嚴重的問題!

  24. at 指令

  ‘at‘指令與cron 指令相似也可用于安排一個任務或者在某個特定時刻執行指令。

1

[email protected]:~

/Desktop

$

echo

"ls -l > /dev/pts/0"

| at 14:012

  或

1

[email protected]:~

/Desktop

$

echo

"ls -l > /dev/pts/0"

| at 2:12 PM

  示例輸出

1 2 3 4 5 6 7 8

-rw-r--r-- 1 avi  avi      220492 Nov  1 13:49 Screenshot-1.png

-rw-r--r-- 1 root root        358 Oct 17 10:11 sources.list

-rw-r--r-- 1 avi  avi  4695982080 Oct 10 20:29 squeeze.iso

..

..

-rw-r--r-- 1 avi  avi       90624 Nov  5 12:59 Untitled 1.doc

-rw-r--r-- 1 avi  avi       96206 Nov  5 12:56 Untitled 1.odt

-rw-r--r-- 1 avi  avi        9405 Nov 12 23:22 Untitled.png

  注意:echo “ls -l”的意思是,将這串指令(這裡是 ls -l)輸出在标準終端上。你可以用你需要或者選擇的指令替代‘ls -l‘。

  > :重定向輸出

  /dev/pts/0: 這是輸出裝置和/或檔案,輸出到指定的地方,這裡輸出在終端(/dev/pts/0)。

  就我而言,此時我的tty在/dev/pts/0。你可以用tty指令檢查你的tty。

1 2 3

[email protected]:~

/Desktop

$

tty

/dev/pts/0

  注意: ‘at‘會在系統時間比對到特定時間時會盡快執行。

  25. du -h –max-depth=1 指令

  下面的指令以人類可讀的形式輸出目前目錄的子目錄的大小。

1 2 3 4 5 6 7

[email protected]:

/home/avi/Desktop

# du -h --max-depth=1

38M     .

/test

1.1G    .

/shivji

42M     .

/drupal

6.9G    .

/101MSDCF

16G .

  注意:上面的指令在檢查系統使用率是非常有用。

  26. expr 指令

  ‘expr‘不是那麼鮮為人知的指令。這個指令在終端中計算簡單的算數時非常有用。

1 2 3 4 5 6 7 8 9 10 11

[email protected]:

/home/avi/Desktop

# expr 2 + 3

5

[email protected]:

/home/avi/Desktop

# expr 6 – 3

3

[email protected]:

/home/avi/Desktop

# expr 12 / 3

4

[email protected]:

/home/avi/Desktop

# expr 2 \* 9

18

  27. look 指令

  在終端上從英文字典上查單詞以防混淆。比如說,我記不清了是該拼成carrier還是carieer。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

[email protected]:

/home/avi/Documents

# look car

Cara

Cara's

...

carps

carpus

carpus's

carrel

carrel's

carrels

carriage

carriage's

carriages

carriageway

carriageway's

carried

carrier

carrier's

carriers

carries

...

caryatids

  上面的指令會顯示字典上所有以'car'開頭的單詞。我得到了我想找的。

  28. yes 指令

  另外一個指令在通常基礎上并不會經常使用,但是在腳本語言和系統管理時非常有用。

  這個指令會持續地輸出給定的字元串,直到由你的中斷指令打斷。

1 2 3 4 5 6 7 8 9 10 11 12

[email protected]:~

/Desktop

$

yes

"Tecmint is one of the best site dedicated to Linux, how to"

Tecmint is one of the best site dedicated to Linux, how to

Tecmint is one of the best site dedicated to Linux, how to

Tecmint is one of the best site dedicated to Linux, how to

Tecmint is one of the best site dedicated to Linux, how to

...

Tecmint is one of the best site dedicated to Linux, how to

Tecmint is one of the best site dedicated to Linux, how to

Tecmint is one of the best site dedicated to Linux, how to

  29. factor 指令

  factor實際是一個源于數學的指令。這個指令會輸出所有給定數字的因數。

1 2 3 4 5 6 7 8

[email protected]:~

/Desktop

$ factor 22

22: 2 11

[email protected]:~

/Desktop

$ factor 21

21: 3 7

[email protected]:~

/Desktop

$ factor 11

11: 11

  30. ping -i 60 -a IP_address

  我們都用ping指令檢測伺服器是否連通。我通常ping google,來檢測我是否連接配接到了網際網路。

  當你等待或者持續盯着你的終端等待指令的回應或者等待伺服器的連接配接時,有時是很氣人的。

  一旦伺服器連通就有一個聲音如何(譯注:下面指令是等60秒PING一次)?

1 2 3 4 5

[email protected]:~

/Desktop

$

ping

-i 60 -a www.google.com

PING www.google.com (74.125.200.103) 56(84) bytes of data.

64 bytes from www.google.com (74.125.200.103): icmp_req=1 ttl=44

time

=105 ms

64 bytes from 74.125.200.103: icmp_req=2 ttl=44

time

=281 ms

  注意,當你發現指令不會傳回聲音時。請確定你的系統不是靜音的,聲音已經在‘sound preferences(聲音選項)‘ 中啟用并確定勾選了‘Enable window and window sound‘。

  31. tac 指令

  這個指令很有趣,他會以倒序輸出文本檔案的内容。也就是從最後一行到第一行。

  在home目錄下,我的Documents目錄下有一個35.txt檔案。用cat 指令檢查内容。

1

[email protected]:~

/Documents

$

cat

35.txt

  示例輸出

  1. Linux is built with certain powerful tools, which are unavailable in windows.
  2. One of such important tool is Shell Scripting. Windows however comes with such a tool but as usual it is much weak as compared to it's Linux Counterpart.
  3. Shell scripting/programming makes it possible to execute command(s), piped to get desired output in order to automate day-to-day usages.

  現在用tac指令反轉檔案内容(譯注:當然,我們知道cat反轉過來就是tac)。

1

<code>[email protected]:~

/Documents

$ tac 35.txt <

/code

>

  示例輸出

  1. Shell scripting/programming makes it possible to execute command(s), piped to get desired output in order to automate day-to-day usages.
  2. One of such important tool is Shell Scripting. Windows however comes with such a tool but as usual it is much weak as compared to it's Linux Counterpart.
  3. Linux is built with certain powerful tools, which are unavailable in windows.

原文位址:http://www.admin10000.com/document/3182.html