Feb 22 2008

Crazy One-Liners

Tag: hack, linux, ubuntuScott Wegner @ 7:42 pm

Command TerminalSo I wrote a pretty interesting one-line command for a specific task today. Here it is– can you guess what it does?

awk '1 {system("lwp-request -Sm HEAD " $0)}' \
input.txt | awk '/200 OK/ {print $2}' > output.txt

Yeah, me either if I were just looking at it. But let’s break it apart, piece-by-piece. You’ll notice that’s its essentially two commands, strung together through some piping and redirection (the “\” character is just to break the command up in two lines). It’s broken up like so:

{command1} | {command2} > {file}

This says to execute command1 first. Then pipe it’s output into command2 as input. Finally take the output of command2, and throw it all into a file. So we start with the first command:

awk '1 {system("lwp-request -Sm HEAD " $0)}' input.txt

So what the heck does awk do? Well, it’s basically a utility to read in input text, do some filtering on it, and then execute a specific task (or tasks) based on the results. In this case, it has the form:

awk 'filter {command}' input

Skipping first to input, we see that the text we want to process comes from a simple text file– in this case, input.txt. filter is what decides which lines of the input actually get used. Generally it’s in the form of a regular expression, and the matching lines are processed. In our case, we just use 1, which means everything matches, and we will process all lines. Next to the command:

system("lwp-request -Sm HEAD " $0)

In awk, the system command actually specifies that the parameter command should be executed in a sub-shell. The parameter is a quoted string, and using $0 means that we should use the first token of the matching line each time. So the function we really want to look at is:

lwp-request -Sm HEAD {token}

The lwp-request command, as seen here, is a command-line utility to send HTTP requests to a server, and observe there response. It has one required argument, which is the URL to query. Since we don’t see that explicitly here, that must be coming from the token we parsed from our input. We also specify two other parameters. -S tells the program to “print the response chain,” meaning that it will show any redirection or authorization handled automatically. Also, we use -m HEAD, which specifies that we are interested in the header data from the HTTP response. So far, pretty confusing, right? Well, let’s see what a sample query looks like:

$ lwp-request -Sm HEAD http://google.com
HEAD http://google.com --> 301 Moved Permanently
HEAD http://www.google.com/ --> 200 OK
Cache-Control: private
Connection: Close
Date: Sat, 23 Feb 2008 01:27:27 GMT
Server: gws
Content-Length: 0
Content-Type: text/html; charset=ISO-8859-1
Client-Date: Sat, 23 Feb 2008 01:27:36 GMT
Client-Peer: 64.233.167.104:80
Client-Response-Num: 1
Set-Cookie: PREF=ID=4b507d757f70e13b:TM=1203730047: (...)

Interesting, sort of. Anyway, let’s move on. So that little piece of code is getting executed for the first token of every line in our input file. Then, the output is getting “piped” into our next command:

awk '/200 OK/ {print $2}'

We’ve seen awk before! This time, though, we don’t specify an input file, because the input comes directly from the previous command. Our other parameters have changed as well. The filter is no longer 1, but rather /200 OK/. This is a true (albeit simple) regular expression, and matches any line that contains the string “200 OK”. Only lines with this string will be processed. Which brings us to our command, or action: print $2. print means to simply output what follows. In this case, $2, which represents the second parsed token. awk is going to consider everything that is piped in from command1, filter out lines it doesn’t care about, and execute the action on the filtered set. Looking at our sample output above, the only matching line is:

HEAD http://www.google.com/ --> 200 OK

This line will be used in the command, print $2. The command simply prints the second token (separated by a space) on the line, so it outputs:

http://www.google.com/

The final piece of our code redirects command2’s output into a file, output.txt. And that’s it! So putting the pieces together, let’s look at what is really happening here:

  • We read in data from an input file for parsing. We can infer that each line contains a URL, which is needed later
  • Each URL is passed to the lwp-request command, which outputs header information from the server
  • We filter the response information down to only the bits we care about. In this case, a new URL
  • Finally, we output each of these “new” URL’s to an output file.

So, that’s the whole one-liner. A little more compactly, it’s a piece of code that takes a list of input URL’s, and outputs the URL’s that each one redirects to. It’s a pretty specific snippet, and has absolutely no error-checking, so is definitely prone to bugs. But, it worked for me the one time I needed it, and it was enough to show off.

On a side-note, this little piece of code made the difference between hours of mindless data-entry, and automated awesomeness.

