歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> apt-get 和 apt-cache 命令實例展示

apt-get 和 apt-cache 命令實例展示

日期:2017/2/28 14:22:25   编辑:Linux教程

apt-get和apt-cache是Ubuntu Linux中的命令行下的包管理工具。 apt-get的GUI版本是Synaptic包管理器。本篇中我們會展示apt-get和apt-cache命令的15個不同例子。

示例:1 列出所有可用包

  1. linuxidc@localhost:~$ apt-cache pkgnames
  2. account-plugin-yahoojp
  3. ceph-fuse
  4. dvd+rw-tools
  5. e3
  6. gnome-commander-data
  7. grub-gfxpayload-lists
  8. gweled
  9. .......................................

示例:2 用關鍵字搜索包

這個命令在你不確定包名時很有用,只要在apt-cache(LCTT 譯注:這裡原文是apt-get,應為筆誤)後面輸入與包相關的關鍵字即可。

  1. linuxidc@localhost:~$ apt-cache search "web server"
  2. apache2 -Apache HTTP Server
  3. apache2-bin -Apache HTTP Server(binary files and modules)
  4. apache2-data -Apache HTTP Server(common files)
  5. apache2-dbg -Apache debugging symbols
  6. apache2-dev -Apache HTTP Server(development headers)
  7. apache2-doc -Apache HTTP Server(on-site documentation)
  8. apache2-utils -Apache HTTP Server(utility programs for web servers)
  9. ......................................................................

注意: 如果你安裝了“apt-file”包,我們就可以像下面那樣用配置文件搜索包。

  1. linuxidc@localhost:~$ apt-file search nagios.cfg
  2. ganglia-nagios-bridge:/usr/share/doc/ganglia-nagios-bridge/nagios.cfg
  3. nagios3-common:/etc/nagios3/nagios.cfg
  4. nagios3-common:/usr/share/doc/nagios3-common/examples/nagios.cfg.gz
  5. pnp4nagios-bin:/etc/pnp4nagios/nagios.cfg
  6. pnp4nagios-bin:/usr/share/doc/pnp4nagios/examples/nagios.cfg

示例:3 顯示特定包的基本信息

  1. linuxidc@localhost:~$ apt-cache show postfix
  2. Package: postfix
  3. Priority: optional
  4. Section: mail
  5. Installed-Size:3524
  6. Maintainer:LaMontJones<[email protected]>
  7. Architecture: amd64
  8. Version:2.11.1-1
  9. Replaces: mail-transport-agent
  10. Provides:default-mta, mail-transport-agent
  11. .....................................................

示例:4 列出包的依賴

  1. linuxidc@localhost:~$ apt-cache depends postfix
  2. postfix
  3. Depends: libc6
  4. Depends: libdb5.3
  5. Depends: libsasl2-2
  6. Depends: libsqlite3-0
  7. Depends: libssl1.0.0
  8. |Depends: debconf
  9. Depends:<debconf-2.0>
  10. cdebconf
  11. debconf
  12. Depends: netbase
  13. Depends: adduser
  14. Depends: dpkg
  15. ............................................

示例:5 使用apt-cache顯示緩存統計

  1. linuxidc@localhost:~$ apt-cache stats
  2. Totalpackage names:60877(1,218 k)
  3. Totalpackage structures:102824(5,758 k)
  4. Normal packages:71285
  5. Purevirtual packages:1102
  6. Singlevirtual packages:9151
  7. Mixedvirtual packages:1827
  8. Missing:19459
  9. Total distinct versions:74913(5,394 k)
  10. Total distinct descriptions:93792(2,251 k)
  11. Total dependencies:573443(16.1 M)
  12. Total ver/file relations:78007(1,872 k)
  13. TotalDesc/File relations:93792(2,251 k)
  14. TotalProvides mappings:16583(332 k)
  15. Total globbed strings:171(2,263)
  16. Total dependency version space:2,665 k
  17. Total slack space:37.3 k
  18. Total space accounted for:29.5 M

示例:6 使用 “apt-get update” 更新倉庫

使用命令“apt-get update”, 我們可以重新從源倉庫中同步文件索引。包的索引從“/etc/apt/sources.list”中檢索。

  1. linuxidc@localhost:~$ sudo apt-get update
  2. Ign http://extras.ubuntu.com utopic InRelease
  3. Hit http://extras.ubuntu.com utopic Release.gpg
  4. Hit http://extras.ubuntu.com utopic Release
  5. Hit http://extras.ubuntu.com utopic/main Sources
  6. Hit http://extras.ubuntu.com utopic/main amd64 Packages
  7. Hit http://extras.ubuntu.com utopic/main i386 Packages
  8. Ign http://in.archive.ubuntu.com utopic InRelease
  9. Ign http://in.archive.ubuntu.com utopic-updates InRelease
  10. Ign http://in.archive.ubuntu.com utopic-backports InRelease
  11. ................................................................

示例:7 使用apt-get安裝包

  1. linuxidc@localhost:~$ sudo apt-get install icinga

上面的命令會安裝叫“icinga”的包。

示例:8 升級所有已安裝的包

  1. linuxidc@localhost:~$ sudo apt-get upgrade

示例:9 更新特定的包

在apt-get命令中的“install”選項後面接上“-only-upgrade”用來更新一個特定的包,如下所示:

  1. linuxidc@localhost:~$ sudo apt-get install filezilla --only-upgrade

示例:10 使用apt-get卸載包

  1. linuxidc@localhost:~$ sudo apt-get remove skype

上面的命令只會刪除skype包,如果你想要刪除它的配置文件,在apt-get命令中使用“purge”選項。如下所示:

  1. linuxidc@localhost:~$ sudo apt-get purge skype

我們可以結合使用上面的兩個命令:

  1. linuxidc@localhost:~$ sudo apt-get remove --purge skype

示例:11 在當前的目錄中下載包

  1. linuxidc@localhost:~$ sudo apt-get download icinga
  2. Get:1 http://in.archive.ubuntu.com/ubuntu/ utopic/universe icinga amd64 1.11.6-1build1 [1,474 B]
  3. Fetched1,474 B in1s(1,363 B/s)

上面的目錄會把icinga包下載到你的當前工作目錄。

示例:12 清理本地包占用的磁盤空間

  1. linuxidc@localhost:~$ sudo apt-get clean

上面的命令會清空apt-get所下載的包占用的磁盤空間。

我們也可以使用“autoclean”選項來代替“clean”,兩者之間主要的區別是autoclean清理不再使用且沒用的下載。

  1. linuxidc@localhost:~$ sudo apt-get autoclean
  2. Readingpackage lists...Done
  3. Building dependency tree
  4. Reading state information...Done

示例:13 使用“autoremove”刪除包

當在apt-get命令中使用“autoremove”時,它會刪除為了滿足依賴而安裝且現在沒用的包。

  1. linuxidc@localhost:~$ sudo apt-get autoremove icinga

示例:14 顯示包的更新日志

  1. linuxidc@localhost:~$ sudo apt-get changelog apache2
  2. Get:1Changelogfor apache2 (http://changelogs.ubuntu.com/changelogs/pool/main/a/apache2/apache2_2.4.10-1ubuntu1/changelog) [195 kB]
  3. Fetched195 kB in3s(60.9 kB/s)

上面的命令會下載apache2的更新日志,並在你屏幕上分頁顯示。

示例:15 使用 “check” 選項顯示損壞的依賴關系

  1. linuxidc@localhost:~$ sudo apt-get check
  2. Readingpackage lists...Done
  3. Building dependency tree
  4. Reading state information...Done

Copyright © Linux教程網 All Rights Reserved