Command Line (CLI) Learning Linux Using Linux

Untar Several Files to a directory

Today we’re going to talk about the UNIX tar command.

Specifically, how to use tar to extract or unpack several archives (tar.gz files).  Only – instead of extracting them to the same directory, we’re going to extract them to a named directory.

This utility came in handy after downloading close to 10 gkrellm themes from . I have used the Gkrellm Monitoring tool over the years and was always disappointed with the (somewhat) `boring` stock themes.  I mean, if you’re going to have something sitting on your desktop, using resources to report on resources, then why not have it look slick, sick, or at the very least—”easy on the eyes”… right?

Why would anyone want to unpack tar.gz files with the command line in Linux when there are GUI tools available?

Right, the linux OS comes packed with tons of GUIs to handle files and archives.  We have programs such as FileRoller.  I have used it but imagine you have downloaded (oh I don’t know, twelve) archives that are themes for gkrellm.  Would you really want to use the manual effort of unpacking all of them first, directing the GUI (with more clicks) where to save the extracted files to?

What if I told you that you can accomplish the same in the Linux Shell with a basic loop over a set of tar.gz files?  You’d probably want to take the quick way I bet.  I know I did.

Ok, get on to it already.  Right. Here we go.  Firstly, I’d be a bad dude if I didn’t at least name some links that I googled along the way to building the handy-dandy loop I’m about to share with ye. very simple layout, 2 commands that do the trick.  And which has the command, and provides advice too.

Using the basic concept of the command, which unpacks a tar.gz to a named directory:

tar -zxvf tarball.tar.gz -C <directory>

Then, using a loop construct, where the find command provides a list of tar.gz files, we iterate over the list, unpacking each one to the specified directory following the -C.

The setup:

  1. Have all of your (downloaded) tar.gz files in the same directory.  You may want to get rid of archives that are unrelated to gkrellm themes.
  2. CD to that directory.  We will extract to the .gkrellm2/themes.
for fyl in $(find . -type f -name "*gz");\
do tar -zxvf $fyl -C /home/awsmeadam/.gkrellm2/themes; done

And that’s it.  Done.  Did you download more tar.gz themes?  Ok, run the command again.

If I run the command again, the previously-unpacked files will get over-written, won’t they?

Yes, they will.  In most cases, this is bad.  But since this is a theme unpack, you really have little to worry about unless you’ve edited or otherwise customized a theme.

Enjoy, and happy Linux-ing.




Command Line (CLI) Learning Linux Using Linux

Using Linux at Work

Use Linux at work—even if your PC runs Windows.

When I started using Linux, I wondered when I would get to actually use it “on the job”.  It wasn’t easy sometimes to work with a Windows-based computer—when there’s a skill set available that can help you do things faster on the computer.  This skill set is the Linux command shell, but as mentioned your computer runs Microsoft Windows.  What can you do??

At one job, I was able to install Perl.  At another job one had to submit a request (with business justification) to add software to a workstation.  Instead of Perl I thought “why not Python?”  Since both times it was a Windows XP Environment, I used the DOS command line (or “CMD” app) to execute the Perl or Python programs.

I used the DOS CMD because (at that time) I did not know about Cygwin.  Using the DOS CMD to execute programs is easy.  The difficult part is dealing with case-insensitivity (which hurts portability) and using a shell to navigate folders with spaces in the names is a real pain.

Using the DOS shell to execute programs is not the same as using Cygwin.  Cygwin is command shell that emulates a working Linux environment.  Installing Cygwin with the base packages is simple and will provide a good “starting point” for learning some shell commands.

Want to use Linux but your work computer is Mac OS-X?

OS-X (like Linux) is Unix-based.  It has a shell that’s known as “Terminal” and it is in the Applications/Utilities folder.  You can also (if the OS is 10.5 or later) use keystrokes (apple + Space bar) to show the “spotlight search” box at the top right and start typing Terminal.  When search finds “Terminal” then press ENTER and voila!  Say hello to the Darwin Terminal.

Darwin handles the basic stuff really well.  It’s capable of helping you learn Shell commands.  But it doesn’t do everything that a regular Linux distro does.  Darwin’s limits can be overcome by installing apps and libraries.  Most likely you will need your System Administrator’s help to get and install X11, MacPorts, or Fink to augment the BSD-derived Darwin environment.

Learning Linux Using Linux

Helpful Resources

Linux offers something for everyone, it’s not just for geeks.

Not everyone who uses Linux are programmers or geeks who feel they have to rebuild software applications.   To this day, I still have not recompiled a Linux kernel to improve performance.  But hey, enough advanced talk for now.  This post is dedicated to the vast resources for learning Linux that are out there (and have been out there for a long time).