If you're new here, you can subscribe to automaticable's RSS feed by clicking here. Or, you can get post updates through your email. Thanks for visiting!


Feb 21 2008

Synfig - The Linux replacement for Flash

Tag: linux, software, technology, ubuntu, windowsJoe Wegner @ 5:54 pm

Synfig LogoMy recent changeover to Ubuntu Linux has had me searching for easy replacements for all of my Windows programs. The Linux community has made this a pretty easy task, especially with Ubuntu. Ubuntu provides you with Gimp (Photoshop), Firefox (Internet Explorer), Thunderbird (Outlook Express), and the OpenOffice Suite (Microsoft Office Suite). One thing they do not provide you with, however, is an easy replacement for Adobe Flash. Adobe Flash was one of my commonly used programs on Windows, because I do a lot of intro movies for my youth group. Not having a replacement for it was a major downfall for Linux.

Seeing this problem, for about two weeks I searched around for a good replacement for it. I ran across programs such as Flash-4-Linux and OpenLazlo. I heard good things about both of these programs, but found that the install was a bit difficult for a Linux newbie. Then I ran across a program called Synfig. It looked like it had good documentation, and a pretty easy install. All you had to do to install was open up a terminal and put in:

sudo aptitude install synfigstudio

After installing Synfig and opening it (Applications > Graphics > Synfig Studio), I found that I really liked the interface. The synfig interface is broken up into multiple windows, much like that of GIMP. This makes it very easy for me to customize it to my specific needs and project. I also noticed that it had a very easy tool selection menu. Choosing a brush, color, and all the other properties of the brush is very simple.

One of the main features I found in synfig that I have come to love is the different keyframe setup from Adobe Flash. Instead of having the keyframes, time, motion tweens, and everything else all bunched up onto a single window like Adobe Flash, synfig seperates all of these components. This means that creating keyframes is a much simpler process, and is much easier to get them at the precise moment you want.

The only downfall, however, is that Synfig is not made to do intense visual editing. Synfig only allows you to go about as complex as creating a simple gradient. Anything greater than that, such as opacity, blending, or even just adding text is not implemented. This means that if you want to make a very nice looking flash movie, you’ve got to couple Synfig with GIMP.

I would say that, if you are a fan of Adobe Flash, you should definately give Synfig a try. It is a great alternative for Linux, it’s got plenty of documentation, and the interface is very simple. Check out the website to get started, or use the terminal command above.


Feb 15 2008

Customize Your Dual-boot: GUI Frontend to GRUB

Tag: linux, software, ubuntuScott Wegner @ 11:47 am

I recently discovered via the Ubuntu Geek blog, a simple tool for customizing your GRUB boot menu. If you’re not familiar, the GRUB menu is the screen you see shortly before your Linux OS starts up. In a dual-boot setup, it is particularly important, because this is where you choose which operating system to boot to. Generally the setup is done via editing the file /boot/grub/menu.lst, but using the program makes it much easier. Continue reading “Customize Your Dual-boot: GUI Frontend to GRUB”


Feb 08 2008

Backup DVDs with dvd::rip

Tag: linux, software, ubuntuScott Wegner @ 5:22 pm

(screenshot source)
I’ve always been a little unorganized with my DVDs– some are in their original cases, some live in a case amongst some other software CDs, some are just completely lost. I’ve been meaning to get organized with my DVDs, but I just never got around to it.

Instead, I decided to back them up on my computer. This is convenient because I generally watch them on my computer anyway. Also, I’ll never have to worry about them getting scratched or lost.

Which brings me to dvd::rip, the DVD ripper of choice on Linux. I started using it earlier this week, and am very happy with it. It’s a little rough around the edges, because it’s so feature-rich. The program itself is basically a GUI wrapper for a number of command-line utilities, and each option refers to a command-line flag you can set. As a result, you can very carefully fine-tune the results of your rip– from target size, to resolution, to subtitles.

One of it’s best features, in my opinion, is it’s “Cluster” daemon. Basically, you are able to distribute the job of transcoding the video amongst as many machines as are available. The job is broken up into parts, and many computers can get the job done together much quicker. Furthermore, you can schedule many jobs on the cluster, which will all be pooled together and processed by the cluster. Once again, it’s an advanced setup, but I was able to backup 10 DVDs overnight.


Feb 06 2008

Crazy Shift-Drag

Tag: linux, ubuntuScott Wegner @ 9:15 pm

