Ubuntu apt-get command Detailed

The basic syntax
syntax:
APT-GET [Options] the Command

Profile:
Early apt-get default profile for /etc/apt/apt.conf, but the current system default Ubuntu does not have this file.
If /etc/apt/apt.conf file exists, apt-get will still read it. But now the idea is to design the configuration files are placed in the rear partition /etc/apt/apt.conf.d directory, it is easier to manage.

Common options:

-h, --help               // help documentation 
-v, --version            // Check the version of apt-get 
-y                       // responded yes to the need to confirm the scene 
-s, the --dry-RUN            // simulation and execution the output 
-d, --download-only      // the package is downloaded to the cache without installing 
--only-upgrade           // update the current version of the package rather than installing a new version of the 
--no-upgrade             // executing the install command when not install the update installed packages 
-q, --quiet              // reduce output 
--purge                  // with the remove command to remove the package configuration file 
--reinstall              // re-install or have installed a new version of the package

Common sub-command:
Update
Update command is used to resynchronize the package index file, / etc / apt / sources.list configuration file specifies the source of the package index file. Update the package index files after you can get updated information available package and the new package information. We have a local such information: What version of the software which can be from somewhere (source) installation.
update command should always be executed before the installation or upgrade package.

the install
the install command to install or upgrade package. Each package has a package name, rather than a fully qualified filename (e.g., the Debian system, parameters are provided apt-utils, instead apt-utils_1.6.1_amd64.deb). The packages are installed package depends will also be installed. /Etc/apt/sources.list configuration file contains the source (server) for obtaining the package. install command can also be used to update the specified package.

upgrade
upgrade command for the latest versions of all packages installed on the source system from the list of currently installed /etc/apt/sources.list. In any case, the currently installed packages will not be removed, not yet installed packages will not be retrieved and installed. Upgrading the case if the new version of the currently installed package can not be installed without changing the status of another package, and it will keep the current version. Update command must be executed in advance in order to know whether the apt-get package installed a new version is available.
Note the difference between update and upgrade:
update is to update the list of software, upgrade software is updated.

dist-upgrade
in addition to performing an upgrade functions, dist-upgrade also changes intelligently handle dependencies new version of the package with. apt-get has a "smart" conflict resolution system, if necessary, it will attempt to upgrade the most important packages at the expense of less important package for the price. Therefore, distr -upgrade command may remove some packages. Therefore, when the system update package, the following recommendations are sequentially executed commands:
$ APT-GET Update
$ APT-GET upgrade -Y
$ APT-GET-DIS upgrade -Y

the Remove
the Remove and install similar, except that instead of deleting the package installation package. Note that using a package will remove command to remove its configuration files remain on the system.

purge
purge command to remove a similar command, purge command to delete the package also removes the configuration files package.

autoremove
autoremove command is used to remove packages automatically installed, these packages had to satisfy dependencies of other packages it installed, and now no longer needed.

download
download command to download binary file specified package to the current directory. Note that this is similar to the * .deb package file.

Clean
Clean command clears retrieved in the local library package. It is from / var / cache / apt / archives / and / var / cache / apt / archives / partial / directory delete everything except lock files.

autoclean
与 clean 命令类似,autoclean 命令清除检索到的包文件的本地存储库。不同之处在于,它只删除不能再下载的软件包文件,而且这些文件在很大程度上是无用的。这允许长时间维护缓存,而不至于大小失控。

source
source 命令下载包的源代码。默认会下载最新可用版本的源代码到当前目录中。

changelog
changelog 命令尝试下载并显示包的更新日志。

常见用法

查看帮助文档

$ apt-get -h

更新包索引文件

$ sudo apt-get update

安装包

$ sudo apt-get install nginx

在需要确认的场景中回应 yes
多数包在安装前都需要与用户交互,在用户确认后才继续安装。而在自动化的任务中是没办法与用户交互的。-y 选项可以在这样的场景中发挥作用,其效果就像是用户确认了安装操作一样:

$ sudo apt-get install -y nginx

安装系统中有更新的包

$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get dis-upgrade -y

重新安装已安装的包
如果我们认为一个包工作不正常,可以尝试重新安装它,为 install 命令添加 --reinstall 选项就可以了。另外,如果已安装的包有了更新或新版本,也可以用这个方法把包升级到最新的版本:

$ sudo apt-get install --reinstall curl

更新指定的包
比较奇怪的是更新指定的包居然不是通过 upgrade 命令完成的,而是使用 intall 命令,注意:是 install 命令:

$ sudo apt-get install vim

模拟执行命令并输出结果
应用 -s 选项后命令并不会真正执行,��是会模拟执行并输出结果,比如下面的例子:

$ sudo apt-get update
$ sudo apt-get -s upgrade

添加 -s 选项后,upgrade 命令会输出将要被更新的软件但不会真的执行升级操作。

检查某个包的版本
通过下面的命令可以查看已安装包或即将安装包的版本:

$ sudo apt-get -s install vim

下面是一个已安装包的例子:

下面是一个未安装包的例子:

安装指定版本的包
注意,这里指的是包的版本号:

$ sudo apt-get install tree=1.7.0-5

把包下载到缓存中而不安装
选项 -d, --download-only 告诉命令只把包下载到缓存中而不安装它们,主要用在把下载包和安装包分开的场景,比如系统默认的自动化更新:

$ sudo apt-get install -d nginx
$ sudo apt-get upgrade -d
$ sudo apt-get dist-upgrade -d

删除包
remove 命令的特点是只删除程序文件,保留相关的配置文件:

$ sudo apt-get remove vim

如果要彻底清除包,可以使用 purge 命令,它会同时删除程序文件及其配置文件:

$ sudo apt-get purge vim

autoremove 命令用于删除自动安装的软件包,这些软件包当初是为了满足其他软件包对它的依赖关系而安装的,而现在已经不再需要了。因此在删除包后执行一下 autoremove 是个不错的选择:

$ sudo apt-get autoremvoe

清除系统中缓存的包安装文件
安装包的过程实际上是先把包安装文件下载到缓存目录,然后执行安装。久而久之系统中会存在大量无用的包安装文件,clean 命令可以清除这些缓存的包安装文件:

$ sudo apt-get clean

clean 命令从 /var/cache/apt/archives/ 和 /var/cache/apt/archives/partial/ 目录删除除锁文件之外的所有内容。

autoclean 命令与 clean 命令类似。不同之处在于,它只删除不能再下载的软件包文件,而且这些文件在很大程度上是无用的。这允许长时间维护缓存,而不至于大小失控:

$ sudo apt-get autoclean

在执行 remove 命令时使用 --purge 选项的效果等同于执行 purge 命令:

$ sudo apt-get remove --purge vim

在执行  install/remove 命令时使用 --autoremove 选项,可以同时删除无用的依赖包,类似在执行完  install/remove 命令后再执行一次 autoremove 命令:

$ sudo apt-get remove --autoremove vim

在执行 install/upgrade 命令时显示更详细的版本信息
通过 -V 选项,可以让 install/upgrade 命令输出包的具体版本,下面是默认的输出:

$ sudo apt-get upgrade -s

下面是添加 -V 选项后的输出:

$ sudo apt-get upgrade -V -s

减少输出
既然可以增加详细的输出信息,也可以灵活地使用 -q 选项来减少输出的信息:

$ sudo apt-get install  -q curl
$ sudo apt-get install  -q=2 curl

查看已安装包的更改日志
可以通过 changelog 命令来查看包的更新记录:

$ apt-get changelog tree

下载包的源代码(source code)
apt-get source 命令用来下载包的源代码。要让 apt-get source 命令起作用需要先更新 /etc/apt/sources.list 文件,添加 deb-src 配置,其实就是取消掉原本被注释掉的以 deb-src 开头的行。在 desktop 版的系统中,还可以通过  "Software & Updates" UI 来做同样的事情,在 "Ubuntu Software" 选项卡中选中 "Source code" 项即可:

现在再执行 apt-get source 命令并指定包的名称,就可以下载到该包的源代码了:

$ apt-get source tree

查看 apt-get 的版本

$ apt-get -v

这里显示的居然是 apt,太奇葩了!接着看:

原来 apt-get、apt-cache 和 apt-config 的功能都是由 apt 命令提供的。

查看系统的更新记录
日志文件 /var/log/apt/history.log 记录了系统的更新历史记录。通过它可以查看所有通过 apt 命令执行的安装、升级、删除记录:

$ less /var/log/apt/history.log

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160244.htm