Some suggestions for learning about (and using) Linux.

  • Listen to Linux-oriented podcasts.  There are many linux podcasts, but one I recommend for newcomers is the Linux Reality Podcast by Chess Griffin.  I listened to these podcasts while commuting on a bus.  Visit the Linux Reality RSS Feed (links and descriptions of each episode).   See my post on how to download multiple audio files with the command line.  Podcasts are an easy-to-follow resource available on your schedule.  If something sounds confusing, you can always google the phrase or method that doesn’t make sense.  Here is a link to various linux podcasts.
  • Visit forums (not newsgroups).  You’ll encounter the phrase “Google is your friend“.  Well, if google is a friend, then is an even better friend.  Chances are good that your questions about Linux have been asked already.  Viewing the online forums can show you just how big and (most of the time) helpful the Linux community can be.  Don’t overlook e-mail discussion groups such as Yahoo.
  • Tinker, experiment, and have fun at your own pace.  Browse the major book stores at their computing/programming section and you’ll find lots of Linux books.  I highly recommend The Linux Phrasebook by Scott Granneman for a few  reasons.  Firstly, it’s packed with helpful tips that I still use today.  Secondly, it’s small and portable.  Lastly, it’s a Linux book as opposed to an Ubuntu or Red Hat Linux book (which are fine books, but they are likely written with a specific OS version in mind, thus it can go out of date.  The Linux Phrasebook’s subtitle is “Essential Code and Commands”, making it a “general” resource that can help you get familiar with almost any Linux distribution.
Command Line (CLI) Learning Linux Using Linux

Useful Commands: introduction

Learning useful shell commands help save time & effort.

Do you have to use the command line?  No, you don’t have to use it.  I didn’t use it much when I started using Linux.  But, in my opinion, learning useful shell commands helps you get the most out of Linux.

What are some benefits of using the Command Line?

  • When you need help from the Linux community, many helpful solutions are expressed as commands to be run in the terminal.  It’s done this way for simplicity, accuracy, and consistency.  Many graphical-based (GUI) programs are “front-ends” where a user triggers events (via menu choices & button clicks) that execute terminal-based commands in the background.
  • Many jobs in the IT and web development field require candidates to be comfortable on the command line.  This implies the ability to issue shell commands and use console-based text editors such as vi and emacs.  Some jobs will also require you to understand (and perhaps troubleshoot) pre-written shell scripts in many languages.
  • For repetitive tasks, using the command line is just plain faster.  Why wait for a GUI program to open, click on things, or browse for file(s) to manipulate one file at a time?  You could simply type one or more commands [and options] into a prompt to accomplish the same.  When you find yourself issuing the same commands a few times, it becomes apparent to save these command calls in a text-based file (a shell script) to make the process even faster.  More on this to come.
Command Line (CLI) Learning Linux Using Linux

Save time on tasks

Learning basic Linux commands helps save you time when working on repetitive tasks.

Imagine you had a folder of images in *.bmp format.  File sizes of bmps are larger than jpg files because they contain more information.  To convert these bmp files to jpgs you could open a .bmp file in a graphics program (such as photoshop), select processes (such as ‘save for web’), set the format to convert to, and finally, save the file “as” filename.jpg.  Converting each image takes about five to six “manual” steps, depending on how you opened the source files and where you save the destination files.  If you had a folder with 100 images, it becomes tedious repetition.  There are better ways to do this.

You could create a batch process or “macro” in your graphics app that lets you record the individual steps performed on a single image, and then point the macro at a folder of source images and a folder to save the converted images.  Photoshop has “batch processing” that handles this fine.  Other programs (such as THE GIMP) are “scriptable” but some knowledge of Python is probably a must.

So we now have a way to convert images en masse with a GUI app, but is it the most efficient way? Is it reusable? It is reusable but you’ll probably have to reset the macro/batch settings if your source and destination folders change the next time you had to convert a lot of images.

Is there a quicker way to accomplish the same thing more or less?  There is, in my opinion, a better way perform mass image conversion in a predictable, reliable way.  It requires the command line terminal and an application known as “ImageMagick“.

Using Linux (the bash shell and ImageMagick) here are the 2 steps:

1. Use the “cd” command to get to the directory that has the bmp images
2. Issue this command:  mogrify -format jpg *.bmp
The above command was found at and I thank them for posting it.  It accomplished in one line the same work as a 50-line shell script.
Result: All of your bmp files have been converted and saved-as jpgs in the same directory
Warning: the “mogrify” command usually replaces the source, but when changing formats, Imagemagick is intelligent enough not to destroy the original bmp file.

2a. To store the new files in a folder other than the source (e.g. “bmpFiles/jpgs”), add the -path option in front of -format.  The command becomes:  mogrify -path jpgs/ -format jpg *bmp
The folder specified by -path must first exist, or the command will fail.