Ok, so I found another interesting little feature in Ubuntu by accident today. I was trying to drag a window across the room, and it would jump sporadically, moving only to the edge of another window. It was really weird, until I realized that my shift-key was stuck down. Apparently, if you hold down shift and drag a window, it’ll only move to the edges of other windows– cool for tiling a lot of programs on your screen.


Jan 30 2008

Call to Action: gDesklets

Tag: linux, software, ubuntuScott Wegner @ 10:00 pm


Ok, so I know when I say “Call to Action” I realize that my blog readership really consists of about, 3 people. So, this could really be rephrased “Hey guys, got any ideas?” Anyway, back to the post…

So one aspect that I find lacking on the Gnome desktop is widgets– Windows has them with Google Desktop or the sidebar in Vista, and Macs have them natively. But on Gnome, all we have is a program called gDesklets. Now, the problem with gDesklets, is that its inventory of widgets is so robust, it’s unusable. Because it’s open source and it’s been around forever, the repository of widgets is dated, most of them don’t work, and there’s no way to rank them based on popularity or usefulness.

Even after a few searches online, it’s hard to find any resource that has details on what the “best” gDesklet widgets are.

So here’s my “call to action”– can anyone find a list of the best gDesklet widgets available, or even a different program to replace it? I’m looking for something that’s open-source, integrates well with Ubuntu, and is easy to use. The winner will get a prize of my choosing…


Jan 18 2008

How-to: Use Your Ubuntu Computer as a Music Alarm Clock

Tag: how-to, music, software, ubuntuScott Wegner @ 7:19 pm

There are many music players that offer the functionality to wakeup to a song or playlist– both on Linux or Windows. But with each option I’ve tried, I’ve never been really happy with the results. For such a simple task, it always seemed overly-complicated. Also, the main downside I found in using a media player plugin, is that you’ll need to have the player running for it to actually work in the morning. Below I describe how to create your own music alarm clock, using only command-line utilities found on most Linux distributions. It uses quite a few different tools, and the tutorial will hit on quite a few different concepts. So, without further adieu…

  1. The first thing you will need is to create a playlist. I used Rhythmbox, since that’s where I store all of my music anyway. Create a playlist with songs you’d like to wake up to. When you’ve got enough, save it in .m3u format, somewhere where you’ll find it later. I put mine in my home directory.
  2. Next, we’ll need to make sure we have all the tools for the job. We’ll be using cron to schedule our tasks, amixer to set our volume, and mplayer to finally play our music. To make sure you have each of these installed, issue the following command:

    sudo aptitude install cron alsa-utils mplayer

  3. Next, we need to actually add the scheduled task. First I’m simply going to give you the commands, and I’ll explain what’s going on afterwards. In a terminal, enter the command:

    crontab -e

    Note: this will open your default text editor, which if you haven’t set it, will probably default to vim.

  4. Go to the end of the page by pressing Shift+G. Then start a new line pressing “o”. Once you’re there, type in or paste the following line:

    30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

  5. Press “ESC” to stop typing. Then enter the command “:wq” (no quotes) to save and quit. If everything went well, you should see the line:

    crontab: installing new crontab

Cool, you’re done! Now, let me explain what all that was, so you can go back and customize it on your own. Remember, any time you want to learn more about a command, you can use the “man” command. For example,

man crontab

to learn about crontab.

So first of all, we used “aptitude” to install a few packages from the Ubuntu repositories. This is probably familiar to you, or you may be using “apt-get”. They are basically the same, but “aptitude” has a few advantages– you should switch to using it if you haven’t already.

Then, we used “crontab -e”. Cron is the name of the task scheduler in Linux, and this command opens up our own personal “scheduled task list”. You can always use “crontab -e” to edit your tasks, or “crontab -l” just to view them.

Now, on to that crazy line I had you type in:

30 7 * * 1-5 /usr/bin/amixer set PCM 35\% && /usr/bin/X11/xterm -display :0 -bg black -fg white -e /usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Each entry in your personal crontab has the following format:

minute hour day-of-month month day-of-week command

So, in our case, our “minute” is 30, “hour” is 7, day-of-month is * (any), “month” is * (any), “day-of-week” is 1-5, and “command” is… the rest of that. This basically means that we’ve scheduled our command to execute at 7:30 am on Monday through Friday. Changing these options should be self explanatory. Now, let’s pick apart our “command” one part at a time.

/usr/bin/amixer set PCM 35\%

