A Personal Post

As you probably noticed, I took a bit of a break from posting. I figure coming off that break is as good a time as any to take some time to say a few things unrelated to the typical postings on here...

I've been pretty busy lately, but who hasn't? The thing is that I'm probably only going to be getting busier coming up. The reason for this is that in addition to working a full time job, I'm going to be going back to school part time. The plan is to take some computer science courses to be able to get into a graduate program.

So, what does this mean for grabag-linux? Hopefully it means a new source of ideas for posts, but it will also mean less time in which to write posts. I don't plan on quitting grabag-linux, but I don't think I will be able to keep up with the two posts per week pace that I've been keeping so far. Moving forward I'm going to cut back to one post a week; I figure Sunday night is a good time for that. Depending on how things go, I may have to fudge a day or two here or there, but I'll do my best to keep up with weekly postings.

So, what have I got planned coming up here? Well, I have plans for something big on Conky, some ideas for some smaller posts, and there are always lots of cool things to do with ImageMagick. But, if you have ideas for something you'd like to see me post on, feel free to shoot me an email or post a comment with your ideas.

See you next time, back with more focus on Linux in the post.


Bash Fork Bomb

Maybe you've heard of the bash fork bomb, maybe you haven't. You may have even seen the command that launches the fork bomb but don't know what it means or just haven't taken the time to think it through. Whatever the case, the bash fork bomb is something that is pretty interesting.

The short explanation of what the fork bomb does is that it recursively and exponentially spawns processes until your system crashes. So, what does this fork bomb look like? Here it is:

$ :(){ :|:& };:

So, that's great, but what exactly does it do? Well, let's break it down to explain what it is

  • :(){ :|:& }
    This defines a function : that does not accept any arguments, thats the () part. So, what does the function do? That's the part inside the curly braces.

  • { :|:& }
    This is the meat of the function, what gets run when you actually call :. When you call the function :, the first thing it does is to call itself and it pipes the output into another call of itself. So, short story here is that : calls itself twice; hence the exponential growth of processes spawned. The thing is that each time : gets called, that's another process running on the system. And, here's the kicker, the second time : is called, it is sent to run in the background with the &, so killing the parent process won't kill it.

  • ;:
    This is where we actually call : for the first time. The ; operator says to run the following command immediately after the previous one has finished. So, what does it do? Immediately after defining the : function this calls it, and then you play the waiting game until your system crashes.

You should probably not run this command on your system. If you're curious, fire up a virtual machine inside virtual box and give it a go there. Maybe I'll do that and post a video.

So, what good is this fork bomb? Well, this is something that sys admins use as a way to test limits on user processes. Once you launch this fork bomb, the only way to end it may be to reboot your system. The reason is that you have to destroy all instances of it to resume normal function.

See you next time.


Regular Expressions in Vim

Regular expressions are a fantastic thing to keep in your arsenal. To get a rough idea of what regular expressions are for, check out this xkcd comic on regular expressions if you haven't already seen it. Basically, regular expressions allow you to find and act on patterns in a file or group of files. I'm going to be looking at this from how you can use regular expressions with Vim, but regular expressions are certainly available in a much larger capacity throughout linux.

When learning a new programming language or programming concept, I find it useful to be able to see examples of code that other people have written. Unfortunately, this attack is probably not quite as helpful for regular expressions. Reading regular expressions can be quite tricky and may require a fair amount of time and energy to decipher a regular expression if you don't have any notes handy to explain them. Just a hint from my personal experience, you should probably leave a comments in any program or script you write to explain any regular expressions contained within. If you don't leave comments, you may find yourself looking back at the regular expressions in your code and feeling like you're reading brainfuck. The best way to learn regular expressions, in my experience, is to dive right in and start writing them yourself.

That all said, let's get started here with where the bulk of the work is done with regular expressions: metacharacters. Metacharacters, or escaped characters, are special characters that represent something else in a regular expression. That maybe doesn't make a whole lot of sense as you read it, but, hopefully, it will be clearer with the following list of metacharacters:
  • . - Represents any character except the "new line" character

  • \n - Represents the "new line" character

  • \s - Represents any whitespace character (e.g. space, tab, etc.)

  • \S - Represents any non-whitespace character

  • \d - Represents any numerical digit (0-9)

  • \D - Represents any non-numerical character

  • \x - Represents any hex digit (0-f), case insensitive

  • \X - Represents any non-hex digit

  • \o - Represents any octal digit (0-7)

  • \O - Represents any non-octal digit

  • \h - Represents any head of word character (a-z,A-Z,_)

  • \H - Represents any non-head of word character

  • \p - Represents any printable character

  • \P - Represents any non-digit printable character

  • \w - Represents any word character

  • \W - Represents any non-word character

  • \a - Represents any alphabetic character

  • \A - Represents any non-alphabetic character

  • \l - Represents any lowercase character

  • \L - Represents any non-lowercase character

  • \u - Represents any uppercase character

  • \U - Represents any non-uppercase character

I know, that's quite a list, but once you start writing some regular expressions, it's not too bad to refer to a list and before you know it, you'll find you won't even have to reference any list. Now that we've covered the metacharacters, the next place we want to look is how to denote the number of times something is to be repeated. The answer is with another class of escaped characters, called quantifiers. Quantifiers can be either greedy or non-greedy. Greedy quantifiers try to match as many times as possible. Non-greedy quantifiers try to match as few times as possible. This should be clear once I run down the list of quantifiers and use them in a couple examples:
  • * - Matches 0 or more of the preceding characters, as many as possible (greedy)

  • \{-} - Matches 0 or more of the preceding characters, as few as possible (non-greedy)

  • \+ - Matches 1 or more of the preceding characters, as many as possible (greedy)

  • \= - Matches 0 or 1 of the preceding characters

  • \{n} - Matches the preceding characters exactly n times

  • \{n,m} - Matches the preceding characters at least n times and at most m times, as many as possible (greedy)

  • \{-n,m} - Matches the preceding characters at least n times and at most m times, as few as possible (non-greedy)

  • \{n,} - Matches the preceding characters at least n times, as many as possible (greedy)

  • \{-n,} - Matches the preceding characters at least n times, as few as possible (non-greedy)

  • \{,m} - Matches the preceding characters at most m times, as many as possible (greedy)

  • \{-,m} - Matches the preceding characters as most m times, as few as possible (non-greedy)

