日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux 新手非常有用的命令

發布時間:2024/3/24 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 新手非常有用的命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.cnblogs.com/felix-/p/4341773.html

?

?

?

Linux 新手非常有用的命令

你打算從Windows換到Linux上來,還是你剛好換到Linux上來?哎喲!!!我說什么呢,是什么原因你就出現我的世界里了。

從我以往的經驗來說,當我剛使用Linux,命令,終端啊什么的,嚇了我一跳。我擔心該記住多少命令,來幫助我完成所有任務。

毫無疑問,在線文檔,書籍,man pages以及社區幫了我一個大忙,但是我還是堅信有那么一篇文章記錄了如何簡單學習和理解命令的秘籍。

這激發了我掌握Linux和使它容易使用的積極性。本文就是通往那里的階梯。

?

1. ls命令

ls命令是列出目錄內容(List Directory Contents)的意思。運行它就是列出文件夾里的內容,可能是文件也可能是文件夾。

1

2

3

4

5

6

7

root@tecmint:~# ls

?

Android-Games???????????????????? Music

Pictures????????????????????????? Public

Desktop?????????????????????????? Tecmint.com

Documents???????????????????????? TecMint-Sync

Downloads???????????????????????? Templates

“ls -l”命令已詳情模式(long listing fashion)列出文件夾的內容。

1

2

3

4

5

6

7

8

9

10

11

root@tecmint:~# ls -l

?

total 40588

drwxrwxr-x 2 ravisaive ravisaive???? 4096 May? 8 01:06 Android Games

drwxr-xr-x 2 ravisaive ravisaive???? 4096 May 15 10:50 Desktop

drwxr-xr-x 2 ravisaive ravisaive???? 4096 May 16 16:45 Documents

drwxr-xr-x 6 ravisaive ravisaive???? 4096 May 16 14:34 Downloads

drwxr-xr-x 2 ravisaive ravisaive???? 4096 Apr 30 20:50 Music

drwxr-xr-x 2 ravisaive ravisaive???? 4096 May? 9 17:54 Pictures

drwxrwxr-x 5 ravisaive ravisaive???? 4096 May? 3 18:44 Tecmint.com

drwxr-xr-x 2 ravisaive ravisaive???? 4096 Apr 30 20:50 Templates

“ls -a”命令會列出文件夾里的所有內容,包括以”.”開頭的隱藏文件。

1

2

3

4

5

6

7

8

9

10

11

12

root@tecmint:~# ls -a

?

.?????????? .gnupg????????? .dbus?????????? .goutputstream-PI5VVW?????? .mission-control

.adobe????????????????? deja-dup??????????????? .grsync???????????????? .mozilla??????????????????? .themes

.gstreamer-0.10???????? .mtpaint??????????????? .thumbnails???????????? .gtk-bookmarks????????????? .thunderbird

.HotShots?????????????? .mysql_history????????? .htaccess?????? .apport-ignore.xml????????? .ICEauthority??????????

.profile??????????????? .bash_history?????????? .icons????????????????? .bash_logout??????????????????? .fbmessenger

.jedit????????????????? .pulse????????????????? .bashrc???????????????? .liferea_1.8??????????????? .pulse-cookie???????????

.Xauthority???? .gconf????????????????? .local??????????????????.Xauthority.HGHVWW????? .cache

.gftp?????????????????? .macromedia???????????? .remmina??????????????? .cinnamon?????????????????????? .gimp-2.8

.ssh????????????????????.xsession-errors??? .compiz???????????????? .gnome????????????????????????? teamviewer_linux.deb?????????

.xsession-errors.old??? .config???????????????? .gnome2???????????????? .zoncolor

