IOS203_Ch8
Chapter 8: Advanced File Management
Compressing Files ........................................................................................ 3
Archiving Files ............................................................................................. 5
Installing and Managing Packages (Using RedHat Tools)............................ 6
1
IOS203_Ch8
Objectives
Upon completion this chapter, the student should be able to:
Compress/uncompress files using gzip/gunzip commands;
Compress/uncompress files using bzip2/bunzip2 commands;
Use zcat to show the content of compressed files;
Use rpm to add, update or remove software packages;
Use yum to add, update or remove software packages.
Keywords
gzip, gunzip, bzib2, bunzib2, tar, zcat, zmore, zless, whereis, rpm, yum.
2
IOS203_Ch8
Every Linux distro comes with thousands of software applications, covering every major category
of desktop, server, and programming software. Software applications are distributed as packages.
Linux kernel is the core of any Linux operating system and each Linux distribution consists of a
kernel and other software packages. Every package performs small tasks of the complete system
and it could be replaced by an equivalent one. In order to be a good Linux user, you should
understand Linux packages and know how to manage them.
1. Compressing Files
Compressing is the process of reducing the size of files or directories. Usually, large files are
compressed before transferring to other users or systems. It could be useful for you to compress
files that are not used very often. The advantages of saved space and bandwidth usually
outweighs the added time it takes to compress and uncompress files. Text files can be compressed
up to 75%, but this percentage does not exceed 25% when compressing binary files. Sometimes,
a compressed binary file could be larger than the original file.
In spite of the emergence of new compression utilities, older ones are still used. In fact, there is a
tradeoff between compression efficiency and CPU activity. Sometimes, older compression utilities
are good enough in terms of compression time. The two utilities gzip and bzip2 are most common
compression utilities used in Linux. After compression a file, the compressed file replaces the
original file in its location and use its name suffixed with (.gz) if you use gzip command or (.bz2) if
you use bzip2 command. When uncompressing a file, it returns to its status before compressing
and the suffix (.gz or .bz2) will be removed from its name.
gzip command
The zip format, which is common on Windows, is supported by the Linux gzip command for
compressing and unzip command for uncompressing. The following table shows some options
used with gzip command:
Option Description
-c Write output on stdout
-d Decompress instead of compress file
-r Recurse through subdirectories, compressing individual files.
Here are some examples:
[student@StudentHost ~]$ ls -lh file.txt
-rw-rw-r-- 1 student student 5.9M Apr 25 13:44 file.txt
[student@StudentHost ~]$ gzip file.txt
[student@StudentHost ~]$ ls -lh file.txt.gz
-rw-rw-r-- 1 student student 760K Apr 25 13:44 file.txt.gz
[student@StudentHost ~]$ ls -sh file2
3
IOS203_Ch8
750K file2
[student@StudentHost ~]$ gzip file2
[student@StudentHost ~]$ ls -sh file2*
244K file2.gz
[student@StudentHost ~]$ gzip -d file2 # uncompressing the file, you can omit the ‘.gz’
[student@StudentHost ~]$ ls -sh file2*
750K file2
You can get the original file back with gunzip.
[student@StudentHost ~]$ ls -lh file.txt.gz
-rw-rw-r-- 1 student student 760K Apr 25 13:44 file.txt.gz
[student@StudentHost ~]$ gunzip file.txt.gz
[student@StudentHost ~]$ ls -lh file.txt
-rw-rw-r-- 1 student student 5.9M Apr 25 13:44 file.txt
bzip2 command
This command is relatively new and produces the most compact compressed files, but is the most
CPU intensive. Files compressed with bzip2 are uncompressed with bunzip2. The bzip2 command
supports the following options:
Option Description
-c Write output on stdout
-d Decompress instead of compress file
The following examples illustrate the use of bzip2 and bunzip2 commands:
[student@StudentHost ~]$ ls -sh file2
750K file2
[student@StudentHost ~]$ bzip2 file2
[student@StudentHost ~]$ ls -sh file2*
190K file2.bz2
[student@StudentHost ~]$ bunzip2 file2.bz2
[student@StudentHost ~]$ ls -sh file2*
750K file2
4
IOS203_Ch8
zcat, zmore and zless commands
The three commands zcat, zmore and zless allow you show the contents of compressed files
with gzip in the same manner as you use cat, more and less respectively to view the
uncompressed files contents. In the example below, we use zcat to view the content of the
file.txt.gz file.
2. Archiving Files
In some situations, you need to move a group of files around on a single system; back up them to
a recordable DVD, tape, or other removable media; or transfer them across a network. To do that,
you should archive the files. Archiving is the process of combining multiple files and directories
into one “package” file, it is often used when moving data from one system to another.
One of the most prominent archiving commands is tar. The tar program’s name stands for “Tape
archiver”; you can use it to archive data to other media. There are five main operating options in
tar utility:
Option Description
-c Create an archive from a file(s) or directory(s).
-r Append files to the end of an archive.
-x Extract an archive.
-u Append files which are newer than the corresponding copy in the archive
- tf List the contents of the archive, f refers to the file name
tar archives filenames are usually created with .tar filename extensions, although this is not
required. The following table contains some common examples to show you how to use tar utility:
Example Description
Create a new tar archive BackUp.tar from the
$ tar cf BackUp.tar BackUp
directory BackUp
Create an archive from a set of files (file1 file2 file 3)
$ tar cf BackUp.tar file1 file2 file 3
in the current working directory
$ tar xf BackUp.tar Extract an archive in the current directory
5
IOS203_Ch8
Extract the archive in a different directory using –C
$ tar xf BackUp.tar -C Desktop
option (capital c)
$ tar xf BackUp.tar --wildcards "*.txt" Extract only the files their names ends with “.txt”
Create a gzipped archive (.tar.gz), use z option to
$ tar czf BackUp.tar.gz BackUp
compress the archive using gzip command
Create a gzipped archive (.tgz), use z option to
$ tar czf BackUp.tgz BackUp
compress the archive using gzip command
Create a gzipped archive (.tgz), use v option to view
$ tar cvzf BackUp.tgz BackUp the progress while creating the archive, v refers to
verbose
$ tar xzf BackUp.tgz Extract the gzipped archive in the current directory
$ tar xzf BackUp.tgz -C Desktop Extract the gzipped archive in a different folder
$ tar cjf BackUp.tar.bz2 BackUp Create an bzipped archive (.tar.bz2) of a directory
$ tar cjf BackUp.tbz BackUp Create an bzipped archive (.tbz) of a directory
$ tar xjf BackUp.tar.bz2 -C Desktop Extract the bzipped archive to some other directory
$ tar czvf BackUp.tgz Downloads Create archive of multiple directories and/or files at a
Documents/one.txt Videos time
$ tar tvf BackUp.tar Verify if the files are added or not
Here are some other examples of tar command lines and the results of their executions:
If you want to know more details about tar utility, refer to the man pages:
[student@StudentHost ~]$ man tar
3. Installing and Managing Packages (Using RedHat Tools)
One program may depend on multiple programs or libraries; each of those relies on several more,
and so on. Multiple software programs are bundled together to form a package which is an
assemblage of files and information about those files such as its name, the specific version and a
description.
Almost all software are installed on Linux as packages. For this reason, a package manager plays
an important role. A package manager is a collection of tools to automate the process of installing,
6
IOS203_Ch8
upgrading, configuring, and removing software packages from a computer. Before using package
managers, software was mostly distributed as "Tarballs”.
Tarballs
Tarballs are archive files created by tar utility and typically compressed with gzip or bzip2; they
are often used for transferring multiple files between computers in one step, such as when
distributing source code.
Tarballs names have the extension .tar.gz, .tgz or .tar.bz2. They must be compiled and installed
often with components in multiple directories (e.g., /etc, /var/lib, /usr/bin/, and /usr/lib). To
uncompress them, execute the following command line:
[student@StudentHost ~]$ tar zxf Tarfile.tgz # or Tarfile.tar.gz
Or the command line:
[student@StudentHost ~]$ tar jxf Tarfile.tbz2 # or Tarfile.tar.bz2
Now, you should change your working directory to the directory stored the extracted files:
[student@StudentHost ~]$ cd Tarfile
Each tarball comes with installation and build instructions. Before installing a tarball, you should
read Install or Readme files:
[student@StudentHost ~]$ vi Install # or vi Readme
To build and install the software, you may need to execute the following three command lines:
[student@StudentHost ~]$ ./configure
[student@StudentHost ~]$ make
[student@StudentHost ~]$ sudo make install
[student@StudentHost ~]$ sudo reboot # if necessary
Where, ./configure will configure the software to ensure your system has the necessary
functionality and libraries to successfully compile the package, make will compile all the source
files into executable binaries, and make install will install the binaries and any supporting files
into the appropriate locations.
For source code files, you should first of all compile them. Given the hello.c file written in C
programming language, to compile it, you need to use the gcc compiler as follows:
[student@StudentHost ~]$ cat hello.c
#include "stdio.h"
int main(void) {
printf("hello world\n");
return 0; }
[student@StudentHost ~]$ gcc -o hello hello.c
[student@StudentHost ~]$ ./hello
hello world
7
IOS203_Ch8
Nowadays, you don’t need to compile the components you want to install as a part of your Linux
operating system; packages contains binary files which are precompiled and easily managed by a
package manager.
RedHat Package Manager (RPM)
RPM [1] is a popular package management tool in RedHat Linux-based distros (e.g., Centos,
Fedora). It maintains a database of installed packages, which enables powerful and fast queries.
The database is found in the /var/lib/rpm directory. Using RPM, you can install, upgrade, remove
or query individual software packages. An RPM package consists of an archive of files and
metadata. Metadata includes help scripts, file attributes, and information about packages. The
following table contains some commonly used options for RPM:
Option Description
-a Display all package names installed on the system (when used
--all with the –q option)
-f Display the package to witch the specified file belongs (when used
--file with the –q option)
-h Hash (print 50 hash marks as the package archive is unpacked)
-i Install a specified package
--install
-iq Display full information about the specified package
--info
-lq List the filenames the specified package comprises
--list
-U Upgrade a specified package
--upgrade
-e Erase a specified package from the system
--erase
-V Verify a package
--verify
-q Query a package
--test Perform a test installation only (when used with the –i option)
--version Print version number
-v Print verbose output
Note: You must have root privileges to add and remove software with rpm. But, to query installed
packages or package files, anyone can do that.
Here are some rpm examples:
Example Description
# rpm –i nano.2.9.8-1.e18.x86_64.rpm Install a nano package, you should type correctly
the package name
8
IOS203_Ch8
# rpm -Uvh mysql-server-3.23.58- Install or upgrade a package
9.i386.rpm
# rpm -qf /boot/vmlinuz* Determine what package a file belongs to
For example, if you need information about nano package, you can use the following command
line:
Or if you need to list all installed files from nano package and display only the first five:
You can even remove nano package with this command:
9
IOS203_Ch8
Sometimes RPM installations fail and give dependencies errors because a prerequisite RPM
package needs to be installed. To get around this problem, you may run the rpm command
with the --nodeps option to disable dependency checks:
[root@StudentHost ~]# rpm -Uvh --nodeps mysql-3.23.58-9.i386.rpm
YellowDog Updater, Modified (YUM)
YUM [1] is a tool for installing, updating, removing, and managing software packages in in RedHat
Linux-based distros (e.g., Centos, Fedora). YUM performs dependency resolution when installing,
updating, and removing software packages. It can manage packages from installed repositories in
the system or from .rpm packages. The main configuration file for YUM is at /etc/yum.conf, and
all the repos are at /etc/yum.repos.d. YUM makes multiple attempts to download RPMs before
failing. It automatically figures out not only the RPMs packages that need updating, but also all
the supporting RPMs. It then installs them all.
You can use YUM to modify the software on your system in four ways:
Install new software from package repositories;
Install new software from an individual package file;
Update existing software on your system;
Remove unwanted software from your system.
The following table contains some commonly-used commands for YUM:
Command Description
install Install the specified packages
reinstall Reinstall the specified packages
remove Remove the specified packages
search Search package metadata for keywords
info List description
update Update each package to the latest version
repolist List repositories
history Display what has happened in past transactions
10
IOS203_Ch8
Here are some options commonly-used with YUM:
Option Description
-C Run from system cache
--security Include packages that provide a fix for a
security issue
-y Automatically answer yes to all questions
--skip-broken Skip packages causing problems
-v Print verbose output
yum examples
The following command tries to install nano command; but after checking the installed package,
it finds that the package is up-to-date and missing nothing. So, nothing to do:
The following command lists enabled repositories:
The following command updates firefox package:
11
IOS203_Ch8
To find the location in which a command is installed, you can use whereis command.
whereis command
This command is used to locate the binary, source, and manual page files for a command. It
searches for files in a restricted set of locations (binary file directories, man page directories, and
library directories). The following command lines show you how to use the whereis command:
12
IOS203_Ch8
Questions
First: “Using RPM”
1 What does RPM stand for?
2 How to check the package is installed?
3 How to get more detail information while installing a rpm package?
4 How to remove multiple packages at the same time?
5 How to list the scripts in a package?
6 How to list the most recently installed packages?
7 How to list what has changed for a package?
Second: “Using YUM”
8 How to install packages using yum?
9 How to check the updates for yum repository?
10 How to list the installed packages on Linux using yum command?
Third: “Installing a source package from the CentOS source directory [5]”
11 Prepare your working environment.
# mkdir -p ∼/rpmbuild/{SPECS,SRPMS,RPMS,BUILD,SOURCES}
# echo "%_topdir ∼/rpmbuild" >> ∼/.rpmmacros
12 Download the package you want to the current directory. nstall the source code package
using the following command:
# rpm -iv packagename*.src.rpm
Of course, you should replace packagename with the name of the package you are
installing.
13 Change to the SPECS directory as follows:
# cd ∼/rpmbuild/SPECS
14 Unpack the source code as follows. Note that you may need to install the rpm-build
package:
# rpmbuild -bp packagename*.spec
The package’s source code is installed to the rpmbuild/BUILD/package directory, where
package is the name of the software package.
15 You can now make changes to the files in the package’s BUILD directory. Read the
README, Makefile, and other documentation files for details on how to build the
individual package.
The --rebuild option to rpmbuild can be used to rebuild the RPM without installing it first.
The resulting binary will be in rpmbuild/RPMS/arch, where arch is replaced by i386 or other
architecture for which you are building the RPM.
Fourth: “Installing Software in tar.gz or tar.bz2 Formats [5]”
16 Get the source code package from the Internet or from a CD distribution and copy it into
13
IOS203_Ch8
an empty directory.
17 To check the contents of your tar archive before extracting it to your hard drive, you could
use the following command:
# tar tvf package.tar.gz
18 Assuming that the file is compressed using gzip, uncompress the file using the following
command:
# gunzip package.tar.gz
The result is that the package is uncompressed and the .gz is removed from the package name
(e.g., package.tar). (If your package ends in bz2, use the bzip2 command instead of gunzip
shown above.)
19 From the resulting tar archive, run the tar command as follows:
# tar xvf package.tar
This command extracts the files from the archive and copies them to a subdirectory of the
current directory. (Using tar xvfz package.tar.gz, you can do Steps 2 and 3 in one step. For
a compressed bzip2 file, run tar xvfj package.tar.bz2 instead.)
20 Change directories to the new subdirectory created in Step 3, as follows:
# cd package
21 Look for a file called INSTALL or README. One of these files should give you instructions on
how to proceed with the installation. In general, the make command is used to install
package. Here are a few things to look for in the current directory:
If there is a Make.in file, try running:
# ./configure –prefix=/usr/local
# make all
If there is an Imake file, try running:
# xmkmf –a
# make all
If there is a Makefile, try running:
# make all
References
[1] https://www.redhat.com/sysadmin/how-manage-packages, by Keerthi Chinthaguntla,
updated on April 22, 2020
[2] Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
[3] Paul Cobbaut, “Linux Fundamentals”, https://linux-training.be/funhtml/index.html.
Updated on 2015-05-24
[4] https://www.linuxnix.com/10-file-globbing-examples-linux-unix/
[5] “CentOS Bible”, C. Negus and T. Boronczyk, Wiley Publishing, Inc., 2009
14