We have enough to go through some common examples.

Find dates in YYYY-MM-DD format (two equivalent expressions):


There is another point that I should make, the / character is a special character in Vim, so it has to be escaped. For example, if you want to find dates in mm/dd/yyyy format:


Before we can do much with finding and repacing text within Vim, there is one more thing we should go over. A lot of times when finding and replacing text, we will want to keep part of the patern that we find and have it in a different (or even keep it in the same) place. This will probably be easier if I just do this with an example. The following will convert all dates from MM/DD/YYYY format to YYYY-MM-DD format:


There are a couple things to explain in the above example. The \( and \) characters are not actually searched for, but are used to delimit the paterns that you want to keep for the text to replace with. The \n characters are mapped to the paterns that are surrounded by \( and \) characters in order. In other words, the first thing in the "find" portion of the command surrounded by \( and \) characters maps to \1.

What if you want to turn a list with each value on separate lines into a comma separated list? Here's how:


What about that problem from the xkcd comic linked above? To find text formatted as an address I will assume the following about the address:
  1. The address is of the form that you would use to mail a letter in the United States

  2. The name is two words (i.e. first and last name only)

  3. The house number is no more than 4 digits

  4. The street name ends with a common ending: st., ave., blvd., etc. and none longer than four characters.
  5. The zip code is in extended form: #####-####

Before the example, I should explain that since the . character is a metacharacter, if you want to search for a period character specifically, we need to escape it like this: \.. Also, the ^ character is used by Vim to denote the beginning of a line and the $ character is used to denote the end of a line. Here's my example of a search command to find text formatted like an address:


I know that's kind of long, but let me break this example down one part at a time:
  • ^ - Makes sure that it starts finding the address at the beginning of a line

  • \w* - This is to find the first name

  • \s - This is to find the space between the first and last name

  • \w* - This is to find the last name

  • \n - This is to make sure that there is nothing else on this line

  • \d\{,4} - This looks for a house number of at most 4 digits

  • \P* - This looks for the text of the street name, and takes into account street names with hyphens or multiple words

  • \w\{2,4}\. - This looks for the street ending (e.g. st. or blvd) ending with a period character

  • \n - This makes sure that there is nothing else on this line

  • \P*, - This looks for the city name ending with a comma character, and takes into account cities with hyphens or multiple words

  • \s - Makes sure there is a space between the city and state abbreviation

  • \u\{2} - This looks for the two digit state abbreviation

  • \s - This makes sure there is a space between the state abbreviation and the zip code

  • \d\{5} - This looks for the first five digits of the extended zip code

  • - - This makes sure there is a hyphen separating the two sections of the zip code

  • \d\{4} - This looks for the last four digits of the extended zip code

  • $ - This makes sure that there is nothing else on this line

I know that this may all seem rather daunting, especially if you've never really worked with regular expressions, but the best advice I can give is what I said before: start trying to write your own regular expressions. Sure, you'll make mistakes at first; heck, I still make mistakes when I write regular expressions. But, at least you can learn from your mistakes.

Well, that pretty much does it for tonight's post. Have fun writing regular expressions. See you next time.


Vim: Visual Modes

I realized that in my last post, I mentioned the Visual and Visual Line modes in Vim without actually mentioning what you would use these for. The visual modes are used for selecting text and doing things like cutting and copying text, or in vim-speak deleting and yanking text, and for running commands on the selected text. Once you change to visual mode, moving the cursor around will automatically select text. Visual line mode works much like visual mode, only you can only select entire lines.

Cutting and copying in visual modes:
  • y - yanks (copies) the selected text

  • d - deletes (cuts) the selected text

Cutting, copying, and pasting in normal mode
  • :y or yy or Y - yanks the current line

  • :ny - yanks line number n

  • :n,my - yanks line numbers n through m, inclusive

  • :d or dd - deletes the current line

  • D - deletes from the current cursor position to the end of the line

  • :nd - deletes line number n

  • :n,md - deletes from line number n through m, inclusive

  • p - puts (pastes) text from the clip board starting to the right of the cursor, or below the cursor if putting entire lines of text

  • P - puts text from the clip board starting to the left of the cursor, or above the cursor if putting entire lines of text

Another important thing when you are editing anything is to know how to undo and redo changes made. This is really easy to do in Vim:
  • u - undo the most recent change

  • U - undo all of the most recent changes to the current line

  • Ctrl + r - redo the last undone change

That pretty much does it for this post. See you next time


Vim: an Introduction

If you saw my favorite linux applications post, then you'll know that Vim is my text editor of choice. Vim is a very powerful text editor, but it does come with a bit of a learning curve. I figured I would present an introduction into Vim that should help in getting to the point where using Vim is not a challenge.

I suppose the first thing that we should do is cover how to open a file in Vim:

$ vim filename

This will open the file filename, and if the file does not exist, Vim will create the file once you save. Probably the first thing everyone notices when they first open up Vim is that they cannot immediately enter text into the file. The reason for this is that Vim is a modal editor with the following modes: Insert, Replace, Visual, Visual Line, and Normal modes. Vim opens up by default in Normal mode which is where you can issue commands to the Program.

Another common problem that people face when they are first learning to use Vim is that they have a hard time keeping track of which mode they are in. The first thing I will point out is that at the bottom of the Vim window, it does tell you what mode you're in; it will display the following:
  • -- INSERT --: When in Insert Mode

  • -- REPLACE --: When in Replace Mode

  • -- VISUAL --: When in Visual Mode

  • -- VISUAL LINE --: when in Visual Line Mode

  • Or it will be blank when in Normal Mode

Part of the reason that people have a hard time keeping track of what mode they are in is that once the open up Vim, they set it to Insert Mode and leave it there. If you only set Vim to Insert mode when you are actually inserting text into the file and leave it in Normal mode otherwise, this will cut down on the confusion and encourage you to learn Vim commands, rather than just using Vim like Notepad.

