Tuesday, December 21, 2004

Intermediate rpm

A few articles back I talked a bit about the basics of installing and upgrading a package using rpm, the Red Hat Package Manager. This article covers some rpm tips and tricks that might come in handy when you're trying to find out more information about software installed on your system.
To begin with, open a x-terminal and type:

rpm -qa

A long list of software will scroll by quickly, don't worry at this point. The -q stands for query, the -a for all. We can add commands to this one by using the pipe symbol |, followed by the command. So, if we wanted to pause the listing we would type:

rpm -qa | more

After you execute this command, at the bottom of your screen you'll see a highlighted more text. If you press the enter key you can scroll to the next screen. If you want to quit the listing at this point press :q. It might seem like a strange way of exiting, but if you ever use the vi text editor you'll use the same command to exit the editor.

We can expand this command even more by adding a sort to it. Sort is a Linux command, not an option of a command, so it's possible to get more information about sorting by typing:

man sort

If you look at the man(ual) page for sort you'll see that the -r switch will sort in reverse. Let's combine a reverse sort with our previous command:

rpm -qa | sort -r | more

Now we get a list of managed packages that begin with the letter z. Pretty neat stuff, eh? Let's say you're like me and you like to install a lot of software and remove packages later. One of the things you might want to do is generate a list of software packages and redirect that list to an actual file. While you could simply open a second x-terminal and use the rpm and more commands to pause through the package listing there is a redirection symbol, >, that makes it possible to redirect output to a file.

rpm -qa > packages.installed | sort

The main caveat to this command is that you need to be sure you're in a directory you can write to. If, for example, you're logged in as a regular user, but in the /usr/bin directory you won't be able to run this command because you won't have permission to store the file packages.installed in /usr/bin.

Now let's say we want to look for a specific package and we know a little about that package, but not everything. Let's say I wanted to know if the game frozen-bubble was installed. I could run a sorted listing of all the rpms and space down until I got to the f's. But it's easier to use a handy command called grep:

rpm -qa | grep frozen

Grep will search the package listing and display only the packages that contain the word frozen in it. If there is no package with the word frozen in it you will not see anything. For the sake of this article I'm going to say it is installed, it is on my system. Now I want to get a bit more information about what the package is and what it does. To get information about frozen-bubble we would type:

rpm -qi frozen-bubble

Notice I did not type all the version numbers after the filename, in almost all cases you don't have to type a version number after the package name. Expanding our repertoire even further we can list what files the frozen-bubble package actually puts on our system and where those files are located. This might not seem useful at first, but it comes in really handy when you're looking for something like documentation. The command to see the installed files is:

rpm -ql frozen-bubble | more

Note that the switch beside the q is a small L, not a number one. Using this command I can see that there is a README file located at /usr/share/doc/packages/frozen-bubble/, truly handy. I can also see the location of some of the graphics and music files. If I was truly adventurous I might replace some of these files with graphics of my own.

If you have frozen-bubble installed and have been following along you'll notice that there are quite a few files installed by the frozen bubble package. We can get a count of how many by using the word count program along with the switch to count the number of lines:

rpm -ql frozen-bubble | wc -l

On my computer this command spits out 298. So frozen-bubble consists of 298 files. If you're planning on trimming software from your system word count can come in handy.

In this article you've digested a lot about rpm. Amazingly, rpm can do a whole lot more when combined with other commands. One of the great things about Linux is how flexible and expandable it is. This is just a small example of Linux's flexibility. Until next time, happy rpmming.

No comments: