useful-linux-commands/chapters/ch04-system.tex

262 lines
12 KiB
TeX

\chapter{System}
\section{To Reload Your \texttt{.bashrc} File}
\lettrine{S}{ometimes} you add something to your \texttt{.bashrc}\index[concepts]{.bashrc}\index[concepts]{bashrc} file (such as a new bash alias)\index[concepts]{bash alias} and you need to reload it. Of course, this file is reloaded every time you log in, but you may not wish to log out and then log back in if you are in the middle of important work. There are few different ways to do this:
\begin{Verbatim}[frame=single, breaklines=true]
source ~/.bashrc
\end{Verbatim}
\index{source}
and of course a shorter way:
\begin{Verbatim}[frame=single, breaklines=true]
. ~/.bashrc
\end{Verbatim}
\section{To See if a Package Exists in the \texttt{apt} Repository}\index[concepts]{repositories}
\lettrine{W}{hile} there are many ways to install software on a Linux system, the easiest way is usually through the package manager, which on Ubuntu is \texttt{apt}. \index{apt} To see if a package is available in that repo, we can use
\begin{Verbatim}[frame=single, breaklines=true]
apt search <package>
\end{Verbatim}
\index{apt!search}
As an example, searching for the Okular document viewer in \texttt{apt} returns the following:
\begin{Verbatim}[]
libokular5core9 - libraries for the Okular document viewer
okular - universal document viewer
okular-backend-odp - Okular backend for ODP documents
okular-backend-odt - Okular backend for ODT documents
okular-dev - development files for the Okular libraries
okular-extra-backends - additional document format support for Okular
okular-mobile - mobile support for Okular
qml-module-org-kde-okular - mobile support for Okular - QML modules
\end{Verbatim}
This does not tell us which version is available however. We have a couple of options here. First, we can use
\begin{Verbatim}[frame=single, breaklines=true]
apt policy <package>
\end{Verbatim}
\index{apt!policy}
Using that for Okular returns the following:
\begin{Verbatim}[breaklines=true]
okular:
Installed: (none)
Candidate: 4:21.12.3-0ubuntu1
Version table:
4:21.12.3-0ubuntu1 500
500 http://us.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
100 /var/lib/dpkg/status
\end{Verbatim}
We can also use:
\begin{Verbatim}[frame=single, breaklines=true]
apt search <package>
\end{Verbatim}
\index{apt!search}
Which gives a more detailed listing with the available versions of all related packages:
\begin{Verbatim}[]
Sorting... Done
Full Text Search... Done
libokular5core9/jammy 4:21.12.3-0ubuntu1 amd64
libraries for the Okular document viewer
okular/jammy,now 4:21.12.3-0ubuntu1 amd64 [residual-config]
universal document viewer
okular-backend-odp/jammy 1:3.2.1+dfsg-5 amd64
Okular backend for ODP documents
okular-backend-odt/jammy 1:3.2.1+dfsg-5 amd64
Okular backend for ODT documents
okular-dev/jammy 4:21.12.3-0ubuntu1 amd64
development files for the Okular libraries
okular-extra-backends/jammy 4:21.12.3-0ubuntu1 amd64
additional document format support for Okular
okular-mobile/jammy 4:21.12.3-0ubuntu1 amd64
mobile support for Okular
qml-module-org-kde-okular/jammy 4:21.12.3-0ubuntu1 amd64
mobile support for Okular - QML modules
\end{Verbatim}
To get even more information, including dependencies, we can use:
\begin{Verbatim}[frame=single]
apt show <package>
\end{Verbatim}
\index{apt!show}
Executing this command for Okular returns:
\begin{Verbatim}[breaklines=true]
Package: okular
Version: 4:21.12.3-0ubuntu1
Priority: extra
Section: universe/graphics
Origin: Ubuntu
Maintainer: Kubuntu Developers <kubuntu-devel@lists.ubuntu.com>
Original-Maintainer: Debian Qt/KDE Maintainers <debian-qt-kde@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 16.4 MB
Provides: pdf-viewer, postscript-viewer
Depends: kinit, kio, libc6 (>= 2.35), libfreetype6 (>= 2.2.1), libjpeg8 (>= 8c), libkf5activities5 (>= 5.68.0~), libkf5archive5 (>= 5.68.0~), libkf5bookmarks5 (>= 5.68.0~), libkf5codecs5 (>= 4.96.0), libkf5completion5 (>= 5.68.0~), libkf5configcore5 (>= 5.68.0~), libkf5configgui5 (>= 5.68.0~), libkf5configwidgets5 (>= 5.81.0), libkf5coreaddons5 (>= 5.68.0~), libkf5crash5 (>= 5.68.0~), libkf5i18n5 (>= 5.68.0~), libkf5itemviews5 (>= 4.96.0), libkf5jobwidgets5 (>= 4.96.0), libkf5kexiv2-15.0.0 (>= 21.12.3~), libkf5kiocore5 (>= 5.68.0~), libkf5kiowidgets5 (>= 5.68.0~), libkf5parts5 (>= 5.68.0~), libkf5pty5 (>= 5.68.0~), libkf5purpose-bin, libkf5purpose5 (>= 5.68.0~), libkf5service-bin, libkf5service5 (>= 4.96.0), libkf5textwidgets5 (>= 5.68.0~), libkf5wallet-bin, libkf5wallet5 (>= 5.68.0~), libkf5widgetsaddons5 (>= 5.77.0), libkf5windowsystem5 (>= 5.68.0~), libkf5xmlgui5 (>= 5.79.0), libokular5core9 (= 4:21.12.3-0ubuntu1), libphonon4qt5-4 (>= 4:4.8.0), libpoppler-qt5-1 (>= 21.11.0), libqmobipocket2 (>= 4:21.12.3~), libqt5core5a (>= 5.15.1), libqt5dbus5 (>= 5.14.1), libqt5gui5 (>= 5.14.1) | libqt5gui5-gles (>= 5.14.1), libqt5printsupport5 (>= 5.13.0~), libqt5texttospeech5 (>= 5.12.0~), libqt5widgets5 (>= 5.15.1), libqt5xml5 (>= 5.13.0~), libspectre1 (>= 0.2.3), libstdc++6 (>= 11), phonon4qt5, zlib1g (>= 1:1.1.4)
Recommends: cups-bsd, okular-extra-backends, qtspeech5-flite-plugin
Suggests: ghostscript, okular-extra-backends (= 4:21.12.3-0ubuntu1), poppler-data, texlive-binaries, unrar
Breaks: kdegraphics-mobipocket (<< 4:4.12), okular-extra-backends (<< 4:16.12.0), okular-mobile (<< 4:17.04.1-0)
Replaces: okular-extra-backends (<< 4:16.12.0), okular-mobile (<< 4:17.04.1-0)
Homepage: https://apps.kde.org/en/okular
Task: kubuntu-desktop, kubuntu-full, ubuntustudio-desktop
Download-Size: 6,206 kB
APT-Sources: http://us.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
Description: universal document viewer
Okular is a universal document viewer with support for advanced document
features, such as annotations, forms, and embedded files.
.
This package provides the document viewer application, plus plugins for a wide
variety of document formats such as:
* PostScript (PS)
* Portable Document Format (PDF)
* TeX Device independent file format (DVI)
* XML Paper Specification (XPS)
* G3 fax
* various electronic book formats: ComicBook, FictionBook, and Plucker
.
Additional document formats are in the okular-extra-backends package.
.
The Calligra suite provides Okular plugins for OpenDocument formats that can
be found in the okular-backend-odt and okular-backend-odp packages.
.
This package is part of the KDE graphics module.
\end{Verbatim}
\ktcbox{I generally prefer to install software from the \texttt{apt} repository, but sometimes it does not have the most recent version. These tools allow me to quickly check which version the repo does have. If the version is acceptable, I can install from there, or if not, I can explore other options.}
\section{To See Which Packages Have Been Installed}
\lettrine{I}{nstalling} software is easy on a Linux-based system. The problem is that sometimes you forget what you've installed. There are a few ways to do this. The first is
\begin{Verbatim}[frame=single]
apt list --installed
\end{Verbatim}
\index{apt!list}
This will give output like this:
\begin{Verbatim}[breaklines=true]
Listing... Done
abcm2ps/jammy,now 8.14.11-0.1 amd64 [installed]
accountsservice/jammy-security,jammy-updates,now 22.07.5-2ubuntu1.6 amd64 [installed,automatic]
acl/jammy,now 2.3.1-1 amd64 [installed,automatic]
acpi-support/jammy,now 0.144 amd64 [installed,automatic]
acpid/jammy,now 1:2.0.33-1ubuntu1 amd64 [installed,automatic]
adduser/jammy,jammy,now 3.118ubuntu5 all [installed,automatic]
adwaita-icon-theme-full/jammy,jammy,now 41.0-1ubuntu1 all [installed,automatic]
adwaita-icon-theme/jammy,jammy,now 41.0-1ubuntu1 all [installed,automatic]
adwaita-qt/jammy,now 1.4.1-1 amd64 [installed,automatic]
aglfn/jammy,jammy,now 1.7+git20191031.4036a9c-2 all [installed,automatic]
aisleriot/jammy,now 1:3.22.22-1 amd64 [installed,automatic]
alsa-base/jammy,jammy,now 1.0.25+dfsg-0ubuntu7 all [installed,automatic]
[…snip]
\end{Verbatim}
Again, that's a lot of information, but \texttt{[installed]} means that this package was installed manually, and \texttt{[installed,automatic]} means that it was installed as a dependency of some other package.\index[concepts]{dependencies}
Unfortunately, that will give you a list of \texttt{every} packaged that has been installed on your system, including from when you installed Linux originally. To get a list of manually installed applications, use
\begin{Verbatim}[frame=lines]
apt list --manually-installed
\end{Verbatim}
On my system, using the \texttt{--installed} option produced 3,184 lines of output, whereas using the \texttt{--manual-installed} option produced 237 lines of output—a considerable difference.
If necessary, you can redirect this information to a file using
\begin{Verbatim}[frame=lines]
apt list --installed > app_list.txt
\end{Verbatim}
Which will produce a warning: ``\texttt{WARNING: apt does not have a stable CLI interface. Use with caution in scripts.}'' What that means is that the \texttt{apt} command is meant to be used in an interactive mode (it includes things like progress bars when downloading and installing software, for example) and its output is not really able to be parsed in a script. (These commands work fine, however, as the output has a simple structure.) For scripting purposes, you really want to use \texttt{apt-get}\index{apt-get} and \texttt{apt-cache}\index{apt-cache}.\index[concepts]{apt stable CLI interface}
\section{Redirecting \texttt{stdout} and \texttt{stderr} Somewhere Else}\index[concepts]{redirection}
\lettrine{E}{very} command in Linux has three components:
\begin{enumerate}[noitemsep]
\item Something to work on (that is, input) which we call \texttt{stdin}
\item Something it spits out (that is, output) which we call \texttt{stdout}
\item Error messages if something isn't right, which we call \texttt{stderr}
\end{enumerate}\index[concepts]{stdin}\index[concepts]{stdout}\index[concepts]{sterr}
\noindent{}To redirect \texttt{stdout} to a file, you can use
\begin{Verbatim}[frame=lines]
command > file.txt
\end{Verbatim}
or
\begin{Verbatim}[frame=lines]
command 1> file.txt
\end{Verbatim}
\noindent{}To redirect \texttt{stderr} to a file, you can use
\begin{Verbatim}[frame=lines]
command 2> file.txt
\end{Verbatim}
\section{Command Substitution}\index[concepts]{command substitution}
\lettrine{O}{ccasionally} you may need to use the output of one command as the argument of another command.
For example, suppose you have a folder full of pdfs that you want to combine into a single pdf using \texttt{pdftk}\index{pdftk}, and you want to join them in alphabetical order. You could do something like this:
\begin{Verbatim}[frame=lines]
pdftk a.pdf b.pdf c.pdf d.pdf e.pdf f.pdf cat output merged.pdf
\end{Verbatim}
However, if you have a lot of pdf files, that becomes very tedious. Using command substitution, we can use the \texttt{ls} command to sort that list of files and then use the output of that command as the input to \texttt{pdftk}:
\begin{Verbatim}[frame=lines]
pdftk $(ls -v *.pdf) cat output merged.pdf
\end{Verbatim}
%$
In place of the dollar sign/parentheses construction, you can also use grave accents. Here is an example where we can use the \texttt{date} command as a variable in scripts:
\begin{Verbatim}[frame=lines]
echo "Today is `date`"
\end{Verbatim}
\noindent{}which returns
\begin{Verbatim}[]
Today is Thu Sep 17 09:39:44 AM EDT 2026
q\end{Verbatim}
And of course, you can pass options to the substituted command as well:
\begin{Verbatim}[frame=lines]
echo "Today is day #`date +%j` of the year"
\end{Verbatim}
\noindent{}which returns
\begin{Verbatim}[]
Today is day #260 of the year
\end{Verbatim}