It has been one of those days. I’m getting ready to head back to my hotel soon, but I just had to share a story with everyone. If nothing else it is proof that the Linux command line is a very powerful tool.. if you take the time to learn it.
Here is the setting. I’m teaching a class this week in Boston, MA. It is the RedHat 300 “Fast Track” course, for preparing Linux administrators for the RedHat Certified Engineer Exam. I’ve got a room full of very experienced geeks that have definitely kept me on my toes. Some of the questions they’ve come up with… well let me tell you. One in particular I just had to share.
One student decided to see what would happen if he filled up his drive using yum. (For those not familiar with yum it is the RH/Fedora equivalent of apt-get or aptitude). So he proceeded to do the following until his drive was at 100% capacity:
yum install a*
yum install b*
yum install c*
yum install d*
…. you get the idea.
He then posed the question: “How can I clean that up without re-installing the system?”
The first thought was to simply revert what he had done. ‘yum remove a*’, etc. We quickly realized that would not work because it would also remove packages that were required for the system. Hmm.. we’d have to come up with something more specific. Here is what we put together:
for i in `rpm -qai | grep -B5 'Thu 21 Jun 2007 03' | grep Name | awk '{ print $3 }' | grep -v kernel` ; do yum -y remove $i ; done
For those of you familiar with scripting and work in the shell it should make some sense. For the rest of you, I guess I might have to explain.
This one command actually fixes his problem and keeps the system intact.
First we’re creating a loop so that the command will repeat itself for every package installed on the system (rpm -qai lists info for all installed packages). We then grep through that output for todays date and the hour he began (grep -B5 ‘Thu 21 Jun 2007 03’), including the 5 previous lines. Based on that we grep for ‘Name’ within that output (grep Name). We then print a value from what we find there, which ends up being the package name (awk ‘{ print $3 }’). We throw that through one more grep to search for anything that *doesn’t* match the kernel (grep -v kernel). The output of all of this filtering is then assigned to the value of i, which then is run through the package removal command for each instance (yum -y remove $i).
In other words it scans all the packages on the machine, finds anything installed today during 3:00, pulls out the package name and removes it automatically. I’d say this is proof yet again that the Linux command line is a very powerful tool if you know how to use it, and this is a good example why you should learn how to use it.
Can anyone think of a better way to do it?
Neat story 🙂 Incidentally, do you know if there is any way to list all installed packages with aptitude, similar to what you would get with “rpm -qa”? I tried “aptitude show *”, but no luck.. It would be a useful trick to know.
Keith – to list your currently installed packages try “dpkg -l”. This shows the package, version and short description. “dpkg -L ” will show files provided by .
…as usual there is quite a bit more info in the man page for dpkg.
Thanks Christer for sharing this with us.
I like your blog , keep up the good work 🙂
Gah! Useless Use of Awk!
for i in `rpm -qai | grep -B5 ‘Thu 21 Jun 2007 03? | awk ‘
/Name/ && ! /kernel/ { print $3 }` ; do yum -y remove $i ; done
Neat. Personally, as I am no shell guru, I’d might have just used the yum groupremove/groupinstall (iirc) until the system gets back to a sane setup. You better know what you are doing running a one line command mentioned in the blog post here. 🙂
Now, anyone want to add a “Date Installed” column to Synaptic’s package list? Then you could achieve the same thing in Ubuntu from a GUI. 🙂
@mpt
+1
Did you file that on Malone?
Pingback: Can You Improve This Command Line Magic? : Ubuntu Tutorials : Breezy - Dapper - Edgy - Feisty
Pingback: Christer Edwards: Can You Improve This Command Line Magic? // The Linux Index