OK, enough for my introductional rant, let's get down to the business at hand and start talking about how to use Vim:

  • Changing Modes
    Before we start discussing the commands and how to use the various modes, it may be a good idea to know how to change between them so that you don't get yourself stuck somewhere in unfamiliar territory. The first thing I will point out is that from Normal mode, you can get to any of the other modes, but from any other mode, you have to return to Normal mode before you can change to a different mode. Here is a list of hot-keys that you can use to change modes:
    • i - Puts you into Insert Mode where the cursor currently sits

    • I - Puts you into Insert Mode at the beginning of the current line

    • a - Puts you into Insert Mode one character after where the cursor currently sits

    • A - Puts you into Insert Mode at the end of the current line

    • s - Puts you into Insert Mode and deletes the character immediately under the cursor

    • S - Puts you into Insert Mode and deletes the current line

    • r - Puts you into Replace Mode for only one character where the cursor currently sits

    • R - Puts you into Replace Mode and keeps you there where the cursor currently sits

    • v - Puts you into Visual Mode where the cursor currently sits

    • V - Puts you into Visual Line mode on the current line

    • Esc - Returns you back to Normal Mode

  • Moving around in Normal Mode
    The first thing I will say about Normal Mode is that there are, what I call, hot-keys and commands. Hot-keys are keys that when pressed do something immediately. Commands start with either the : character for normal commands or the / character for search commands. Now I will run down a list of hot-keys and commands for moving around in Normal Mode in Vim
    • h - Moves the cursor to the left one character

    • j - Moves the cursor down one line

    • k - Moves the cursor up one line

    • l - Moves the cursor to the right one character

    • w - Moves the cursor to the right one word

    • b - Moves the cursor to the left one word

    • $ - Moves the cursor to the end of the line

    • ^ - Moves the cursor to the beginning of the line

    • gg - Moves the cursor to the first line of the file

    • G - Moves the cursor to the last line of the file

    • :n - Moves the cursor to line number n

  • Searching for and Replacing Text
    I shouldn't even have to explain how important it is to be able to do find and replace commands inside a text editor. I use them all the time and they make things go a whole lot faster. Searching for text in Vim is very easy:


    Typing the above while in Command Mode will move the cursor to the next appearance of text after the cursor's current position. To move to the next appearance, simply type n; to move to the previous appearance, type N.

    Replacing text is a little bit more complicated, but here is the basic syntax:

    :[region]s/[text to find]/[text to replace with]/[options]

    This may look a little daunting, but let's just break it down one piece at a time:
    • [region]
      The region tells the command what section of the file to do the find and replace on. You can use a number to do the replace only on one line, you can use two numbers separated by a comma to replace between two lines inclusive, or you can use the % character to replace throughout the entire document. Here are three examples:


      The first example with substitute the first instance of find with replace on line number 5. The second example will substitute the first instance of find with replace on each line between line numbers 5 and 10, inclusive. The third example will substitute the first instance of find with replace on each line in the entire file.

    • [text to find] and [text to replace with]
      These two sections are where the bulk of the work for the substitute command is done. This is where you identify the text that you want to replace and what you want it to be replaced with. The real power of these areas comes when you start using regular expressions, which I will cover in a future post.

    • [options]
      The options are where you tell the substitute command how to behave. If you do not specify any options, then it only performs the substitution on the first match on each line in the specified region. Here is a list of the options available:
      • g - This option will perform the substitution on all matches in the specified region

      • c - This option will prompt you for confirmation before making substitutions

      • i - This option will ignore case when looking for matches

      The above options can be combined. For example if you wanted to find do a substitue on al matches ignoring case, you could do:


    • Working with files
      To really be able to use Vim effectively, you're going to have to know how to open and save files as well as exit the program when you are done. Here is a list of commands for handling these actions:
      • :e [filename] - This will open the file [filename] in Vim

      • :w - This will save the open file

      • :sav [filename] - This will save the open file as [filename]

      • :q - This will exit Vim

      • :q! - This will exit Vim, discarding any changes since the last save

      • :wq - This will save the open file and exit Vim

      • :x - This behaves much like :wq, it will save the open file and exit Vim, but will not save if no changes have been made

    Well, that does it for a pretty basic introduction into Vim. See you next time.
  • 2009-07-15

    Searching files with grep

    It is pretty common that you might want to search for text within a file or directory. The best way to do that is with grep. The basic way to use grep is:

    $ grep [PATTERN] [FILE]

    • Searching a single file
      A simple example would be if you wanted to see all the different Linux kernels that show up in your /boot/grub/menu.lst file, you could:

      $ grep kernel /boot/grub/menu.lst

    • Searching for multiple strings
      What if you want to search for multiple strings in one file? Say, you want to see all the section headings, identifiers, and drivers in your /etc/X11/xorg.conf file. You could do this by using the -e option, and I'll also use the -i option to ignore case when searching:

      $ grep -i -e section -e identifier -e driver /etc/X11/xorg.conf

    • Searching multiple files
      It is also possible to use grep on all the files in a directory or just on a couple files that you want using either the * wildcard or by identifying files with the -f option. For example, let's say I wanted to search all my blog posts saved on my computer for ones that mention ImageMagick:

      $ grep -i imagemagick ~/blog/*

    • Displaying lines before and/or after match
      Sometimes you want to see the context in which the matched text appears. To do this we can use the following options: -A, for lines after the match; -B, for lines before the match; or -C, for lines on both sides of match. So, repeating the previous ImageMagick search to display 5 lines of text after the match, 5 lines before the match, or 5 lines on both sides of the match, we could use the following, respectively:

      $ grep -iA 5 imagemagick ~/blog/*
      $ grep -iB 5 imagemagick ~/blog/*
      $ grep -iC 5 imagemagick ~/blog/*

    • Displaying lines that do not match
      You might want to find lines that are not commented out in your /etc/apt/sources.list file. To do so, we can use the -v option, and we want to find lines that do not contain the # character. Since the # character is used to comment out lines in Bash, we have to search for this in a way that won't comment out the rest of the command (hint: regular expressions...I plan on doing a post dedicated specifically to the topic of regular expressions in the future), so we can search for this in either of the following two ways:

      $ grep -v "#" /etc/apt/sources.list
      $ grep -v \# /etc/apt/sources.list

    • Displaying only file names that contain matches
      It may be the case that you are really only concerned with the names of the files that contain your search string. To do this, we can use the -l (lower case L) option. I'll return to the ImageMagick example above here:

      $ grep -il imagemagick ~/blog/*

    • Displaying the number of matches per file
      To go one step further from the previous example, you can use the -c option to show you the number of matches you have in each file:

      $ grep -ic imagemagick ~/blog/*

      If you test out the above example, you'll find that it returns all the files that have no matches as well. If you only want to show the number of matches on files that actually have matches, we can use a little bit of redirection to pipe the output of one grep command into the input of another grep command (and a little help with regular expressions):

      $ grep -ic imagemagick ~/blog* | grep -v ":0$"

    • Displaying the line numbers for the matches
      Sometimes it is important to know where in a file the matches are, to help find this, we can use the -n option:

      $ grep -in imagemagick ~/blog/*

    Well, that does is for a pretty basic introduction into grep. As usual, there are still some other things that you can do with grep. The biggest thing that you can use with grep that I have not really covered is the use of regular expressions, but that is a topic deserving of its own post.

    See you next time


    Re-issue Previous Command as Root

    Here's a quick Bash tip. Don't you just hate it when you key in a command at the terminal and press enter only to realize that you didn't preface the command with sudo? Well, have no fear, because the command !! will re-issue the previous command.

    Now, I know you're probably thinking, "what good does that do me? I can just press the up arrow and then hit enter to run the last command." And you'd be right; however, what if you wanted to re-issue the previous command, but you wanted to put sudoat the beginning of it? Well, you could simply press the up arrow and then move the cursor to the beginning of the prompt and then key in sudo and press the enter key, or you could just do this:

    $ sudo !!

    The time that this happens to me most frequently is when I want to edit a file, for example, if I wanted to add a new repository to my /etc/apt/sources.list file, and run the following by mistake:

    $ vim /etc/apt/sources.list

    This will open up the sources.list file as read only, a fact which becomes perfectly clear once you change to insert mode in vim, with a Warning: changing a readonly file warning across the bottom of the window. At this point, it's easy to close the file, and re-open it as root with:

    $ sudo !!

    Well, that pretty much does it for tonight. See you next time.


    More on ImageMagick

    I've talked about ImageMagick before, and I figured it would be fun to cover some more features available with it. The reason I like ImageMagick is that it is really convenient for when you want to do the same thing to a large group of pictures. You may find yourself with some digital pictures that you want to make look more artsy by changing them to black and white or maybe to a sepia tone.

    The first thing I always do before using ImageMagick is to make backup copies of all my pictures I'm going to be editing. Once you're ready to get started, converting pictures to black and white is really easy:

    $ mogrify -monochrome input.jpg

    It really is that simple. The above will only convert the image named input.jpg. If you wanted to do the same thing to all .jpg images in a directory, you could do this:

    $ mogrify -monochrome *.jpg

    Sepia tone allows for a little more tweaking, because it allows you to input a threshold as a percent of the intensity, ranging from 0 - 99.9%. This is something that you would want to play with to get the desired result, but from my experience 80% is usually a good starting point. We could convert an image to sepia tone like this:

    $ mogrify -sepia-tone 80% input.jpg

    Again, you could use the same command on all .jgp images in a directory:

    $ mogrify -sepia-tone 80% *.jpg

    This still only scratches the surface of what you can do with ImageMagick. I'll be covering more of the features in future posts.

    See you next time


    Favorite Linux Applications

    I thought I would change things up a little bit this time and talk about some of my favorite Linux applications.

    • Terminal Emulator - Terminator
      If you do very much from the terminal, you'll surely find scenarios where you would like to have multiple terminal windows or tabs open at once. Tabs are great, but if you want to be able to see the contents of multiple terminal windows as once, they don't help. If you keep opening up new windows, it can become a chore to resize and manage all the windows so that you can see all of what you want. Terminator takes care of that by allowing you to split your terminal window and drag the split to resize your frames however you want. The split windows in Terminator allow for more efficient use of screen real estate as you don't have so much space taken up by window borders and decorations. To install Terminator:

      # apt-get install terminator

    • Text Editor - Vim
      I feel that everyone should know how to use at least one real text editor, and Vim is a great choice because it can run on almost any platform. Vim, or at least Vi, is installed by default on all Linux systems, so, if you know Vim, you'll always be able to use a text editor on any Linux machine. Aside from being easily available, Vim is a very powerful and extensible text editor. Vim can do syntax highlighting and spell checking as well as allowing you to write macros and use regular expressions. In fact, I use Vim to write all of my posts on here. To make sure you've the latest version of Vim installed from your repositories:

      # apt-get install vim

    • Web Browser - It's a two way tie:
      • Firefox/Iceweasel - I like Firefox for the great set of add ons available for using it. Also, it is very easy to get Java and Flash installed for Firefox in Debian.

      • Links2 - I like using Links2 for when I'm reading a lot of text in a blog or when I'm reading web comics. Links2 is a great lightweight web browser and has a graphical mode. To install Links2:

        # apt-get install links2

        To launch Links2 in graphical mode:

        $ links2 -g

    • Chat Client - Naim
      I know that Pidgin is pretty much the standard favorite chat client, but I like the simplicity and the keyboard commands/shortcuts in Naim. I will admit that Naim does have a bit of a learning curve, as with most console apps, but once you get past that, I find Naim to be a great chat client. To install Naim:

      # apt-get install naim

    • Video Player - VLC
      From my experience, VLC can play just about anything you throw at it. It is by far the easiest video player I've used to play DVDs with menus, and the interface is very simple. To install VLC:

      # apt-get install vlc

    • Audio Player - Songbird
      I like the clean interface and the ability to install add ons to Songbird. Check out my earlier post on installing Songbird.

    Well, that's a good start for my favorite Linux applications. What are your favorites?

    See you next time


    Installing Picasa in Debian

    Everyone needs a good photo album program. My favorite is Google's Picasa: it does a great job at organizing your pictures and even has some good tools for touching up your photos.

    We're going to go ahead and set up the Google repositories in Debian so that our package manager will handle upgrades as they become available. To do this, we will need to add the following to our /etc/apt/sources.list file:

    # Google repository
    deb http://dl.google.com/linux/deb/ stable non-free main

    As always, you can use your favorite text editor to add this:

    # vim /etc/apt/sources.list


    # gedit /etc/apt/sources.list

    Or, you can use echo and output redirection:

    # echo "# Google repository" >> /etc/apt/sources.list
    # echo "deb http://dl.google.com/linux/deb/ stable non-free main" >> /etc/apt/sources.list

    Now that we've got the repository added, we need to add the apt-key so that we can use it without error:

    # wget -q -O – https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -

    We are ready to update our packages list:

    # apt-get update

    Now that everything is up to date, we can install Picasa with the following:

    # apt-get install picasa

    This should put Picasa on your menu, and you're ready to start using Picasa.

    See you next time.


    Moving Windows with the Title Bar Off Screen

    In pretty much any operating system, if you want to move a window, you can click and drag the title bar of the window to move it. This shouldn't come as a surprise; in fact, you should probably be questioning why I'm even mentioning it. Well, what happens if you have a window open up and the title bar is off screen? If you use computers long enough, I promise, you will run into this scenario.

    You are more likely to run into this situation if you are using a netbook with the standard resolution of 1024x600. I wouldn't call this a common event with my Mini 9, but I'll be honest, it's happened a few times. So, what can we do when we find ourselves in this situation? Well, it turns out that there are a couple of things that we can do:

    • Keyboard only method
      Your Linux distro should have a keyboard shortcut for moving windows. In Debian, the default is Alt + F7. To find out what the keyboard shortcut for moving windows is on your installation, open up a window (pretty much anything will do..nautilus, firefox, etc.). Then open the Window Menu for the window you just opened (keyboard shortcut here is Alt + Spacebar), and you should see this on the list. Now to move a window, you simply need to press the keyboard shortcut you found and then you can move the active window with the arrow keys. You can also simply move the mouse after pressing the keyboard shortcut, and when you click the mouse, the window will stay where you put it.

    • Keyboard and Mouse combination method
      There is another default shortcut for moving windows, and I find this one easier to remember. We simply need to hold the Alt key and click and drag anywhere on the window we wish to move to reposition it.

    Now you should never have to worry about windows being positioned such that you cannot click and drag the title bar.

    See you next time.


    Bash Tutorial: Part 3

    We're back at it with more about using Bash. This time, rather than focus on new commands, we'll look at command operators and redirection. Command operators allow you to run multiple Bash commands in one statement. Redirection is not just a useful tool when performing magic, it's also a great way to change where the input and/or output of a command come from and/or go.

    Command Operators
    • ;
      To execute multiple commands one after another, we use the ; operator. In other words, the first command specified will run until completion before the second command starts. For example, if you want to update your index for the locate command and then search for the xorg.conf, we would do the following:

      # updatedb ; locate xorg.conf

      The above is essentially the same as running this:

      # updatedb
      # locate xorg.conf

    • &
      Executing commands in rapid succession all on one line is cool, but it can be more useful to run multiple commands all at the same time. For example, if you wanted to write a Bash script to launch firefox, pidgin, and songbird all at once, the important piece would be the following command:

      $ firefox & pidgin & songbird

      Another use for the & operator is if you want to run a command in the background. In other words, if you would like to run a command, but instead of waiting for the command to finish to regain control of the terminal, the control will immediately be returned and your computer will continue executing the command in the background. To do this, you simply end your command with &. If you wanted to update your index for the locate command, but don't want to wait for this to finish, you could do the following:

      # updatedb &

    • >
      Sometimes, the output of a command isn't immediately important, but you would like to save a record of it for later. To do that, we can use the > operator to create a new file, or overwrite an existing file, with the output of a command. For example, if you're trying to troubleshoot a networking problem, you might want someone to look at the output of the ifconfig command on your system. To save this output, you can do the following:

      # ifconfig > output.txt

    • >>
      Sometimes you want the output of a command to write to a file, but you don't want to lose the existing contents of that file. In this case, we can use the >> operator to append the output to the end of the specified file. For example, if you want to add the output of the lsmod command to the output file we just created, we could do the following:

      # lsmod >> output.txt

      If you have read some of my other posts, you have seen me use the two above redirection operators in conjunction with the echo command to create or add to files that control the behavior of some parts of the computer.

    • <
      There are also cases where you might want to specify the input of a command or program from a file. One example I can think of is if you have a text file that you would like to sort, you could use this as the input for the sort command like this:

      $ sort < unsorted.txt

      Of course, the above will simply output the text from unsorted.txt to the screen in sorted order. If we wanted to save the sorted version of the text file, we could combine input and output redirection to accomplish this:

      $ sort < unsorted.txt > sorted.txt

    • <<
      The << operator acts basically as a simple line based text editor. If you remember, we used it in the Installing Songbird post as an option for creating the Bash script to launch Songbird. Here's an example of the << operator with the sort command:

      $ sort << EOF
      > 3
      > 1
      > 2
      > EOF

      For another example, I've included the section where we used this in the Songbird post. In this example we use both input and output redirection to create a Bash script without dealing with a real text editor:

      # cat > /usr/share/Songbird/launcher.sh << EOF
      > #!/bin/sh
      > cd /usr/share/Songbird
      > ./songbird
      > EOF

    • |
      Pipes. Saving the best for last here, pipes allow you to redirect the standard output of one command to the standard input of another command. That may sound a little confusing as you read it, but let me give an example, that may clear things up:

      $ ls -al | less

      Using what we have learned about Bash command operators, the above command is basically the same thing as:

      $ ls -al > temp.txt ; less temp.txt ; rm temp.txt

      As you can see, piping commands is powerful and can save a bit of typing. My favorite use of pipes is with the grep command:

      $ ps -ef | grep firefox

      I'll cover both the ps and grep commands in future posts, but the above will give you information about any processes running on your system that have firefox as part of the process name. The above would be the same as the following:

      $ ps -ef > temp.txt ; grep firefox temp.txt ; rm temp.txt

    That does it for Part 3 of the introduction to Bash.

    See you next time.


    Bash Tutorial: Part 2

    For part 2 of my Bash tutorial, I didn't have the time to make it as big as I had wanted, so it's a little shorter. Let's get started.

    Displaying file contents
    • cat
      Concatenate files. The cat command can be used to type out the contents of a file or of multiple files. To type out the contets of your /etc/apt/sources.list file, just do:

      $ cat /etc/apt/sources.list

      If you want to type out the contents of multiple files right after each other, just do:

      $ cat file1 file2 file3

    • less
      less is similar to cat for displaying the contents of a single file. However, less is much better than cat for large files. If you use cat on a large file, you will only be able to see the end of the file because the contents would have scrolled out of view and even beyond the scroll buffer. less allows you to scroll through the entire contents of the file with the up and down arrows as well as the page up and page down keys. To use less to display the contents of your /etc/apt/sources.list file, just do:

      $ less /etc/apt/sources.list

    That's all for now. Next time we'll be back with part3.

    See you next time


    Bash Tutorial: Part 1

    I know a lot of people get scared any time the command line comes up when using a computer. Really, the command line shouldn't be scary, and learning how to use the command line in any operating system will only make you better at using your computer. As such, I figured I would do a couple posts as an introduction into the Linux shell I use most: the Bourne-again shell (Bash). Let's get started.

    Moving around the shell
    • pwd
      Print working directory. When you first open a shell, it starts you in your home directory, usually /home/[username], also abbreviated ~. If you're ever not sure what directory you're currently working in, the pwd command will tell you:

      $ pwd

    • ls
      List files. If you want to see what files and folders are in a directory, ls is the command to do it. If you just want to see the contents of the current directory, just use:

      $ ls

      In Linux, files whose name begin with . are hidden. To list the contents of the current directory and show hidden files, use:

      $ ls -a

      If you want to see more information about the files, such as who has permissions to read, run, and execute files; the owning user and group; the file size; and the last date modified, just use:

      $ ls -l

      The ls command does not simply restrict you to looking at files in your current working directory; you can look inside any directory you want. To see the long listing of all files in the /usr/bin directory, just use:

      $ ls -al /usr/bin

    • cd
      Change directories. Now that we've covered how to see your current working directory and listing files within directories, the next place to go is to change directories. In order to change directories to /etc/X11, just do:

      $ cd /etc/X11

      We can also use .. to move up a level:

      $ pwd
      $ cd ..
      $ pwd
      $ cd ../..
      $ pwd
      $ cd ../2.6.26-1-686
      $ pwd

    Working with files
    • cp
      Copy. The basic syntax for the cp command is:

      $ cp [source file] [destination file]

      For example, if you wanted to make a backup copy of your .bashrc file before making changes:

      $ cp .bashrc .bashrc-backup

      This is a good for copying single files, but if you want to make a copy of an entire directory, we can use the -r option to do a recursive copy:

      $ cp -r Documents Documents-backup

    • mv
      Move. The mv command works similar to cp:

      $ mv [source file] [destination file or directory]

      If you want to simply rename your .bashrc-backup file to fix edits made to your .bashrc file:

      $ mv .bashrc-backup .bashrc

      If you want to move a file into a different directory:

      $ mv .bahsrc-backup Backup/

      If you need to move or rename a directory, it works exactly the same with the mv command as moving files.

    • mkdir
      Make directory. If you want to create a new directory for all your digital pictures, just do:

      $ mkdir Pictures

      You can also create a any missing parent directories with the -p option:

      $ mkdir -p Pictures/Vacation/2009

    • rm
      Remove. If you want to delete the .bashrc-backup file, just do:

      $ rm .bashrc-backup

      If you want to delete a directory and all the files and subdirectories within it, you can use the -r recursive option:

      $ rm -r /Pictures/Vacation/2009

    • rmdir
      Remove directory. If you want to delete a directory, you can use the rmdir command. The rmdir command requires that the directory that you're trying to delete is empty:

      $ rmdir Pictures/Vacation/2009

      Similar to the recursive option that we've seen with other commands, we have the -p parents option to use with rmdir:

      $ rmdir -p Pictures/Vacation/2009

      The above behaves the same as the following:

      $ rmdir Pictures/Vacation/2009
      $ rmdir Pictures/Vacation
      $ rmdir Pictures

    Learning more

    We've covered a lot in this post, but there is still more that can be done with the above commands. Here are some resources that are available to learn more of the options available.
    • man
      Manual. The man command will bring up what is called the "man pages" for other commands. The man pages describe the syntax and available options for bash commands and programs. The syntax for man is as follows:

      $ man [command]

      So, if you wanted to see more information about the cp command:

      $ man cp

    • info
      Information. The info command is similar to the man command, but the info pages can be a little easier to learn from if you're not used to reading man pages. If you want to see the info page for the cp command:

      $ info cp

    That does it for Part 1 of the introduction to Bash.

    See you next time.


    Scheduling Commands with Crontab

    A common thing to want to do is to schedule a command to run at a specific time. We can do with with cron using the crontab command. By default, all users have permissions to use crontab; however, this can be changed by the existence of one of two files:
    • /etc/cron.allow - Only users listed in this file can use crontab
    • /etc/cron.deny - Users listed in this file cannot user crontab

    To see what you have scheduled via crontab, you can run the following command:

    $ crontab -l

    If you want to see what is scheduled to be run as a different user, you can use the following:

    # crontab -u [username] -l

    Where you would replace [username] with the appropriate username. Entries in the crontab file have the following syntax:

    [m] [h] [dom] [mon] [dow] [command]

    Now, I'll explain the above:
    • [m] - Minute to run; represented as a number: 0-59
    • [h] - Hour to run; represented as a number in: 0-23
    • [dom] - Day of Month to run; represented as a number: 1-31
    • [mon] - Month to run; represented as a number: 1-12
    • [dow] - Day of Week to run; represented as a number: 0-6 (Sunday is 0)
    • [command] - Command to run

    All of the timing options above can be replaced by an * where necessary to have your command run for all possibilities of that option. For example, in my post on locating files I mentioned scheduling the updatedb command. Let's say we want to schedule this command to run every morning at 6:30 am, then we would put the following into the root user's crontab file:

    30 6 * * * updatedb

    You can also use a comma separated list for the timing options to have a command run at multiple timings. For example, I have the bad habit of staying on my computer too late at night when I should be going to bed. To try to correct this, in the past, I have scheduled my computer to shutdown at 11:00 pm on Sunday through Thursday nights:

    55 22 * * 0,1,2,3,4 shutdown -h +5

    Now that we see the necessary syntax for entries in the crontab file, how do we use the crontab command to edit the file to schedule our commands? We can use the following to create or edit a user's crontab file:

    $ crontab -e

    The above will open your current user's crontab file in the current user's default text editor. Once here, you simply need to put in the entries you would like using the above syntax to schedule them. There are some other options that you can use with the crontab command:
    • -l [username] - Displays [username]'s crontab file
    • -r [username] - Removes [username]'s crontab file
    • -u [username] - Runs the specified contab command on [username]'s crontab file

    That pretty much does it for a basic introduction to scheduling commands with crontab.

    See you next time


    Finding Files on Your Computer

    It happens to all of us, we forget where files are saved. The good news is that with the locate command is a great way to find files on your computer. Before we can use the locate command, we've got to build an index of the files on our computer, which is easy enough: we just need to use the updatedb command:

    # updatedb

    It is a good idea to periodically run the updatebd command to keep your index current when you go to search for files. All you need to know to search for files is to know all or part of the file name. For example, if you couldn't remember where the xorg.conf file is saved, you can search for it like this:

    $ locate xorg.conf

    Here is an example output of the above command:

    $ locate xorg.conf

    Alternatively, if you were looking for the xorg.conf file, but could only remember that the filename started with xorg., you could search for it like the following:

    $ locate /xorg.

    There is more that can be done with the locate command, but this has covered about 98% of the times I've used it. I'll probably cover some more of these features in a later post. Another thing that is useful is to schedule the updatedb command via cron, which we'll be discussing next time.

    See you next time


    Turn Off Your Laptop Display

    One thing I find inconvenient about laptops is that they typically do not have a power button for the display the way standard monitors do for desktop computers. It turns out that if you have ACPI enabled in your kernel, there's an easy way to create a keyboard shortcut to turn off your laptop display.

    First thing, we can test to make sure this will work by running the following command:

    $ xset dpms force off

    That should turn off your laptop monitor until you move the mouse or type on the keyboard. Now to assign the keyboard shortcut, we need to open the configuration editor with the following command:

    $ gconf-editor

    You'll want to navigate the tree view on the left to the following path: /apps/metacity/keybinding_commnads. In the right panel you'll see that it lets you assign 12 commands (numbered 1 through 12). Just pick an empty one and assign it the following value:

    xset dpms force off

    Next you will want to find the following path on the tree view on the left: /apps/metacity/global_keybindings. Here you will see entries named run_command_x for x between 1 and 12. You will want to find the command that you edited earlier and in the Value space, type in the shortcut you want to run this with. For example, on the Mini 9, since Sleep is mapped to Fn+1, I decided to map this to Alt+1. To do this, I used the following value:


    Once you do that, you can test your keyboard shortcut and then close out of the configuration editor.

    See you next time.


    Convert Raster Graphics to Vector Graphics

    Have you ever wanted to convert a raster image (.jpg, .png, etc.) to a vector image (.svg)? Maybe not yet, but in my post on Installing Songbird, when we added Songbird to the menu, we did not have an icon to set for the menu item. It turns out that this is because we need a .svg image and the Songbird tarball only came with a .png image. No worries, though, we'll go through converting raster to vector and, specifically, how to convert the songbird.png image to a vector image for use on our menu.

    In order to do this, we're going to make use of two programs: ImageMagick and Inkscape. ImageMagick is a powerful command line utility for all kinds of graphics manipulations. Inkscape is more widely known as an open source GUI vector graphics editor; however, it also comes with its own set of command line tools. Let's make sure we have these two programs installed:

    # apt-get update
    # apt-get install imagemagick inkscape

    While it is possible to do this though the Inkscape GUI, I find the whole process easier and faster right from the command line. We will use ImageMagick to resize images, if necessary (refer to my first ImageMagick post if you have questions about resizing images with ImageMagick), and we will use Inkscape to do the actual conversion. The command to do the actual conversion is actually very simple:

    $ inkscape InputImage.png --export-plain-svg=OutputImage.svg

    When you run this command, expect to see a message similar to the following:

    (inkscape:30498): GLib-GObject-CRITICAL **: g_object_set_qdata_full: assertion `quark > 0' failed

    This is OK, your image still got converted. Now, let's use this to get the songbird.png image in place for use on our menu. Let's start by copying the songbird.png image into out home directory to make things easier:

    $ cp /usr/share/Songbird/songbird.png ~/

    Now we can use the mogrify command to resize this to a 48x48 pixel image and then use the inkscape command to convert this to songbird.svg:

    $ mogrify -resize 48x48 songbird.png
    $ inkscape songbird.png --export-plain-svg=songbird.svg

    Now, we'll just copy our new songbird.svg image into the same directory as all the other menu icons:

    # cp songbird.svg /usr/share/icons/hicolor/scalable/apps/

    If we set up Songbird in your menu the way I described before, then all we will need to do is to restart X (Ctrl + Alt + Backspace) and our new songbird.svg image should should show up in our menu.

    See you next time


    Batch Image Editing with ImageMagick

    If you take a lot of digital pictures, you may find that there are times when you want to do the same thing to a group of images. You could use the GIMP to edit all of your pictures one at a time, but that is a time-consuming process, especially if you just want to make thumbnails of your vacation pictures. The good thing is that there is a simple, yet powerful, command line utility that you can use to edit photos en masse: ImageMagick.

    Let's start by making sure that we have ImageMagick installed:

    # apt-get update
    # apt-get install imagemagick

    I'm going to start by going over two of the features of ImageMagick that I use the most frequently: resizing and cropping images. There are a lot more features available in ImageMagick, and I will probably cover more of them future posts.
    • Batch Resizing Images
      Digital SLR cameras are great for taking high quality pictures, the problem is that they typically take very large pictures that are too large for a lot of uses, such as posting on a blog or using in a presentation. Generally, the first thing I will do is open one of my pictures in the gthumb or some other image viewer that allows me to zoom in and out to find the percentage I want to shrink the images to.

      The next thing I do is I will make copies of all my images that I am going to edit so that I can retain the originals; I do this in a separate directory to minimize my own confusion:

      $ cd /[path-to-images]/
      $ mkdir thumb
      $ cp *.jpg thumb/
      $ cd thumb

      Now, we just use the mogrify command with the -resize option to resize our images. Let's say we want to resize them all to 20% of their original size:

      $ mogrify -resize 20% *.jpg

      Depending on the number of images you are resizing, this command may take a little bit to run, but it is a lot faster than editing each image by hand. If you wanted to resize your images to a specific size, say to create background images for your Mini 9, we would specify 1024x600 pixels rather than a percent:

      $ mogrify -resize 1024x600 *.jpg

      That pretty much does it for resizing pictures with ImageMagick.

    • Batch Cropping Images
      For my post on Installing Debian, I ran the installation in VirtualBox and took screen shots of the VirtualBox window to get the images I posted. The problem with this was, there was some of the window showing in the screen shots that I did not want to include in my post as you can see below:

      The good news is that this window border was identical on all of my screen shots, so I just had to figure out what I needed to crop on one image, and I could use that information in ImageMagick to crop all of the screen shots that I took.

      In order to figure out what I needed to crop, I opened one of the images in the GIMP. Using either the rectangle selection or the crop tool, I selected the region that I wanted to crop out of the image. Then looking on the tool window, I was able to see the position of the top-left corner of the selection as well as the dimensions of the selection:

      The Position section, indicated above in red, is what we will use for our offset with mogrify, and the Size section, indicated above in blue, is what we will use to define the size of the area we want to crop. We just need to use the information above with the mogrify command with the -crop option. But first, we'll copy the images to keep a record of the originals:

      $ cd /[path-to-images]/
      $ mkdir cropped-images
      $ cp *.png cropped-images/
      $ cd cropped-images
      $ mogrify -crop 639x480+1+44! *.png

      The exclamation point in the above command is there to make ImageMagick move the viewport for the image to the same as the crop offset. That may sound a little confusing, so I've included a screen shot of what a picture looks like in the GIMP if you don't use the exclamation point in the above command:

      So, you are best to use the exclamation point in the command, and when you use it, you're picture will look like this:

      That's about all there is for batch cropping images with ImageMagick

    See you next time.


    Installing Songbird

    If you want to use Songbird as a media player in Debian, you may run into some problems since there isn't a Songbird .deb installation file compatible with Debian. You're in luck, though, because it's not too difficult to install from the tarball available from the Songbird website. We'll start in the logical place by downloading the tarball here, or you can use wget:

    $ wget http://download.songbirdnest.com/installer/linux/i686/Songbird_1.1.2-1042_linux-i686.tar.gz

    Now we want to untar the installation file:

    # tar -xvf Songbird_1.1.2-1042_linux-i686.tar.gz -C /usr/share/

    The above will create a directory Songbird inside /usr/share/. This way we can keep things clear in your home directory. In theory, we could actually run Songbird right now; however, we can do a few easy things to make it much easier to use Songbird. First thing, we're going to create a shell script, /usr/share/Songbird/launcher.sh, containing the following text:

    cd /usr/share/Songbird

    We can create this file with our favourite text editor or straight from the command line with cat:

    # vim /usr/share/Songbird/launcher.sh


    # gedit /usr/share/Songbird/launcher.sh


    # cat > /usr/share/Songbird/launcher.sh << EOF
    cd /usr/share/Songbird

    The next thing we need to do is to change the permissions on the /usr/share/Songbird directory and all child objects so that users other than root can run Songbird:

    # chmod 755 -R /usr/share/Songbird

    Now the next thing we want is to be able to launch Songbird simply by typing songbird into the terminal, the run prompt, or deskbar. We can do that simply by creating a symbolic link to the shell script we just created:

    # ln -s /usr/share/Songbird/launcher.sh /usr/bin/songbird

    Now you can go ahead and test this out simply by typing songbird into the terminal, run prompt, or deskbar. The next thing that many people will probably ask for is to add Songbird to their menu. We just need to run alacarte to do this:

    $ alacarte

    Alacarte should also be accessible from the System menu (System -> Preferences -> Main Menu). You'll want to select the menu item you would like Songbird to appear under in the Menus panel to the left, and then click the New Item button on the right. In the Create Launcher window, you'll want to use the following settings:
    • Type - Application
    • Name - Songbird
    • Command - songbird
    • Comment - Whatever description you would like, or leave it blank

    Click OK to create the launcher and then make sure the Show checkbox is checked for Songbird in the Items panel in the middle. Then click close and you will now be able to launch Songbird from your menu.

    Enjoy listening to your music in Songbird. See you next time.


    Raising Skinny Elephants

    Even with as stable as Linux is, at some point, you will probably run into a situation where your system becomes unresponsive. Most of the time, restarting X by pressing Ctrl + Alt + Backspace will take care of the problem. However, if you are unfortunate enough to find yourself in a situation where your machine will not respond to this, there is still something you may be able to do: Raise the Skinny Elephant.

    I know this sounds pretty silly, but it is just a mnemonic device to help you remember what to do. Before I explain that, let's check to make sure your system has the sysrq key enabled:

    $ ls /proc/sys/kernel/sysrq

    If your system finds this, then it is enabled. On most keyboards, the sysrq key is the same as the Print Screen key. The exception here is on some laptop keyboards, Print Screen and sysrq are mapped to different function keys. This is not the case on the Dell Mini 9, both Print Screen and sysrq keys are mapped to fn + 7. Now onto the mnemonic device:

    Raising Skinny Elephants Is Utterly Boring

    So, now that we know the mnemonic, what do we do with it? We can type the following key strokes to reboot an unresponsive system:

    Alt + sysrq + r
    Alt + sysrq + s
    Alt + sysrq + e
    Alt + sysrq + i
    Alt + sysrq + u
    Alt + sysrq + b

    Between each line, we should wait a couple seconds to let the system finish what we're telling it to do. Speaking of, just what are we telling the system to do?

    r - Puts the keyboard in raw mode
    s - Syncs the disk
    e - Terminates all processes
    i - Kills all processes
    u - Remounts all filesystems in read only
    b - Reboots the system

    When your system reboots, fsck will run to check your disk, just let it run.

    I just want to restate that this is not something you should do normally, it is there as a failsafe for when everything else fails: when Ctrl + Alt + Backspace or rebooting from the command line do not work.

    See you next time.

    Edit: Thanks to Scowdich for pointing this out:
    Typing Alt+SysReq+r,e,i,s,u,b will do the same thing, but will skip the unnecessary fsck on reboot.