Downloading MySQL rpms with a Linux one liner

I love linux. It’s almost one year now since I switched my main work desktop machine to Linux from Windows XP, and I’ve not looked back. Windows was slow, unreliable (regular blue screens) and lacked many of the advanced features that linux has out of the box (or should I say off of the web).

Linux’s features are too numerous to list, but every now and then I use one that just reminds me how superior it is to it’s proprietary rival. In this case I needed to download the latest MySQL 5.1.23 rpms for installation on an ndb cluster.

Normally I would go to MySQL’s download page, and manually right click to save them individually, but since I have been doing it so frequently recently, and that I was likely to need to do it again in the future, I thought there must be a better way.

The answer lay with a few bash commands strung together with pipes:

wget -O - http://mirror.fubra.com/www.mysql.com/Downloads/MySQL-5.1/ | grep -o -P 'href=".+5.1.23-0.glibc23.x86_64.rpm"' | grep -o -P 'MySQL[^"]+' | xargs -I {} wget http://mirror.fubra.com/www.mysql.com/Downloads/MySQL-5.1/{}

The command above (which should be all on one line) does the following:

  • First we use wget to download a directory listing of all MySQL 5.1 downloads from our local MySQL mirror (but this could be any mirror). We invoke the -O – option to direct the output of the webpage to STDOUT rather than a file.
  • The output from wget is piped to grep which does a perl regular expression to look for links to all rpms from the particular version of MySQL we want, in this case generic 5.1.23 for x86_64 machines. This is returned as a list of every link from the html source containing a link to one of these files.
  • The output from grep is piped to another instance of grep. The reason for this is that we want to cut out the href=”” from the links so we are left with just the file name.
  • The tidied list of filenames is piped to xargs which runs wget for each one, pre-pending the full path to the beginning of the filename

And that’s it. We end up with each rpm being downloaded to the current working directory.

I know that it is technically possible to do things like this with Windows Power Shell, and cygwin, but they are not native solutions that are available to every machine by default, as they are on all *nix machines.

2 thoughts on “Downloading MySQL rpms with a Linux one liner”

  1. I’ll be switching to Ubuntu as soon as I have a few hours spare. I bought a new HP Compaq 6720S laptop in January and installed Ubuntu then. There were 3 problems – no wifi, no LAN, did not recognise my external Western Digital USB hard disk. Other people who had just bought this new laptop model had the same problem.

    I tried to fix the problems but it was taking too long so I switched back to Vista. Then, four days later, someone responded to one of my Ubuntu forum posts with detailed instructions on how to fix all the problems. What an amazing response! It’s yet another happy story of why the open source community is the best 🙂

  2. Glad to hear your thinking of making the switch 🙂 You won’t regret it!

    Yeah, there are sometimes some issues with Ubuntu not recognising every device on your system, but I have found the later editions like 7.10 to be pretty good. In fact, I have found Ubuntu to be far more effective at auto-discovering hardware than a vanilla installation of XP.

    I recently upgraded Aimi’s laptop from Vista to XP (yup XP is an upgrade compared to Vista), and I had to manually download drivers for everything from the network card through to the graphics card! Ubuntu would have definitely have picked up more I think.

Leave a Reply

Your email address will not be published. Required fields are marked *