First thing to note, is that it’s a good idea to use full paths for any command you execute from cron. To find out the full path to a command, use

which {command}

In this case, we’re using amixer, which is a utility for changing the volume on your computer. I set mine to 35% to wake up to, but you can use anything. Also note here that we can’t use simply “35%”, because cron uses ‘%’ as a special character. Therefore, we preceed it with ‘\’.

Next thing to notice is “&&”. This essentially strings two commands together– it won’t start the next command until our first one has finished. So, onto our next command:

/usr/bin/X11/xterm -display :0 -bg black -fg white -e …

This is actually another compound command. xterm is another terminal that we are going to launch our music alarm in, so we can easily shut if off in the morning. We set all sorts of parameters to make the terminal look nice, but the important one is following the “-e”: that’s the command we will run in the new terminal:

/usr/bin/mplayer -shuffle -playlist ~/.alarm-playlist

Ahhh, finally, this is where we finally play our music. mplayer is a command-line music player with a very basic interface, and easy controls. You can run this line in a normal terminal now to make sure it works. We use the parameters “shuffle” to randomize our playlist, and then “-playlist …” to tell it what to play. Make sure you change “~/.alarm-playlist” to your own location.

And we’re done! At this point you should have a fully-functioning music alarm clock. Now go back and tweak it out with preferences that work for you.

Bonus: Move the alarm clock command to a shell script, and keep increasing your volume every minute or so.


Jan 18 2008

How-to: Mount a Network drive in Ubuntu

Tag: linux, ubuntuScott Wegner @ 3:16 am


Recently, one of my roommates bought an external network drive, so everyone in the house can upload their music and movies. It’s hooked right into the network through the router so it’s easy for anyone to access– pretty cool stuff. It was pretty simple to setup in Windows, but it took a little bit of research for Ubuntu. Specifically, we have Maxtor “Shared Storage II” hard-drive, and I wanted it to auto-mount each time I reboot my computer. I figured it out using the tutorial here, but the basics are below.

  1. First you will need to install the “smbfs” package. This is what we will use to mount the drive.

    sudo aptitude install smbfs

  2. We will also need a folder to mount the drive in. I used /media/public.

    sudo mkdir /media/public

  3. Then, we need to edit /etc/fstab. This is where we add all the information needed to find and mount the drive. Open /etc/fstab using the command:

    gksudo gedit /etc/fstab

    At the end of the file add the following lines:

    # Mount our network drive
    //SERVER/SHARE /MOUNT-POINT smbfs guest 0 0

    Where “SERVER” is the name of your drive on the network. This can be either a name or IP address. For example, mine was “MAXTOR”. Replace “SHARE” with the folder in the drive you’d like to mount– mine was “Public”. “MOUNT-POINT” is the directory we created earlier, such as “/media/public”. The rest of the parameters have to do with permissions, and also where you can add advanced options. For more information on the advanced preferences, see

    man mount.smbfs

  4. The final step is to tell the system to reload /etc/fstab and mount our drive. Do this with the command:

    sudo mount -a

    Then, you’re done! At this point you should be able to see the files in your drive with the command

    ls /MOUNT-POINT


Jan 15 2008

Ubuntu Desktop Course

Tag: ubuntuScott Wegner @ 6:31 am


From the makers of Ubuntu, there now exists an official Ubuntu 7.10 Desktop Course. This really shows the strength of the open source community; the Ubuntu team really goes full-circle with their support. The course is completely free (in both senses of the word), and available for download here. There is a student version, as well as a professor version.

Although it’s obviously intended to be taught in a mentoring or classroom environment, this would be a good read for anyone interested in moving to Linux. The read would probably be pretty rudimentary for somebody with a background Linux. However, it would be an excellent overview for somebody not only interested in using Ubuntu, but actually understanding what’s going on.


Jan 07 2008

Ubuntu Tutorial of the Week

Tag: linux, ubuntuScott Wegner @ 7:00 am

In my opinion, the best part of the open source community, is that it really is a community. There is all sorts of knowledge to be gained through the many freely-available resources for Ubuntu, and all open-source alike.

This evening, I found another little gem, called the “Tutorial of the Week,” hosted on the Ubuntu forums. Each week a new tutorial is posted. Some more useful than others, but they are all high-quality. You can subscribe to the weekly tutorials by subscribing to the RSS feed for this forum topic. This week’s tutorial is about Magic SysReq keys, which are helpful when your Linux computer freezes up.


« Previous PageNext Page »