注意:在Linux中,文件以“.”開頭的就是隱藏文件,并且每個文件,文件夾,設備或者命令都是以文件對待。ls -l 命令輸出:

  • d (代表了是目錄).
  • rwxr-xr-x?是文件或者目錄對所屬用戶,同一組用戶和其它用戶的權限。
  • 上面例子中第一個ravisaive?代表了文件文件屬于用戶ravisaive
  • 上面例子中的第二個ravisaive代表了文件文件屬于用戶組ravisaive
  • 4096?代表了文件大小為4096字節.
  • May 8 01:06?代表了文件最后一次修改的日期和時間.
  • 最后面的就是文件/文件夾的名字
  • 更多”ls”例子請查看?15 linux中ls命令實例

    ?

    2. lsblk命令

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    root@tecmint:~# lsblk

    ?

    NAME?? MAJ:MIN RM?? SIZE RO TYPE MOUNTPOINT

    sda????? 8:0??? 0 232.9G? 0 disk

    ├─sda1?? 8:1??? 0? 46.6G? 0 part /

    ├─sda2?? 8:2??? 0???? 1K? 0 part

    ├─sda5?? 8:5??? 0?? 190M? 0 part /boot

    ├─sda6?? 8:6??? 0?? 3.7G? 0 part [SWAP]

    ├─sda7?? 8:7??? 0? 93.1G? 0 part /data

    └─sda8?? 8:8??? 0? 89.2G? 0 part /personal

    sr0???? 11:0??? 1? 1024M? 0 rom

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    root@tecmint:~# lsblk -l

    ?

    NAME MAJ:MIN RM?? SIZE RO TYPE MOUNTPOINT

    sda??? 8:0??? 0 232.9G? 0 disk

    sda1?? 8:1??? 0? 46.6G? 0 part /

    sda2?? 8:2??? 0???? 1K? 0 part

    sda5?? 8:5??? 0?? 190M? 0 part /boot

    sda6?? 8:6??? 0?? 3.7G? 0 part [SWAP]

    sda7?? 8:7??? 0? 93.1G? 0 part /data

    sda8?? 8:8??? 0? 89.2G? 0 part /personal

    sr0?? 11:0??? 1? 1024M? 0 rom

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

    ?

    3. md5sum命令

    “md5sum”就是計算和檢驗MD5信息簽名。md5 checksum(通常叫做哈希)使用匹配或者驗證文件的文件的完整性,因為文件可能因為傳輸錯誤,

    磁盤錯誤或者無惡意的干擾等原因而發生改變。

    1

    2

    3

    root@tecmint:~# md5sum teamviewer_linux.deb

    ?

    47790ed345a7b7970fc1f2ac50c97002? teamviewer_linux.deb

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

    ?

    4. 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設備的讀寫速度,消耗幾秒到幾分鐘不等。

    ?

    5. uname命令

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

    1

    2

    3

    root@tecmint:~# uname -a

    ?

    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

  • “Linux“: 機器的內核名
  • “tecmint“: 機器的分支名
  • “3.8.0-19-generic“: 內核發布版本
  • “#30-Ubuntu SMP“: 內核版本
  • “i686“: 處理器架構
  • “GNU/Linux“: 操作系統名
  • 6. history命令

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

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    root@tecmint:~# history

    ?

    ?1? sudo?add-apt-repository ppa:tualatrix/ppa

    ?2? sudo?apt-get update

    ?3? sudo?apt-get install?ubuntu-tweak

    ?4? sudo?add-apt-repository ppa:diesch/testing

    ?5? sudo?apt-get update

    ?6? sudo?apt-get install?indicator-privacy

    ?7? sudo?add-apt-repository ppa:atareao/atareao

    ?8? sudo?apt-get update

    ?9? sudo?apt-get install?my-weather-indicator

    ?10 pwd

    ?11 cd?&& sudo?cp?-r unity/6?/usr/share/unity/

    ?12 cd?/usr/share/unity/icons/

    ?13 cd?/usr/share/unity

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

    1

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

    7. sudo命令

    “sudo”(super user do)命令允許授權用戶執行超級用戶或者其它用戶的命令。通過在sudoers列表的安全策略來指定。

    1

    root@tecmint:~# sudo add-apt-repository ppa:tualatrix/ppa

    注意:sudo 允許用戶借用超級用戶的權限,然而”su”命令實際上是允許用戶以超級用戶登錄。所以sudo比su更安全。
    并不建議使用sudo或者su來處理日常用途,因為它可能導致嚴重的錯誤如果你意外的做錯了事,這就是為什么在linux社區流行一句話:

    “To err is human, but to really foul up everything, you need root password.”

    “人非圣賢孰能無過,但是擁有root密碼就真的萬劫不復了。”

    8. mkdir命令

    “mkdir”(Make directory)命令在命名路徑下創建新的目錄。然而如果目錄已經存在了,那么它就會返回一個錯誤信息”不能創建文件夾,

    文件夾已經存在了”(“cannot create folder, folder already exists”)

    1

    root@tecmint:~# mkdir tecmint

    注意:目錄只能在用戶擁有寫權限的目錄下才能創建。mkdir:不能創建目錄`tecmint`,因為文件已經存在了。

    (上面的輸出中不要被文件迷惑了,你應該記住我開頭所說的-在linux中,文件,文件夾,驅動,命令,腳本都視為文件)

    9. touch 命令

    “touch”命令代表了將文件的訪問和修改時間更新為當前時間。touch命令只會在文件不存在的時候才會創建它。如果文件已經存在了,

    它會更新時間戳,但是并不會改變文件的內容。

    1

    root@tecmint:~# touch tecmintfile

    注意:touch 可以用來在用戶擁有寫權限的目錄下創建不存在的文件。

    10. chmod 命令

    “chmod”命令就是改變文件的模式位。chmod會根據要求的模式來改變每個所給的文件,文件夾,腳本等等的文件模式(權限)。

    在文件(文件夾或者其它,為了簡單起見,我們就使用文件)中存在3中類型的權限

    1

    2

    3

    Read (r)=4

    Write(w)=2

    Execute(x)=1

    所以如果你想給文件只讀權限,就設置為’4′;只寫權限,設置權限為’2′;只執行權限,設置為1; 讀寫權限,就是4+2 = 6, 以此類推。

    現在需要設置3種用戶和用戶組權限。第一個是擁有者,然后是用戶所在的組,最后是其它用戶。

    1

    rwxr-x--x?? abc.sh

    這里root的權限是?rwx讀寫和執行權限),

      所屬用戶組權限是?r-x (只有讀寫權限, 沒有寫權限)

      對于其它用戶權限是 -x(只有只執行權限)

    為了改變它的權限,為擁有者,用戶所在組和其它用戶提供讀,寫,執行權限。

    1

    root@tecmint:~# chmod 777 abc.sh

    三種都只有讀寫權限

    1

    root@tecmint:~# chmod 666 abc.sh

    擁有者用戶有讀寫和執行權限,用戶所在的組和其它用戶只有可執行權限

    1

    root@tecmint:~# chmod 711 abc.sh

    注意:對于系統管理員和用戶來說,這個命令是最有用的命令之一了。在多用戶環境或者服務器上,對于某個用戶,如果設置了文件不可訪問,

    那么這個命令就可以解決,如果設置了錯誤的權限,那么也就提供了為授權的訪問。

    11. chown命令

    “chown”命令就是改變文件擁有者和所在用戶組。每個文件都屬于一個用戶組和一個用戶。在你的目錄下,使用”ls -l”,你就會看到像這樣的東西。

    1

    2

    3

    4

    root@tecmint:~# ls -l

    ?

    drwxr-xr-x 3 server root 4096 May 10 11:14 Binary

    drwxr-xr-x 2 server server 4096 May 13 09:42 Desktop

    在這里,目錄Binary屬于用戶”server”,和用戶組”root”,而目錄”Desktop”屬于用戶“server”和用戶組”server”

    “chown”命令用來改變文件的所有權,所以僅僅用來管理和提供文件的用戶和用戶組授權。

    1

    2

    3

    4

    root@tecmint:~# chown server:server Binary

    ?

    drwxr-xr-x 3?server server 4096?May 10?11:14?Binary

    drwxr-xr-x 2?server server 4096?May 13?09:42?Desktop

    注意:“chown”所給的文件改變用戶和組的所有權到新的擁有者或者已經存在的用戶或者用戶組。

    12. apt命令

    Debian系列以“apt”命令為基礎,“apt”代表了Advanced Package Tool。

    APT是一個為Debian系列系統(Ubuntu,Kubuntu等等)開發的高級包管理器,

    在Gnu/Linux系統上,它會為包自動地,智能地搜索,安裝,升級以及解決依賴。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    root@tecmint:~# apt-get install mplayer

    ?

    Reading package lists... Done

    Building dependency tree??????

    Reading state information... Done

    The following package was automatically installed and is no longer required:

    ??java-wrappers

    Use 'apt-get autoremove'?to remove it.

    The following extra packages will be installed:

    ??esound-common libaudiofile1 libesd0 libopenal-data libopenal1 libsvga1 libvdpau1 libxvidcore4

    Suggested packages:

    ??pulseaudio-esound-compat libroar-compat2 nvidia-vdpau-driver vdpau-driver mplayer-doc netselect fping

    The following NEW packages will be installed:

    ??esound-common libaudiofile1 libesd0 libopenal-data libopenal1 libsvga1 libvdpau1 libxvidcore4 mplayer

    0 upgraded, 9 newly installed, 0 to remove and 8 not upgraded.

    Need to get 3,567 kB of archives.

    After this operation, 7,772 kB of additional disk space will be used.

    Do you want to continue?[Y/n]? y

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    root@tecmint:~# apt-get update

    ?

    Hit http://ppa.launchpad.net raring Release.gpg??????????????????????????????????????????

    Hit http://ppa.launchpad.net raring Release.gpg??????????????????????????????????????????

    Hit http://ppa.launchpad.net raring Release.gpg?????????????????????

    Hit http://ppa.launchpad.net raring Release.gpg?????????????????????

    Get:1 http://security.ubuntu.com raring-security Release.gpg [933 B]

    Hit http://in.archive.ubuntu.com raring Release.gpg??????????????????????????????????????????????????

    Hit http://ppa.launchpad.net raring Release.gpg?????????????????????

    Get:2 http://security.ubuntu.com raring-security Release [40.8 kB]??

    Ign http://ppa.launchpad.net raring Release.gpg?????????????????????????????????????????????????

    Get:3 http://in.archive.ubuntu.com raring-updates Release.gpg [933 B]???????????????????????????

    Hit http://ppa.launchpad.net raring Release.gpg???????????????????????????????????????????????????????????????

    Hit http://in.archive.ubuntu.com raring-backports Release.gpg

    注意:上面的命令會導致系統整體的改變,所以需要root密碼(查看提示符為”#”,而不是“$”).

    和yum命令相比,Apt更高級和智能。

    見名知義,apt-cache用來搜索包中是否包含子包mplayer, apt-get用來安裝,升級所有的已安裝的包到最新版。

    關于apt-get 和 apt-cache命令更多信息,請查看?25 APT-GET和APT-CACHE命令

    13. tar命令

    tar”命令是磁帶歸檔(Tape Archive),對創建一些文件的的歸檔和它們的解壓很有用。

    1

    root@tecmint:~# tar -zxvf abc.tar.gz (記住'z'代表了.tar.gz)

    1

    root@tecmint:~# tar -jxvf abc.tar.bz2 (記住'j'代表了.tar.bz2)

    ?

    1

    root@tecmint:~# tar -cvf archieve.tar.gz(.bz2) /path/to/folder/abc

    注意: “tar.gz“代表了使用gzip歸檔,“bar.bz2”使用bzip壓縮的,它壓縮的更好但是也更慢。

    了解更多”tar 命令”的例子,請查看?18 Tar命名例子

    14. cal 命令

    “cal”(Calender),它用來顯示當前月份或者未來或者過去任何年份中的月份。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    root@tecmint:~# cal

    ?

    May 2013???????

    Su Mo Tu We Th Fr Sa?

    ??????????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

    顯示已經過去的月份,1835年2月

    1

    2

    3

    4

    5

    6

    7

    8

    root@tecmint:~# cal 02?1835

    ?

    ???February 1835?????

    Su Mo Tu We Th Fr Sa?

    ?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

    顯示未來的月份,2145年7月。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    root@tecmint:~# cal 07?2145

    ?

    ?????July 2145???????

    Su Mo Tu We Th Fr Sa?

    ?????????????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

    注意: 你不需要往回調整日歷50年,既不用復雜的數據計算你出生那天,也不用計算你的生日在哪天到來,

    [因為它的最小單位是月,而不是日]。

    15. date命令

    “date”命令使用標準的輸出打印當前的日期和時間,也可以深入設置。

    1

    2

    3

    root@tecmint:~# date

    ?

    Fri May 17?14:13:29?IST 2013

    1

    2

    3

    root@tecmint:~# date --set='14 may 2013 13:57'

    ?

    Mon May 13?13:57:00?IST 2013

    注意:這個命令在腳本中十分有用,以及基于時間和日期的腳本更完美。而且在終端中改變日期和時間,讓你更專業!!!

    (當然你需要root權限才能操作這個,因為它是系統整體改變)

    16. cat命令

    “cat”代表了連結(Concatenation),連接兩個或者更多文本文件或者以標準輸出形式打印文件的內容。

    1

    root@tecmint:~# cat a.txt b.txt c.txt d.txt abcd.txt

    1

    2

    3

    4

    root@tecmint:~# cat abcd.txt

    ....

    contents of file abcd

    ...

    注意:“>>”和“>”調用了追加符號。它們用來追加到文件里,而不是顯示在標準輸出上。“>”符號會刪除已存在的文件,

    然后創建一個新的文件。

    所以因為安全的原因,建議使用“>>”,它會寫入到文件中,而不是覆蓋或者刪除。

    在深入探究之前,我必須讓你知道通配符(你應該知道通配符,它出現在大多數電視選秀中)。

    通配符是shell的特色,和任何GUI文件管理器相比,

    它使命令行更強大有力!如你所看到那樣,在一個圖形文件管理器中,你想選擇一大組文件,你通常不得不使用你的鼠標來選擇它們。

    這可能覺得很簡單,

    但是事實上,這種情形很讓人沮喪!

    例如,假如你有一個有很多很多各種類型的文件和子目錄的目錄,然后你決定移動所有文件名中包含“Linux”字樣的HTML文件到另外一個目錄。

    如何簡單的完成這個?如果目錄中包含了大量的不同名的HTML文件,你的任務很巨大,而不是簡單了。

    在LInux CLI中,這個任務就很簡單,就好像只移動一個HTML文件,因為有shell的通配符,才會如此簡單。這些是特殊的字符,

    允許你選擇匹配某種字符模式的文件名。它幫助你來選擇,即使是大量文件名中只有幾個字符,而且在大多數情形中,

    它比使用鼠標選擇文件更簡單。

    這里就是常用通配符列表:

    1

    2

    3

    4

    5

    6

    7

    8

    Wildcard Matches

    ???*????????????零個或者更多字符

    ????????????????恰好一個字符

    [abcde]?????????????恰好列舉中的一個字符

    ?[a-e]??????????恰好在所給范圍中的一個字符

    [!abcde]????????任何字符都不在列舉中

    [!a-e]??????????任何字符都不在所給的范圍中

    {debian,linux}??????恰好在所給選項中的一整個單詞

    ! 叫做非,帶’!’的反向字符串為真

    更多請閱讀Linux cat 命令的實例?13 Linux中cat命令實例

    17. cp 命令

    “copy”就是復制。它會從一個地方復制一個文件到另外一個地方。

    1

    root@tecmint:~# cp /home/user/Downloads abc.tar.gz /home/user/Desktop (Return 0?when sucess)

    注意:?cp,在shell腳本中是最常用的一個命令,而且它可以使用通配符(在前面一塊中有所描述),來定制所需的文件的復制。

    18. mv 命令

    “mv”命令將一個地方的文件移動到另外一個地方去。

    1

    root@tecmint:~# mv /home/user/Downloads abc.tar.gz /home/user/Desktop (Return 0?when sucess)

    注意:mv?命令可以使用通配符。mv需謹慎使用,因為易懂系統的或者未授權的文件不但會導致安全性問題,而且可能系統崩潰。

    19. pwd 命令

    “pwd”(print working directory),在終端中顯示當前工作目錄的全路徑。

    1

    2

    3

    root@tecmint:~# pwd

    ?

    /home/user/Desktop

    注意: 這個命令并不會在腳本中經常使用,但是對于新手,當從連接到nux很久后在終端中迷失了路徑,這絕對是救命稻草。

    20. cd 命令

    最后,經常使用的“cd”命令代表了改變目錄。它在終端中改變工作目錄來執行,復制,移動,讀,寫等等操作。

    1

    root@tecmint:~# cd /home/user/Desktop

    1

    2

    3

    server@localhost:~$ pwd

    ?

    /home/user/Desktop

    注意: 在終端中切換目錄時,cd就大顯身手了。“cd ~”會改變工作目錄為用戶的家目錄,

    而且當用戶發現自己在終端中迷失了路徑時,非常有用。

    “cd ..”從當前工作目錄切換到(當前工作目錄的)父目錄。

    這些命令肯定會讓你在Linux上很舒服。但是這并不是結束。不久,我就會寫一些其它的針對于中級用戶的有用命令。

    例如,如果你熟練使用這些命令,

    歡呼吧,少年,你會發現你已從小白級別提升為了中級用戶了。在下篇文章,我會介紹像“kill”,”ps”,”grep”等等命令,

    期待吧,我不會讓你失望的。

    對中級 Linux 用戶非常有用的 20 個命令 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

    也許你已經發現第一篇文章非常的有用,這篇文章是繼《對初級Linux用戶非常有用的20個命令》的一個延伸。?

    第一篇文章的目的是為新手準備,而這篇文章則是為了Linux的中高級用戶。在這里你將學會如何進行自定義搜索,知道正在進行的進程和停掉進程,

    如何使用Linux的強勢功能和如何在系統內編譯C、C++和JAVA程序。

    ?

    21.?命令:?Find

    搜索指定目錄下的文件,從開始于父目錄,然后搜索子目錄。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    root@tecmint:~# find -name *.sh

    ?

    ./Desktop/load.sh

    ./Desktop/test.sh

    ./Desktop/shutdown.sh

    ./Binary/firefox/run-mozilla.sh

    ./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh

    ./Downloads/kdewebdev-3.5.8/admin/doxygen.sh

    ./Downloads/kdewebdev-3.5.8/admin/cvs.sh

    ./Downloads/kdewebdev-3.5.8/admin/ltmain.sh

    ./Downloads/wheezy-nv-install.sh

    注意:?`-name‘選項是搜索大小寫敏感。可以使用`-iname‘選項,這樣在搜索中可以忽略大小寫。

    (*是通配符,可以搜索所有的文件;‘.sh‘你可以使用文件名或者文件名的一部分來制定輸出結果)

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    root@tecmint:~# find -iname *.SH ( find -iname *.Sh /? find -iname *.sH)

    ?

    ./Desktop/load.sh

    ./Desktop/test.sh

    ./Desktop/shutdown.sh

    ./Binary/firefox/run-mozilla.sh

    ./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh

    ./Downloads/kdewebdev-3.5.8/admin/doxygen.sh

    ./Downloads/kdewebdev-3.5.8/admin/cvs.sh

    ./Downloads/kdewebdev-3.5.8/admin/ltmain.sh

    ./Downloads/wheezy-nv-install.sh

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    root@tecmint:~# find -name *.tar.gz

    ?

    /var/www/modules/update/tests/aaa_update_test.tar.gz

    ./var/cache/flashplugin-nonfree/install_flash_player_11_linux.i386.tar.gz

    ./home/server/Downloads/drupal-7.22.tar.gz

    ./home/server/Downloads/smtp-7.x-1.0.tar.gz

    ./home/server/Downloads/noreqnewpass-7.x-1.2.tar.gz

    ./usr/share/gettext/archive.git.tar.gz

    ./usr/share/doc/apg/php.tar.gz

    ./usr/share/doc/festival/examples/speech_pm_1.0.tar.gz

    ./usr/share/doc/argyll/examples/spyder2.tar.gz

    ./usr/share/usb_modeswitch/configPack.tar.gz

    注意:以上命令查找根目錄下和所有文件夾以及加載的設備的子目錄下的所有包含‘tar.gz’的文件。

    ’find’命令的更詳細信息請參考35?Find?Command?Examples?in?Linux

    ?

    22.?命令:?grep

    ‘grep‘命令搜索指定文件中包含給定字符串或者單詞的行。舉例搜索‘/etc/passwd‘文件中的‘tecmint’

    1

    2

    3

    root@tecmint:~# grep tecmint /etc/passwd

    ?

    tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash

    使用’-i’選項將忽略大小寫。

    1

    2

    3

    root@tecmint:~# grep -i TECMINT /etc/passwd

    ?

    tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash

    使用’-r’選項遞歸搜索所有自目錄下包含字符串?“127.0.0.1“.的行。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    root@tecmint:~# grep -r "127.0.0.1" /etc/

    ?

    /etc/vlc/lua/http/.hosts:127.0.0.1

    /etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"

    /etc/mysql/my.cnf:bind-address????? = 127.0.0.1

    /etc/apache2/mods-available/status.conf:??? Allow from 127.0.0.1 ::1

    /etc/apache2/mods-available/ldap.conf:??? Allow from 127.0.0.1 ::1

    /etc/apache2/mods-available/info.conf:??? Allow from 127.0.0.1 ::1

    /etc/apache2/mods-available/proxy_balancer.conf:#??? Allow from 127.0.0.1 ::1

    /etc/security/access.conf:#+ : root : 127.0.0.1

    /etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;

    /etc/dhcp/dhclient.conf:#? option domain-name-servers 127.0.0.1;

    /etc/init/network-interface.conf:?? ifconfig lo 127.0.0.1 up || true

    /etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).

    /etc/java-6-openjdk/net.properties:# http.nonProxyHosts=localhost|127.0.0.1

    /etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).

    /etc/java-6-openjdk/net.properties:# ftp.nonProxyHosts=localhost|127.0.0.1

    /etc/hosts:127.0.0.1??? localhost

    注意:您還可以使用以下選項:

    1?-w搜索單詞?(egrep?-w?‘word1|word2‘?/path/to/file).

    2?-c用于統計滿足要求的行?(i.e.,?total?number?of?times?the?pattern?matched)?(grep?-c?‘word‘?/path/to/file).

    3?–color彩色輸出?(grep?–color?server?/etc/passwd).

    ?

    23.?命令:?man

    ‘man‘是系統幫助頁。Man提供命令所有選項及用法的在線文檔。幾乎所有的命令都有它們的幫助頁,例如:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    root@tecmint:~# man man

    ?

    MAN(1)?????????????????????????????????????????????????????????????? Manual pager utils????????????????????????????????????????????????????????????? MAN(1)

    ?

    NAME

    ???????man - an interface to the on-line reference manuals

    ?

    SYNOPSIS

    ???????man? [-C? file]? [-d]? [-D]? [--warnings[=warnings]]? [-R? encoding]? [-L? locale]? [-m? system[,...]]? [-M? path]? [-S list] [-e extension] [-i|-I]

    ???????[--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification]? [-p

    ???????string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...

    ???????man -k [apropos options] regexp ...

    ???????man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...

    ???????man -f [whatis options] page ...

    ???????man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t] [-T[device]]

    ???????[-H[browser]] [-X[dpi]] [-Z] file ...

    ???????man -w|-W [-C file] [-d] [-D] page ...

    ???????man -c [-C file] [-d] [-D] page ...

    ???????man [-hV]

    上面是man命令的系統幫助頁,類似的有cat和ls的幫助頁。

    注意:系統幫助頁是為了命令的使用和學習而設計的。

    ?

    24.?命令:?ps

    ps命令給出正在運行的某個進程的狀態,每個進程有特定的id成為PID。

    1

    2

    3

    4

    5

    root@tecmint:~# ps

    ?

    ?PID TTY????????? TIME CMD

    ?4170 pts/1??? 00:00:00 bash

    ?9628 pts/1??? 00:00:00 ps

    使用‘-A‘選項可以列出所有的進程及其PID。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    root@tecmint:~# ps -A

    ?

    ?PID TTY????????? TIME CMD

    ????1 ???????? 00:00:01 init

    ????2 ???????? 00:00:00 kthreadd

    ????3 ???????? 00:00:01 ksoftirqd/0

    ????5 ???????? 00:00:00 kworker/0:0H

    ????7 ???????? 00:00:00 kworker/u:0H

    ????8 ???????? 00:00:00 migration/0

    ????9 ???????? 00:00:00 rcu_bh

    ....

    注意:當你要知道有哪些進程在運行或者需要知道想殺死的進程PID時ps命令很管用。你可以把它與‘grep‘合用來查詢指定的輸出結果,例如:

    1

    2

    3

    4

    root@tecmint:~# ps -A | grep -i ssh

    ?

    ?1500 ???????? 00:09:58 sshd

    ?4317 ???????? 00:00:00 sshd

    ps命令與grep命令用管道線分割可以得到我們想要的結果。

    ?

    25.?命令:?kill

    也許你從命令的名字已經猜出是做什么的了,kill是用來殺死已經無關緊要或者沒有響應的進程.

    它是一個非常有用的命令,而不是非常非常有用.

    你可能?很熟悉Windows下要殺死進程可能需要頻繁重啟機器因為一個在運行的進程大部分情況下不能夠殺死,

    即使殺死了進程也需要重新啟動操作系統才能生效.

    但?在linux環境下,事情不是這樣的.你可以殺死一個進程并且重啟它而不是重啟整個操作系統.

    殺死一個進程需要知道進程的PID.

    假設你想殺死已經沒有響應的‘apache2′進程,運行如下命令:

    1

    2

    3

    root@tecmint:~# ps -A | grep -i apache2

    ?

    1285 ???????? 00:00:00 apache2

    搜索‘apache2′進程,找到PID并殺掉它.例如:在本例中‘apache2′進程的PID是1285..

    1

    root@tecmint:~# kill 1285 (to kill the process apache2)

    注意:每次你重新運行一個進程或者啟動系統,每個進程都會生成一個新的PID.你可以使用ps命令獲得當前運行進程的PID.

    另一個殺死進程的方法是:

    1

    root@tecmint:~# pkill apache2

    注意:kill需要PID作為參數,pkill可以選擇應用的方式,比如指定進程的所有者等.

    ?

    26.?命令:?whereis

    whereis的作用是用來定位命令的二進制文件\資源\或者幫助頁.舉例來說,獲得ls和kill命令的二進制文件/資源以及幫助頁:

    1

    2

    3

    root@tecmint:~# whereis ls

    ?

    ls: /bin/ls /usr/share/man/man1/ls.1.gz

    1

    2

    3

    root@tecmint:~# whereis kill

    ?

    kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz

    注意:當需要知道二進制文件保存位置時有用.

    ?

    27.?命令:?service

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

    在Ubuntu上啟動apache2?server:

    1

    2

    3

    4

    root@tecmint:~# service apache2 start

    ?

    ?* Starting web server apache2???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

    httpd (pid 1285) already running??????????????????????? [ OK ]

    重啟apache2?server:

    1

    2

    3

    4

    root@tecmint:~# service apache2 restart

    ?

    * Restarting web server apache2?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

    ?... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName? [ OK ]

    停止apache2?server:

    1

    2

    3

    4

    root@tecmint:~# service apache2 stop

    ?

    ?* Stopping web server apache2???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName

    ?... waiting??????????????????????????????????????????????????????????????? [ OK ]

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

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

    ?

    28.?命令:?alias

    alias是一個系統自建的shell命令,允許你為名字比較長的或者經常使用的命令指定別名。

    我經常用‘ls?-l‘命令,它有五個字符(包括空格)。于是我為它創建了一個別名‘l’。

    1

    root@tecmint:~# alias l='ls -l'

    試試它是否能用:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    root@tecmint:~# l

    ?

    total 36

    drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary

    drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop

    drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents

    drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads

    drwxr-xr-x 2 tecmint tecmint 4096 May? 7 16:58 Music

    drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures

    drwxr-xr-x 2 tecmint tecmint 4096 May? 7 16:58 Public

    drwxr-xr-x 2 tecmint tecmint 4096 May? 7 16:58 Templates

    drwxr-xr-x 2 tecmint tecmint 4096 May? 7 16:58 Videos

    去掉’l’別名,要使用unalias命令:

    1

    root@tecmint:~# unalias l

    再試試:

    1

    2

    3

    root@tecmint:~# l

    ?

    bash: l: command not found

    開個玩笑,把一個重要命令的別名指定為另一個重要命令:

    1

    2

    3

    4

    5

    alias cd='ls -l' (set alias of ls -l to cd)

    alias su='pwd' (set alias of pwd to su)

    ....

    (You can create your own)

    ....

    想想多么有趣,現在如果你的朋友敲入‘cd’命令,當他看到的是目錄文件列表而不是改變目錄;當他試圖用’su‘命令時,

    他會進入當前目錄。你可以隨后去掉別名,向他解釋以上情況。

    ?

    29.命令:?df

    報告系統的磁盤使用情況。在跟蹤磁盤使用情況方面對于普通用戶和系統管理員都很有用。?‘df‘?通過檢查目錄大小工作,

    但這一數值僅當文件關閉時才得到更新。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    root@tecmint:~# df

    ?

    Filesystem???? 1K-blocks??? Used Available Use% Mounted on

    /dev/sda1?????? 47929224 7811908? 37675948? 18% /

    none?????????????????? 4?????? 0???????? 4?? 0% /sys/fs/cgroup

    udev???????????? 1005916?????? 4?? 1005912?? 1% /dev

    tmpfs???????????? 202824???? 816??? 202008?? 1% /run

    none??????????????? 5120?????? 0????? 5120?? 0% /run/lock

    none???????????? 1014120???? 628?? 1013492?? 1% /run/shm

    none????????????? 102400????? 44??? 102356?? 1% /run/user

    /dev/sda5???????? 184307?? 79852???? 94727? 46% /boot

    /dev/sda7?????? 95989516?? 61104? 91045676?? 1% /data

    /dev/sda8?????? 91953192?? 57032? 87218528?? 1% /personal

    ‘df’命令的更多例子請參閱?12?df?Command?Examples?in?Linux.

    ?

    30.?命令:?du

    估計文件的空間占用。?逐層統計文件(例如以遞歸方式)并輸出摘要。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    root@tecmint:~# du

    ?

    8?????? ./Daily Pics/wp-polls/images/default_gradient

    8?????? ./Daily Pics/wp-polls/images/default

    32????? ./Daily Pics/wp-polls/images

    8?????? ./Daily Pics/wp-polls/tinymce/plugins/polls/langs

    8?????? ./Daily Pics/wp-polls/tinymce/plugins/polls/img

    28????? ./Daily Pics/wp-polls/tinymce/plugins/polls

    32????? ./Daily Pics/wp-polls/tinymce/plugins

    36????? ./Daily Pics/wp-polls/tinymce

    580???? ./Daily Pics/wp-polls

    1456??? ./Daily Pics

    36????? ./Plugins/wordpress-author-box

    16180?? ./Plugins

    12????? ./May Articles 2013/Xtreme Download Manager

    4632??? ./May Articles 2013/XCache

    注意:?‘df‘?只顯示文件系統的使用統計,但‘du‘統計目錄內容。‘du‘命令的更詳細信息請參閱10?du?(Disk?Usage)?Commands.

    ?

    31.?命令:?rm

    ‘rm’?標準移除命令。?rm?可以用來刪除文件和目錄。

    刪除目錄

    1

    2

    3

    root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0

    ?

    rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory

    ‘rm’不能直接刪除目錄,需要加上相應的’-rf’參數才可以。

    1

    root@tecmint:~# rm -rf PassportApplicationForm_Main_English_V1.0

    警告:?”rm?-rf”?命令是一個破壞性的命令,假如你不小心刪除一個錯誤的目錄。一旦你使用’rm?-rf’?刪除一個目錄,

    在目錄中所有的文件包括目錄本身會被永久的刪除,所以使用這個命令要非常小心。

    ?

    32.?命令:?echo

    echo??的功能正如其名,就是基于標準輸出打印一段文本。它和shell無關,shell也不讀取通過echo命令打印出的內容。

    然而在一種交互式腳本中,echo通過終端將信息傳遞給用戶。它是在腳本語言,交互式腳本語言中經常用到的命令。

    1

    2

    3

    root@tecmint:~# echo "Tecmint.com is a very good website"

    ?

    Tecmint.com is a very good website

    創建一小段交互式腳本

    1.?在桌面上新建一個文件,命名為?‘interactive_shell.sh‘??(記住必須帶?‘.sh‘擴展名)。

    2.?復制粘貼如下腳本代碼,確保和下面的一致。

    1

    2

    3

    4

    #!/bin/bash

    echo "Please enter your name:"

    ???read name

    ???echo "Welcome to Linux $name"

    接下來,設置執行權限并運行腳本。

    1

    root@tecmint:~# chmod 777 interactive_shell.sh

    1

    2

    3

    4

    5

    root@tecmint:~# ./interactive_shell.sh

    ?

    Please enter your name:

    Ravi Saive

    Welcome to Linux Ravi Saive

    注意:?‘#!/bin/bash‘?告訴shell這是一個腳本,并且在腳本首行寫上這句話是個好習慣。.?‘read‘?讀取給定的輸出.

    ?

    33.?命令:?passwd

    這是一個很重要的命令,在終端中用來改變自己密碼很有用。顯然的,因為安全的原因,你需要知道當前的密碼。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    root@tecmint:~# passwd

    ?

    Changing password for tecmint.

    (current) UNIX password: ********

    Enter new UNIX password: ********

    Retype new UNIX password: ********

    Password unchanged?? [這里表示密碼未改變,例如:新密碼=舊密碼]

    Enter new UNIX password: #####

    Retype new UNIX password:#####

    ?

    34.?命令:?lpr

    這個命令用來在命令行上將指定的文件在指定的打印機上打印。

    1

    root@tecmint:~# lpr -P deskjet-4620-series 1-final.pdf

    注意:?”lpq”命令讓你查看打印機的狀態(是開啟狀態還是關閉狀態)和等待打印中的工作(文件)的狀態。

    ?

    35.?命令:?cmp

    比較兩個任意類型的文件并將結果輸出至標準輸出。如果兩個文件相同,?‘cmp‘默認返回0;如果不同,

    將顯示不同的字節數和第一處不同的位置。

    以下面兩個文件為例:

    file1.txt

    1

    2

    3

    root@tecmint:~# cat file1.txt

    ?

    Hi My name is Tecmint

    file2.txt

    1

    2

    3

    root@tecmint:~# cat file2.txt

    ?

    Hi My name is tecmint [dot] com

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

    1

    2

    3

    root@tecmint:~# cmp file1.txt file2.txt

    ?

    file1.txt file2.txt differ: byte 15, line 1

    ?

    36.?命令:?wget

    Wget是用于非交互式(例如后臺)下載文件的免費工具.支持HTTP,?HTTPS,?FTP協議和?HTTP?代理。

    使用wget下載ffmpeg

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    root@tecmint:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2

    ?

    --2013-05-22 18:54:52--? http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2

    Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59

    Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.

    HTTP request sent, awaiting response... 302 Found

    Location: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following]

    --2013-05-22 18:54:54--? http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2

    Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)... 92.46.53.163

    Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80... connected.

    HTTP request sent, awaiting response... 200 OK

    Length: 275557 (269K) [application/octet-stream]

    Saving to: ‘ffmpeg-php-0.6.0.tbz2’

    ?

    100%[===========================================================================>] 2,75,557??? 67.8KB/s?? in 4.0s?

    ?

    2013-05-22 18:55:00 (67.8 KB/s) - ‘ffmpeg-php-0.6.0.tbz2’ saved [275557/275557]

    ?

    37?命令:?mount

    mount?是一個很重要的命令,用來掛載不能自動掛載的文件系統。你需要root權限掛載設備。

    在插入你的文件系統后,首先運行”lsblk”命令,識別出你的設備,然后把分配的設備名記下來。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    root@tecmint:~# lsblk

    ?

    NAME?? MAJ:MIN RM?? SIZE RO TYPE MOUNTPOINT

    sda????? 8:0??? 0 931.5G? 0 disk

    ├─sda1?? 8:1??? 0 923.6G? 0 part /

    ├─sda2?? 8:2??? 0???? 1K? 0 part

    └─sda5?? 8:5??? 0?? 7.9G? 0 part [SWAP]

    sr0???? 11:0??? 1? 1024M? 0 rom

    sdb????? 8:16?? 1?? 3.7G? 0 disk

    └─sdb1?? 8:17?? 1?? 3.7G? 0 part

    從這個輸出上來看,很明顯我插入的是4GB的U盤,因而“sdb1”就是要掛載上來的文件系統。以root用戶操作,

    然后切換到/dev目錄,它是所有文件系統掛載的地方。

    1

    2

    3

    4

    root@tecmint:~# su

    Password:

    ?

    root@tecmint:~# cd /dev

    創建一個任何名字的目錄,但是最好和引用相關。

    1

    2

    ?????

    root@tecmint:~# mkdir usb

    現在將“sdb1”文件系統掛載到“usb”目錄.

    1

    root@tecmint:~# mount /dev/sdb1 /dev/usb

    現在你就可以從終端進入到/dev/usb或者通過X窗口系統從掛載目錄訪問文件。

    是時候讓程序猿見識見識Linux環境是多么豐富了!

    ?

    38.?命令:?gcc

    gcc?是Linux環境下C語言的內建編譯器。下面是一個簡單的C程序,在桌面上保存為Hello.c?(記住必須要有‘.c‘擴展名)。

    1

    2

    3

    4

    5

    6

    #include <stdio.h>

    int main()

    {

    ??printf("Hello world\n");

    ??return 0;

    }

    編譯

    1

    root@tecmint:~# gcc Hello.c

    運行

    1

    2

    3

    root@tecmint:~# ./a.out

    ?

    Hello world

    注意:?編譯C程序時,輸出會自動保存到一個名為“a.out”的新文件,因此每次編譯C程序?“a.out”都會被修改。?

    因此編譯期間最好定義輸出文件名.,這樣就不會有覆蓋輸出文件的風險了。

    用這種方法編譯

    1

    root@tecmint:~# gcc -o Hello Hello.c

    這里‘-o‘將輸出寫到‘Hello‘文件而不是?‘a.out‘。再運行一次。

    1

    2

    3

    root@tecmint:~# ./Hello

    ?

    Hello world

    ?

    39.?命令:?g++

    g++是C++的內建編譯器。下面是一個簡單的C++程序,在桌面上保存為Add.cpp?(記住必須要有‘.cpp‘擴展名)。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    #include <iostream>

    ?

    using namespace std;

    ?

    int main()

    ????{

    ??????????int a;

    ??????????int b;

    ??????????cout<<"Enter first number:\n";

    ??????????cin >> a;

    ??????????cout <<"Enter the second number:\n";

    ??????????cin>> b;

    ??????????cin.ignore();

    ??????????int result = a + b;

    ??????????cout<<"Result is"<<"? "<<result<<endl;

    ??????????cin.get();

    ??????????return 0;

    ?????}

    編譯

    1

    root@tecmint:~# g++ Add.cpp

    運行

    1

    2

    3

    4

    5

    root@tecmint:~# ./a.out

    ?

    Enter first number:

    ...

    ...

    注意:編譯C++程序時,輸出會自動保存到一個名為“a.out”的新文件,因此每次編譯C++程序?“a.out”都會被修改。

    ?因此編譯期間最好定義輸出文件名.,這樣就不會有覆蓋輸出文件的風險了。

    用這種方法編譯

    1

    2

    ?????

    root@tecmint:~# g++ -o Add Add.cpp

    運行

    1

    2

    3

    4

    5

    root@tecmint:~# ./Add

    ?

    Enter first number:

    ...

    ...

    ?

    40.?命令:?java

    Java?是世界上使用最廣泛的編程語言之一.?它也被認為是高效,?安全和可靠的編程語言.?現在大多數基于網絡的服務都使用Java實現.

    拷貝以下代碼到一個文件就可以創建一個簡單的Java程序.?不妨把文件命名為tecmint.java?(記住:?’.java’擴展名是必需的).

    1

    2

    3

    4

    5

    class tecmint {

    ??public static void main(String[] arguments) {

    ????System.out.println("Tecmint ");

    ??}

    }

    用javac編譯tecmint.java

    1

    2

    ?????

    root@tecmint:~# javac tecmint.java

    運行

    1

    root@tecmint:~# java tecmint

    注意:?幾乎所有的Linux發行版都帶有gcc編譯器,?大多數發行版都內建了g++?和?java?編譯器,?有些也可能沒有.

    ?你可以用apt?或?yum?安裝需要的包.

    請留下您寶貴的意見和想看到的文章類型.?稍后,?我會來一個很有趣主題,?關于Linux的一些鮮為人知的知識.

    ?

    對 Linux 專家非常有用的 20 個命令 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

    接下來呢?在這篇文章中我將解釋管理Linux?服務器所需的一些命令。

    41. 命令: ifconfig

    ifconfig用來配置常駐內核的網絡接口信息。在系統啟動必要時用來設置網絡適配器的信息。之后,它通常是只需要在調試時或當系統需要調整時使用。

    檢查活動網絡適配器

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    [avishek@tecmint ~]$ ifconfig

    ?

    eth0????? Link encap:Ethernet? HWaddr 40:2C:F4:EA:CF:0E?

    ??????????inet addr:192.168.1.3? Bcast:192.168.1.255? Mask:255.255.255.0

    ??????????inet6 addr: fe80::422c:f4ff:feea:cf0e/64?Scope:Link

    ??????????UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1

    ??????????RX packets:163843 errors:0 dropped:0 overruns:0 frame:0

    ??????????TX packets:124990 errors:0 dropped:0 overruns:0 carrier:0

    ??????????collisions:0 txqueuelen:1000

    ??????????RX bytes:154389832 (147.2 MiB)? TX bytes:65085817 (62.0 MiB)

    ??????????Interrupt:20 Memory:f7100000-f7120000

    ?

    lo??????? Link encap:Local Loopback?

    ??????????inet addr:127.0.0.1? Mask:255.0.0.0

    ??????????inet6 addr: ::1/128?Scope:Host

    ??????????UP LOOPBACK RUNNING? MTU:16436? Metric:1

    ??????????RX packets:78 errors:0 dropped:0 overruns:0 frame:0

    ??????????TX packets:78 errors:0 dropped:0 overruns:0 carrier:0

    ??????????collisions:0 txqueuelen:0

    ??????????RX bytes:4186 (4.0 KiB)? TX bytes:4186 (4.0 KiB)

    檢查所有的網絡適配器

    “-a”參數用來顯示所有網絡適配器(網卡)的詳細信息,包括那些停用的適配器。

    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

    [avishek@tecmint ~]$ ifconfig?-a

    ?

    eth0????? Link encap:Ethernet? HWaddr 40:2C:F4:EA:CF:0E?

    ??????????inet addr:192.168.1.3? Bcast:192.168.1.255? Mask:255.255.255.0

    ??????????inet6 addr: fe80::422c:f4ff:feea:cf0e/64?Scope:Link

    ??????????UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1

    ??????????RX packets:163843 errors:0 dropped:0 overruns:0 frame:0

    ??????????TX packets:124990 errors:0 dropped:0 overruns:0 carrier:0

    ??????????collisions:0 txqueuelen:1000

    ??????????RX bytes:154389832 (147.2 MiB)? TX bytes:65085817 (62.0 MiB)

    ??????????Interrupt:20 Memory:f7100000-f7120000

    ?

    lo??????? Link encap:Local Loopback?

    ??????????inet addr:127.0.0.1? Mask:255.0.0.0

    ??????????inet6 addr: ::1/128?Scope:Host

    ??????????UP LOOPBACK RUNNING? MTU:16436? Metric:1

    ??????????RX packets:78 errors:0 dropped:0 overruns:0 frame:0

    ??????????TX packets:78 errors:0 dropped:0 overruns:0 carrier:0

    ??????????collisions:0 txqueuelen:0

    ??????????RX bytes:4186 (4.0 KiB)? TX bytes:4186 (4.0 KiB)

    ?

    virbr0??? Link encap:Ethernet? HWaddr 0e:30:a3:3a:bf:03?

    ??????????inet addr:192.168.122.1? Bcast:192.168.122.255? Mask:255.255.255.0

    ??????????UP BROADCAST MULTICAST? MTU:1500? Metric:1

    ??????????RX packets:0 errors:0 dropped:0 overruns:0 frame:0

    ??????????TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

    ??????????collisions:0 txqueuelen:0

    ??????????RX bytes:0 (0.0 B)? TX bytes:0 (0.0 B)

    停用網絡適配器

    1

    [avishek@tecmint ~]$ ifconfig?eth0 down

    啟用網絡適配器

    1

    [avishek@tecmint ~]$ ifconfig?eth0 up

    指定IP地址到網絡適配器

    為網絡適配器eth0設定IP地址“192.168.1.12”.

    1

    [avishek@tecmint ~]$ ifconfig?eth0 192.168.1.12

    更改網絡適配器eth0的子網掩碼

    1

    [avishek@tecmint ~]$ ifconfig?eth0 netmask 255.255.255.

    更改網絡適配器eth0的廣播地址

    1

    [avishek@tecmint ~]$ ifconfig?eth0 broadcast 192.168.1.255

    為網絡適配器eth0指定IP地址,子網掩碼,廣播地址

    1

    [avishek@tecmint ~]$ ifconfig?eth0 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255

    Note:如果你設置一塊無線網卡的信息,你可以使用的命令是“iwconfig”.欲知更多ifconfig命令的例子和使用方法,

    讀“15個有用的ifconfig 命令”.

    ?

    42. 命令: netstat

    netstat命令顯示各種網絡相關的信息,如網絡連接,路由表,接口統計,偽裝連接,組播成員身份等….

    列出所有的網絡端口

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    [avishek@tecmint ~]$ netstat?-a

    ?

    Active UNIX domain sockets (servers and established)

    Proto RefCnt Flags?????? Type?????? State???????? I-Node?? Path

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 741379?? /run/user/user1/keyring-I5cn1c/gpg

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 8965???? /var/run/acpid.socket

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 18584??? /tmp/.X11-unix/X0

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 741385?? /run/user/user1/keyring-I5cn1c/ssh

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 741387?? /run/user/user1/keyring-I5cn1c/pkcs11

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 20242??? @/tmp/dbus-ghtTjuPN46

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 13332??? /var/run/samba/winbindd_privileged/pipe

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 13331??? /tmp/.winbindd/pipe

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 11030??? /var/run/mysqld/mysqld.sock

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 19308??? /tmp/ssh-qnZadSgJAbqd/agent.3221

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 436781?? /tmp/HotShots

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 46110??? /run/user/ravisaive/pulse/native

    unix? 2????? [ ACC ]???? STREAM???? LISTENING???? 19310??? /tmp/gpg-zfE9YT/S.gpg-agent

    ....

    顯示所有tcp相關端口

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    [avishek@tecmint ~]$ netstat?-at

    ?

    Active Internet connections (servers and established)

    Proto Recv-Q Send-Q Local Address?????????? Foreign Address???????? State?????

    tcp??????? 0????? 0 localhost:mysql???????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 *:5901????????????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 *:5902????????????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 *:x11-1???????????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 *:x11-2???????????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 *:5938????????????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 localhost:5940????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 ravisaive-OptiPl:domain *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 ravisaive-OptiPl:domain *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 localhost:ipp?????????? *:*???????????????????? LISTEN????

    tcp??????? 0????? 0 ravisaive-OptiPle:48270 ec2-23-21-236-70.c:http ESTABLISHED

    tcp??????? 0????? 0 ravisaive-OptiPle:48272 ec2-23-21-236-70.c:http TIME_WAIT?

    tcp??????? 0????? 0 ravisaive-OptiPle:48421 bom03s01-in-f22.1:https ESTABLISHED

    tcp??????? 0????? 0 ravisaive-OptiPle:48269 ec2-23-21-236-70.c:http ESTABLISHED

    tcp??????? 0????? 0 ravisaive-OptiPle:39084 channel-ecmp-06-f:https ESTABLISHED

    ...

    顯示所有連接的統計信息

    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

    [avishek@tecmint ~]$ netstat?-s

    ?

    Ip:

    ????4994239 total packets received

    ????0 forwarded

    ????0 incoming packets discarded

    ????4165741 incoming packets delivered

    ????3248924 requests sent out

    ????8 outgoing packets dropped

    Icmp:

    ????29460 ICMP messages received

    ????566 input ICMP message failed.

    ????ICMP input histogram:

    ????????destination unreachable: 98

    ????????redirects: 29362

    ????2918 ICMP messages sent

    ????0 ICMP messages failed

    ????ICMP output histogram:

    ????????destination unreachable: 2918

    IcmpMsg:

    ????????InType3: 98

    ????????InType5: 29362

    ????????OutType3: 2918

    Tcp:

    ????94533 active connections openings

    ????23 passive connection openings

    ????5870 failed connection attempts

    ????7194 connection resets received

    ....

    好的!由于某些原因如果你不想解析netstat 輸出的主機、端口和用戶名稱的話 。

    1

    [avishek@tecmint ~]$ netstat?-an

    好,你可能需要獲取的 netstat 持續輸出的動態信息,通過傳遞中斷輸出指令 (ctrl + c)來停止。

    1

    [avishek@tecmint ~]$ netstat?-c

    更多關于“netstat”的例子和使用方法,瀏覽文章“20個netstat 的使用案例”。

    ?

    43. 命令: nslookup

    網絡實用程序,用于獲得互聯網服務器的信息。顧名思義,該實用程序將發現通過查詢 DNS 域的名稱服務器信息。

    1

    2

    3

    4

    5

    6

    7

    8

    [avishek@tecmint ~]$ nslookup?tecmint.com

    ?

    Server:???? 192.168.1.1

    Address:??? 192.168.1.1#53

    ?

    Non-authoritative answer:

    Name:?? tecmint.com

    Address: 50.16.67.239

    查詢郵件交換器記錄

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    [avishek@tecmint ~]$ nslookup?-query=mx tecmint.com

    ?

    Server:???? 192.168.1.1

    Address:??? 192.168.1.1#53

    ?

    Non-authoritative answer:

    tecmint.com mail exchanger = 0 smtp.secureserver.net.

    tecmint.com mail exchanger = 10 mailstore1.secureserver.net.

    ?

    Authoritative answers can be found from:

    查詢域名服務器

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    [avishek@tecmint ~]$ nslookup?-type=ns tecmint.com

    ?

    Server:???? 192.168.1.1

    Address:??? 192.168.1.1#53

    ?

    Non-authoritative answer:

    tecmint.com nameserver = ns3404.com.

    tecmint.com nameserver = ns3403.com.

    ?

    Authoritative answers can be found from:

    查詢DNS記錄

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    [avishek@tecmint ~]$ nslookup?-type=any tecmint.com

    ?

    Server:???? 192.168.1.1

    Address:??? 192.168.1.1#53

    ?

    Non-authoritative answer:

    tecmint.com mail exchanger = 10 mailstore1.secureserver.net.

    tecmint.com mail exchanger = 0 smtp.secureserver.net.

    tecmint.com nameserver = ns06.domaincontrol.com.

    tecmint.com nameserver = ns3404.com.

    tecmint.com nameserver = ns3403.com.

    tecmint.com nameserver = ns05.domaincontrol.com.

    ?

    Authoritative answers can be found from:

    查詢起始授權機構

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    [avishek@tecmint ~]$ nslookup?-type=soa tecmint.com

    ?

    Server:???? 192.168.1.1

    Address:??? 192.168.1.1#53

    ?

    Non-authoritative answer:

    tecmint.com

    ????origin = ns3403.hostgator.com

    ????mail addr = dnsadmin.gator1702.hostgator.com

    ????serial = 2012081102

    ????refresh = 86400

    ????retry = 7200

    ????expire = 3600000

    ????minimum = 86400

    ?

    Authoritative answers can be found from:

    查詢端口號

    更改使用你想要連接的端口號

    1

    2

    3

    4

    5

    6

    7

    [avishek@tecmint ~]$ nslookup?-port 56 tecmint.com

    ?

    Server:???? tecmint.com

    Address:??? 50.16.76.239#53

    ?

    Name:?? 56

    Address: 14.13.253.12

    44.?命令:?dig

    dig是查詢DNS 域名服務器的工具,可以查詢的主機地址、 郵件交流、 域名服務器相關的信息。在任何 Linux (Unix)

    或 Macintosh OS X 操作系統上,都可以使用該工具。dig的最典型的用法是單個主機的查詢。

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ dig?tecmint.com

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com

    ;; global options: +cmd

    ;; Got answer:

    ;; ->>HEADER<

    關閉注釋行

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    [avishek@tecmint ~]$ dig?tecmint.com +nocomments

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +nocomments

    ;; global options: +cmd

    ;tecmint.com.?????????? IN? A

    tecmint.com.??????? 14400?? IN? A?? 40.216.66.239

    ;; Query time: 418 msec

    ;; SERVER: 192.168.1.1#53(192.168.1.1)

    ;; WHEN: Sat Jun 29 13:53:22 2013

    ;; MSG SIZE? rcvd: 45

    關閉認證塊

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ dig?tecmint.com +noauthority

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noauthority

    ;; global options: +cmd

    ;; Got answer:

    ;; ->>HEADER<

    關閉 其他 塊

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ dig??tecmint.com +noadditional

    ?

    ; <<>> DiG 9.9.2-P1 <<>> tecmint.com +noadditional

    ;; global options: +cmd

    ;; Got answer:

    ;; ->>HEADER<

    關閉 統計塊

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ dig?tecmint.com +nostats

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +nostats

    ;; global options: +cmd

    ;; Got answer:

    ;; ->>HEADER<

    關閉回復塊

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ dig?tecmint.com +noanswer

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noanswer

    ;; global options: +cmd

    ;; Got answer:

    ;; ->>HEADER<

    關閉所有塊

    1

    2

    3

    4

    [avishek@tecmint ~]$ dig?tecmint.com +noall

    ?

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6 <<>> tecmint.com +noall

    ;; global options: +cmd

    10?LinuxDig?命令實例

    45.命令: uptime

    你連接到你的Linux?服務器時發現一些不尋常或惡意的東西,你會做什么?猜測……不,絕不!

    你可以運行uptime來驗證當服務器無人值守式到底發生了什么事情。

    1

    2

    3

    [avishek@tecmint ~]$ uptime

    ?

    14:37:10 up? 4:21,? 2 users,? load average: 0.00, 0.00, 0.04

    46. 命令: wall

    對系統管理員來說一個最重要的命令.wall發送一條消息到大家登錄端將其mesg權限設置為”yes”。這條信息可以被wall作為參數,

    或者可以將它作為wall的標準輸入。

    1

    2

    3

    4

    5

    [avishek@tecmint ~]$ wall "we will be going down for maintenance for one hour sharply at 03:30 pm"

    ?

    Broadcast message from root@localhost.localdomain (pts/0) (Sat Jun 29 14:44:02 2013):

    ?

    we will be going down for?maintenance for?one hour sharply at 03:30 pm

    47. 命令: mesg

    其他人們可以使用”wtrite”命令,將在在向您發送文本到屏幕上。你可以控制是否顯示。

    1

    mesg [<strong>n</strong>|<strong>y</strong>] <strong>n</strong> - prevents the message from others popping up on the screen. <strong>y</strong> – Allows messages to appear on your screen.

    48. 命令: write

    如果 ‘mesg’ 是 ‘y’,讓你的文本直接發送到另一臺 Linux 機器的屏幕。.

    1

    [avishek@tecmint ~]$ write ravisaive

    49. 命令: talk

    增強的write命令,talk命令可讓你與其他登錄的用戶交談。

    1

    [avishek@tecmint ~]$ talk ravisaive

    注釋: 如果talk命令沒安裝的話,可以通過apt或yum 安裝所需的包.

    1

    2

    3

    [avishek@tecmint ~]$ yum install?talk

    OR

    [avishek@tecmint ~]$ apt-get install?talk

    50. 命令:w

    是否覺得命令’w'很滑稽?但是事實上不是的。它是一個命令,盡管只有一個字符長!命令”w“是uptimewho命令,

    以前后的順序組合在一起。

    1

    2

    3

    4

    5

    6

    7

    [avishek@tecmint ~]$ w

    ?

    15:05:42 up? 4:49,? 3 users,? load average: 0.02, 0.01, 0.00

    USER???? TTY????? FROM????????????? LOGIN@?? IDLE?? JCPU?? PCPU WHAT

    server?? tty7???? :0?????????????? 14:06??? 4:43m? 1:42?? 0.08s pam: gdm-passwo

    server?? pts/0????:0.0???????????? 14:18??? 0.00s? 0.23s? 1.65s gnome-terminal

    server?? pts/1????:0.0???????????? 14:47??? 4:43?? 0.01s? 0.01s bash

    51. 命令: rename

    見名知意,這個命令重命名文件。rename將會通過從文件名的首字符開始替換,重命名為指定的文件名。

    1

    Give the file?names a1, a2, a3, a4.....1213

    僅僅寫這些命令:[@Lesus 注: 在Ubuntu上不支持這種格式, rename與mv不同的是,rename可以批量修改,如同帶了while的mv操作。]

    1

    2

    rename a1 a0 a

    rename a1 a0 a

    52. 命令: top

    顯示CPU進程信息。這個命令自動刷新,默認是持續顯示CPU進程信息,除非使用了中斷指令。

    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

    [avishek@tecmint ~]$ top

    ?

    top?- 14:06:45 up 10 days, 20:57,? 2 users,? load average: 0.10, 0.16, 0.21

    Tasks: 240 total,?? 1 running, 235 sleeping,?? 0 stopped,?? 4 zombie

    %Cpu(s):? 2.0 us,? 0.5 sy,? 0.0 ni, 97.5 id,? 0.0 wa,? 0.0 hi,? 0.0 si,? 0.0 st

    KiB Mem:?? 2028240 total,? 1777848 used,?? 250392 free,??? 81804 buffers

    KiB Swap:? 3905532 total,?? 156748 used,? 3748784 free,?? 381456 cached

    ?

    ??PID USER????? PR? NI? VIRT? RES? SHR S? %CPU %MEM??? TIME+ COMMAND???????????????????????????????????????????????????????????????????????????????????????????????????????????

    23768 ravisaiv? 20?? 0 1428m 571m? 41m S?? 2.3 28.9? 14:27.52 firefox???????????????????????????????????????????????????????????????????????????????????????????????????????????

    24182 ravisaiv? 20?? 0? 511m 132m? 25m S?? 1.7? 6.7?? 2:45.94 plugin-containe???????????????????????????????????????????????????????????????????????????????????????????????????

    26929 ravisaiv? 20?? 0? 5344 1432? 972 R?? 0.7? 0.1?? 0:00.07 top???????????????????????????????????????????????????????????????????????????????????????????????????????????????

    24875 ravisaiv? 20?? 0? 263m? 14m? 10m S?? 0.3? 0.7?? 0:02.76 lxterminal????????????????????????????????????????????????????????????????????????????????????????????????????????

    ????1 root????? 20?? 0? 3896 1928 1228 S?? 0.0? 0.1?? 0:01.62 init??????????????????????????????????????????????????????????????????????????????????????????????????????????????

    ????2 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.06 kthreadd??????????????????????????????????????????????????????????????????????????????????????????????????????????

    ????3 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:17.28 ksoftirqd/0???????????????????????????????????????????????????????????????????????????????????????????????????????

    ????5 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kworker/0:0H??????????????????????????????????????????????????????????????????????????????????????????????????????

    ????7 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kworker/u:0H??????????????????????????????????????????????????????????????????????????????????????????????????????

    ????8 root????? rt?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.12 migration/0???????????????????????????????????????????????????????????????????????????????????????????????????????

    ????9 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 rcu_bh????????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???10 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:26.94 rcu_sched?????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???11 root????? rt?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:01.95 watchdog/0????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???12 root????? rt?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:02.00 watchdog/1????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???13 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:17.80 ksoftirqd/1???????????????????????????????????????????????????????????????????????????????????????????????????????

    ???14 root????? rt?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.12 migration/1???????????????????????????????????????????????????????????????????????????????????????????????????????

    ???16 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kworker/1:0H??????????????????????????????????????????????????????????????????????????????????????????????????????

    ???17 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 cpuset????????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???18 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 khelper???????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???19 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kdevtmpfs?????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???20 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 netns?????????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???21 root????? 20?? 0???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.04 bdi-default???????????????????????????????????????????????????????????????????????????????????????????????????????

    ???22 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kintegrityd???????????????????????????????????????????????????????????????????????????????????????????????????????

    ???23 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 kblockd???????????????????????????????????????????????????????????????????????????????????????????????????????????

    ???24 root?????? 0 -20???? 0??? 0??? 0 S?? 0.0? 0.0?? 0:00.00 ata_sff

    另查看12 TOP命令例子·[@Lesus 注:htop比top命令更好用,不過需要自己安裝

    ?

    53. 命令: mkfs.ext4

    這個命令在指定的設備上創建一個新的ext4文件系統,如果這個命令后面跟的是個錯誤的設備,那么整個設備就會被擦除和格式化,

    所以建議不要運行這個命令,除非你清楚自己正在干什么。

    1

    2

    Mkfs.ext4 /dev/sda1?(sda1 block will be formatted)

    mkfs.ext4 /dev/sdb1?(sdb1 block will be formatted)

    ?

    54. vi/emac/nano 命令

    vi(visual),emac,nano是 linux 中最常用的一些編輯器。它們經常用于編輯文本,配置,… 等文件.

    A quick guide to work around vi and nano is, emac is a.

    vi 編輯器:

    1

    2

    [avishek@tecmint ~]$ touch?a.txt (創建一個名為a.txt的文本文件)

    [avishek@tecmint ~]$ vi?a.txt (用vi打開a.txt)

    [按下‘i’鍵進入插入模式, 否則你不能輸入任何內容]

    1

    echo?"Hello"??(這里的文本會存到文件中)

  • alt+x(退出插入模式, 記得在最后的字符間留有一些空格.
  • ctrl+x命令或你上一個單詞將被刪除).
  • :wq!(以當前的文本保存文件, 記住‘!’ 是覆蓋的意思).
  • nano 編輯器:

    1

    [avishek@tecmint ~]$ nano a.txt (用nano打開 a.txt)

    1

    edit, with the content, required

    ctrl +x (關閉編輯器).它會顯示如下的提示輸出信息:

    1

    2

    3

    Save modified buffer (ANSWERING "No"?WILL DESTROY CHANGES)????????????????????

    ?Y Yes

    ?N No?????????? ^C Cancel

    點擊‘y’ 選擇 yes 并輸入文件名,就完成編輯了.

    ?

    55. 命令: rsync

    Rsync復制文件,參數-P開啟進度條。如果你已經安裝了rsync,你可以使用一個簡單的別名。

    1

    alias?cp='rsync -aP'

    現在嘗試在終端復制一個大文件,這樣將會看到顯示剩余部分的輸出,與進度條類似。

    而且,保持和維護備份是系統管理員不得不做的最重要、最無聊的工作之一。Rsync是一個用于新建和維護備份的非常好用的終端工具

    (也存在許多其它工具)。

    1

    2

    3

    4

    5

    6

    7

    [avishek@tecmint ~]$ rsync?-zvr IMG_5267\ copy\=33\ copy\=ok.jpg ~/Desktop/

    ?

    sending incremental file?list

    IMG_5267 copy=33 copy=ok.jpg

    ?

    sent 2883830 bytes? received 31 bytes? 5767722.00 bytes/sec

    total size is 2882771? speedup is 1.00

    注意:-z表示壓縮, -v表示詳細信息,-r表示遞歸。

    ?

    56. 命令: free

    跟蹤內存的使用和資源一樣重要,就像管理員執行的任何其它任務,可以使用 'free' 命令來在這里救援.

    當前內存使用狀態Current Usage Status of Memory

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:?????? 2028240??? 1788272???? 239968????????? 0????? 69468???? 363716

    -/+ buffers/cache:??? 1355088???? 673152

    Swap:????? 3905532???? 157076??? 3748456

    設置輸出單位為KB,MB或GB

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free?-b

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:??? 2076917760 1838272512? 238645248????????? 0?? 71348224? 372670464

    -/+ buffers/cache: 1394253824? 682663936

    Swap:?? 3999264768? 160845824 3838418944

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free?-k

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:?????? 2028240??? 1801484???? 226756????????? 0????? 69948???? 363704

    -/+ buffers/cache:??? 1367832???? 660408

    Swap:????? 3905532???? 157076??? 3748456

    ?

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free?-m

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:????????? 1980?????? 1762??????? 218????????? 0???????? 68??????? 355

    -/+ buffers/cache:?????? 1338??????? 641

    Swap:???????? 3813??????? 153?????? 3660

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free?-g

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:???????????? 1????????? 1????????? 0????????? 0????????? 0????????? 0

    -/+ buffers/cache:????????? 1????????? 0

    Swap:??????????? 3????????? 0????????? 3

    以可讀的格式顯示,檢查當前內存使用

    1

    2

    3

    4

    5

    6

    [avishek@tecmint ~]$ free?-h

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:????????? 1.9G?????? 1.7G?????? 208M???????? 0B??????? 68M?????? 355M

    -/+ buffers/cache:?????? 1.3G?????? 632M

    Swap:???????? 3.7G?????? 153M?????? 3.6G

    設定時間間隔后,持續檢查使用狀態

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    [avishek@tecmint ~]$ free?-s 3

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:?????? 2028240??? 1824096???? 204144????????? 0????? 70708???? 364180

    -/+ buffers/cache:??? 1389208???? 639032

    Swap:????? 3905532???? 157076??? 3748456

    ?

    ?????????????total?????? used?????? free?????shared??? buffers???? cached

    Mem:?????? 2028240??? 1824192???? 204048????????? 0????? 70716???? 364212

    -/+ buffers/cache:??? 1389264???? 638976

    Swap:????? 3905532???? 157076??? 3748456

    57. mysqldump 命令

    好了,現在你從名字上就能明白這個命令所代表的作用。mysqldump命令會轉儲(備份)數據庫的全部或特定一部分數據到一個給定的文件中。

    例如:

    1

    [avishek@tecmint ~]$ mysqldump -u root -p --all-databases > /home/server/Desktop/backupfile.sql

    注意:mysqldump需要 mysql 在運行中并且有正確的授權密碼。我們在用mysqldump命令備份數據庫中討論了一些有用的 “mysqldump” 命令用法。

    58. mkpasswd 命令

    根據指定的長度,產生一個難猜的隨機密碼。

    1

    2

    3

    [avishek@tecmint ~]$ mkpasswd -l 10

    ?

    zI4+Ybqfx9

    1

    2

    3

    [avishek@tecmint ~]$ yum install?expect

    [avishek@tecmint ~]$ apt-get install?expect

    注意:-l 10產生一個10個字符的隨機密碼,而-l 20產生20個字符的密碼,它可以設置為任意長度來取得所希望的結果。

    這個命令很有用,經常在腳本語言里使用來產生隨機的密碼。你可能需要yumapt‘expect’ 包來使用這個命令。

    1

    2

    3

    [avishek@tecmint ~]$ yum install?expect

    [avishek@tecmint ~]$ apt-get install?expect

    59. paste 命令

    合并兩個或多個文本文件,按行來進行合并。示例。如果 file1 的內容是:

    1

    2

    3

    1

    2

    3

    file2 是這樣的:

    1

    2

    3

    4

    a

    b

    c

    d

    1

    [avishek@tecmint ~]$ paste?file1 file2 > file3

    結果file3將是:

    1

    2

    3

    4

    1??? a

    2??? b

    3??? c

    ?????d

    60.lsof 命令

    lsof 是"list open files("列表中打開的文件") 的縮寫,顯示您的系統當前已打開的所有文件。這是非常有用的對于想找出哪些進程使用某一特定文件,

    或顯示為單個進程打開所有文件。一些有用的10 個lsof 命令示例,你可能會感興趣閱讀。

    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

    [avishek@tecmint ~]$ lsof

    ?

    COMMAND???? PID?? TID??????????? USER?? FD????? TYPE???? DEVICE SIZE/OFF???????NODE NAME

    init????????? 1????????????????? root? cwd?????? DIR??????? 8,1???? 4096????????? 2 /

    init????????? 1????????????????? root? rtd?????? DIR??????? 8,1???? 4096????????? 2 /

    init????????? 1????????????????? root? txt?????? REG??????? 8,1?? 227432???? 395571 /sbin/init

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 47080???? 263023 /lib/i386-linux-gnu/libnss_files-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 42672???? 270178 /lib/i386-linux-gnu/libnss_nis-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 87940???? 270187 /lib/i386-linux-gnu/libnsl-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 30560???? 263021 /lib/i386-linux-gnu/libnss_compat-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1?? 124637???? 270176 /lib/i386-linux-gnu/libpthread-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1? 1770984???? 266166 /lib/i386-linux-gnu/libc-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 30696???? 262824 /lib/i386-linux-gnu/librt-2.17.so

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 34392???? 262867 /lib/i386-linux-gnu/libjson.so.0.1.0

    init????????? 1????????????????? root? mem?????? REG??????? 8,1?? 296792???? 262889 /lib/i386-linux-gnu/libdbus-1.so.3.7.2

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 34168???? 262840 /lib/i386-linux-gnu/libnih-dbus.so.1.0.0

    init????????? 1????????????????? root? mem?????? REG??????? 8,1??? 95616???? 262848 /lib/i386-linux-gnu/libnih.so.1.0.0

    init????????? 1????????????????? root? mem?????? REG??????? 8,1?? 134376???? 270186 /lib/i386-linux-gnu/ld-2.17.so

    init????????? 1????????????????? root??? 0u????? CHR??????? 1,3????? 0t0?????? 1035 /dev/null

    init????????? 1????????????????? root??? 1u????? CHR??????? 1,3????? 0t0?????? 1035 /dev/null

    init????????? 1????????????????? root??? 2u????? CHR??????? 1,3????? 0t0?????? 1035 /dev/null

    init????????? 1????????????????? root??? 3r???? FIFO??????? 0,8????? 0t0?????? 1714 pipe

    init????????? 1????????????????? root??? 4w???? FIFO??????? 0,8????? 0t0?????? 1714 pipe

    init????????? 1????????????????? root??? 5r???? 0000??????? 0,9??????? 0?????? 6245 anon_inode

    init????????? 1????????????????? root??? 6r???? 0000??????? 0,9??????? 0?????? 6245 anon_inode

    init????????? 1????????????????? root??? 7u???? unix 0xf5e91f80????? 0t0?????? 8192 @/com/ubuntu/upstart

    init????????? 1????????????????? root??? 8w????? REG??????? 8,1???? 3916??????? 394 /var/log/upstart/teamviewerd.log.1 (deleted)

    這里并沒有結束,系統管理員會很多東西,為你提供漂亮的界面,服務于你的工作。系統管理實際上是學習和實現的一門優雅的藝術。

    我們會盡力給你介紹 linux 專業人員必須了解的知識以及一些其他必要東西,linux本身是基礎的,簡單的。深入理解是不斷學習的過程。

    你的美言好詞總是在不斷鼓勵我們寫出更多的優秀,豐富的文章。"喜之以分享,助我來傳播"。

    總結

    以上是生活随笔為你收集整理的Linux 新手非常有用的命令的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。