With the invention of the Graphical Interface, GUI, computers became more accessible for the general public. With the increased accessibility, the technology became exceedingly popular and can now be found in every part of our lives. The draw back of the GUI is its inefficiency. From computer resource usage and work flows, there are some serious trade offs compared to its older command line counter part.
This doesn’t mean that one is necessary better than the other. Both are powerful computing tools, but very different. Here’s a quick comparison of the two interfaces.
Graphical Interface | Command Line |
---|---|
Very easy learn and master. | Steep learning curve. |
High resource usage. | Very lean. |
Typically only includes a fixed set of features. | Programs that do a single thing, but work well with other programs. |
Lengthy manual work flows and dialogues. | Cryptic commands, but very easily automated. |
Very complex programs to do even the simplest of things. | Very simple programs to that can accomplish very complex tasks. |
To make an argument for the strengths of the command line and why time should be taken to learn it, we’re going to look at resizing images.
Resizing an Image
I wanted to use this as an example to compare the work flows of a GUI application and the command line because of the graphical nature of the task. To give this example some real world application we’ll set up the following scenario; you’ve created a website to post pictures of your cats (real original right?). We’ll say the pictures on the site are required to be 200 by 200 pixels. To resize the images, the natural thing to do is use our favourite image editor. On Linux, this usually would be Gimp and on other system Photoshop. Let’s get started.
The first thing we’ll have to do is open the image in our image editor program. There is a number of ways to do this, the simplest would be to double click the image in the file browser. Oops, that possibly opened the image in an image viewer not our image editor. So now we have to right click the image to open a menu that we can select our image editor. If the program isn’t in the menu, a dialog can be opened where we can search for the program we want. Our other option is to go to the start or applications menu and find the program. Either way, we are usually given a splash screen as we wait for the program to load. Then we can also use the programs menus (usually file->open) to open another dialog where we can search for the image to open it.
Now that we have the program running with the image it’s time to resize it. We have to go through the programs menus to find the option to resize the image. This will open another dialog where we can type in the size we want the image. Also let’s be sure we find and check the option to maintain the images aspect ratio (usually a chain icon), we don’t want to squish or stretch the image. Hit the OK button and see what we have accomplished.
If everything went well, the image is now at the size we want it. We now want to save it as a different file so we don’t lose the original photo. Back to the programs menus to find the Save As function, (usually file->save as), to open another dialog box to choose the directory and file we want to save it as. We’ll want to create a new directory to save all our website images in. Most of these dialogues will let us do that right in the dialog, if not then we’ll need to switch to your file browser and do it. Once we’ve created the new directory and entered the new file name click OK and we’re done.
To do the same task on the command line, first open a terminal. This can be done from the applications menu, but most desktops have a short cut (on my XUbuntu system it’s CTRL-ALT-T
). In the terminal all we need to do is type in a couple commands as follows.
$ cd ~/images $ convert kitty.jpg -resize 200x200< ~/web/kitty.jpg
That’s it, we’re done. The only issue you might come across is that the convert
program might not be installed on your system. It’s part of the ImageMagick suite and I haven’t seen a main stream Linux distribution that it’s not available for.
Really Putting the Command Line to Work
Let’s say the cat website has become very popular and people want to submit their own pictures. Of course we want to increase its popularity so we start accepting their pictures. People have given us a hundred pictures to resize and upload to the website now. Lets go over our options.
With the graphical programs, we have the open, resize and save as process as I described earlier. If this doesn’t seem labour intensive before it should now. It probably won’t take long before we try to find a better way to do this or we start making plenty of mistakes from the repetition.
Is the command line any better? Typing in the commands earlier seemed a lot easier above, but typing in these commands 100 times is a much nastier task. Lucky for us we don’t have to type the same commands over and over again. We can simply create a text file (we’ll call convert-cat-images.sh
) and put our commands in that. We’re now creating what is known as a shell script and we’ll improve it a bit as follows.
#!/usr/bin/env bash # Change to the photos directory and make sure the web directory exists. cd ~/images # Find all the jpeg images and convert them into our web directory. for IMAGE in *.[jJ][pP]?(e|E)[gG]; do convert "$IMAGE" -verbose -resize 200x200 "~/web/$IMAGE" done
With the for;do;done
command added to our shell script, it will automatically find all the jpeg images in the directory and convert them for the web site. Our script will convert a single image or thousands of images all we have to do is type:
$ bash convert-cat-images.sh
Now that we have a shell script, with a little extra work, we can take it a step further. Using the program inotifywait
, the script can watch the directory we place the cat pictures in and it will automatically convert them without any intervention from us. If you’re not familiar with shell scripting it may look very cryptic, but the following will just sit there and do all the work for us.
#!/usr/bin/env bash # Use inotifywait to watch for new files in our images directory. inotifywait -m ~/images -e close_write | while read path action file; do # Make sure our file is a jpeg. if [[ "$file" == *.[jJ][pP]?(e|E)[gG] ]]; then # Convert the new image. convert ~/images/"$file" -verbose -resize 200x200 ~/web/"$file" fi done
Final Thoughts and Notes
There are many technologies included with GUI programs that can over come some of the short comings I’ve mentioned earlier like macros, plugins and scripting. These options can be obscure, add to the software bloat or have an even steeper learning curve than the command line. To further drive the point home, I’ve collected some stats of the programs convert and Gimp.
$ time convert ... real 0m0.047s user 0m0.076s sys 0m0.008s $ time gimp ... real 0m54.678s user 0m3.459s sys 0m0.420s $ ls -sh /usr/bin/gimp 5.8M /usr/bin/gimp ls -sh /usr/bin/convert 8.0K /usr/bin/convert
Just to be thorough, since the script will only be run while we’re logged in, here’s a more complete example.
#!/usr/bin/env bash # Scan all the images and convert anything that's new while the script wasn't # running. for IMAGE in ~/images/*.[jJ][pP]?(e|E)[gG]; do file=$(basename "$IMAGE") # Test the image if it's newer if [ "~/images/$file" -nt "~/web/$file" ]; then # Convert the image. convert "$IMAGE" -verbose -resize 200x200 "~/web/$IMAGE" fi done # Use inotifywait to watch for new files in our images directory. inotifywait -m ~/images -e close_write | while read path action file; do # Make sure our file is a jpeg. if [[ "$file" == *.[jJ][pP]?(e|E)[gG] ]]; then # Convert the new image. convert ~/images/"$file" -verbose -resize 200x200 ~/web/"$file" fi done