Categories
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 http://www.muhri.net . 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.  http://lejalgenes.com/ very simple layout, 2 commands that do the trick.  And http://www.linuxquestions.org/ 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.

 

 

 

Categories
Applications Using Linux Your Choice

Linux and MS Word Documents

I’m going to discuss something that’s a tad awkward.

It’s a call to improve on the Office Suite software available for Linux.  Is this really something worth writing about? It is, and I’ll tell you why.  After nearly 3 years at the same job, I find myself in a position shared by many—seeking work in a struggling economy.

The first thing I did was find a recent electronic copy of my resume.  Good news!  The most recent version just needed a little touching up.  Since I don’t own Microsoft Office software, updating an MS Word doc should be simple using Open Office dot org, yes?? Supposedly Open Office does the job for Word Documents.  Well, in my case…No.  Abysmally, failingly, No. Broke almost all formatting of the original document.  Caused me two to three times the work just to get it back to a ‘normal-looking’ state.  Mind you my resume was not heavily formatted to begin with.

But what about the FONTS?

Yes, fonts are a big deal when saving the document.  For resumes, it’s important to use a highly-readable font and keep the font size nice as well.  Chances are the resume will be scanned (for keywords) and also printed, faxed, and emailed to users of various Operating Systems.  Keeping the font as generic as possible will help.  Using exotic fonts that only come with Mac will not.  To make my text documents play nice, I installed the msttcorefonts (Microsoft TrueType Core Fonts) package.  This package will let you use fonts commonly found on the MS Windows Platform.

Ok, so is Open Office a total waste?

No, it is not.  I happen to like the Spreadsheet implementation.  For basic spreadsheets, it works great.  I have not tried to edit any MS Excel sheets with embedded macros (yet).  This is (from what I’ve heard) is the tricky part where Open Office has to do a lot of extra work to try and match all the macro languages and formulas—and on that—I’ll give them some leeway.

Your resume broke in OO.org; Can’t you use another app?

Usually, the answer to this question (when put to a Linux user) is yes.  With open source linux there are usually a plethora of substitutes available to try out.  However, when it comes to professional documents, it “just has to work” as the cliche goes.  I tried some text document office-suite alternatives.  First I tried Abiword.  Abiword also broke the formatting and made it’s own arbitrary decisions on font rendering, even though I had the correct MSTTCOREFONT installed (in this case, “Times New Roman”).  So, with the correct Font installed, it didn’t matter.  Document got very messed up.  Other alternatives include purchasing a licensed CD of MS Office and to make it work in Linux, Crossover Office (by Codeweavers), which I would consider purchasing.  MS Office (however) is not high on my list of things to buy.

Abiword is not a bad program.  I’ve written college term papers with it.  This was a start-to-finish document saved as *.doc as opposed to a document created in MS Windows.  I would just like to see Linux have the ability to inter-operate with .doc files that have a table as part of their layout.  I’ve used and recommended Open Office both on the Linux and the MS Windows platform and it usually works great.  Maybe I expected too much out of it?  Maybe tables in an MS Word Doc are not consistently imported into Open Office.

Although I like the idea of the Open Document Format, it’s not something I can count on for across-the-board compatibility with the business world.  This is critical.  If an employer can’t open your resume because it’s in .odt format and their version of MS Word is earlier than 2007, they will simply discard your resume.  They will not bother to read a file they can’t open.  It’s also possible that the .odt file format will make someone (not in the know) uneasy.  They are expecting a .doc file, so to keep yourself “in the game” it’s probably best to send them a .doc file for a resume.

Summary: how can we overcome these limitations?

Use whichever word processor program you want to (as long as it can save as MS Word format *doc).  Send your document to a friend or family member that has the “real” Microsoft Word on their computer and let them open, view, and print your document.  If it looks good, and prints as you expect, then you’re all set.  If not, then try another program or possibly take some time to reformat your document.

What did I wind up doing?

My final solution was to create the resume in iWorks for MAC (as I now use a macbook dual-booted with Linux).  Even if you’re writing your resume on MAC, remember fonts.  The Times New Roman font is recommended as the “generic” font most widely used for this purpose.  Do you have to use it?  No, you don’t.  Just remember the person receiving your resume will need to have the font on his/her computer or else your resume runs the risk of breaking while they see it or print it.  Not a good idea.  Resumes are typically given very little attention to start with as they are quickly scanned.  If a resumeI know Linux users typically do not lean towards conformity, but this is one time when it will work to your advantage.

For the Linux side of the computer, I rebuilt the resume, not relying on tables for formatting in cases where you want to “right-align” dates on the job.  This I felt was a safer “universal approach” and this version became my “default” resume version for better performance on as many platforms as possible.

I put the dates on their own line and right-aligned that line. But again, I was trying to get this done in short time to aid in job-hunting.  What works for you may be something altogether different.  Good Luck

Categories
Command Line (CLI) Using Linux

Cygwin rename command help

Rename files in cygwin, overcoming a limitation

Cygwin is a Linux-like environment for Windows.  It comes in handy when you’re at work, or at home, and your only operating system happens to be Microsoft Windows.  Cygwin allows you to issue Linux commands in a console in Microsoft Windows.  I find it easier to use than the DOS CMD prompt when I’m using a Windows OS.  It’s a great way to get things done quicker, and can be scripted—in a similar environment to a Linux Terminal.

So, let’s say you’re seasoned in the Linux shell.  You’re now at work, using Cygwin, and you want to rename a lot of files based on their name matching a pattern of letters or numbers or both.  This is where cygwin has some trouble.

By default, cygwin does not come with rename ability.  The package util-linux is required.

Cygwin’s rename command renames files if you tell it the exact name of your target file.  This is the same as the mv command. I’m no Cygwin expert, but this is the experience I’ve had.

In Linux, you can rename files using wildcards or pattern matches.  Cygwin’s rename command cannot seem to handle regexes in the same way.  I’ve tried a fair amount of googling to see why this command has limited behavior in cygwin.  I haven’t found a usable solution or add-on to cygwin to overcome this limitation.

So, here’s a shell script that uses sed, mv, and find to overcome the limitation.  I was unable to attach it, so I copied and pasted it below.

Please understand and accept the following disclaimer:  YOU MAY USE & MODIFY THE SCRIPT.  AUTHOR ASSUMES ZERO LIABILITY.  SCRIPT INTENDED FOR USE IN CYGWIN WITHIN A WINDOWS OS. SCRIPT COMES WITH NO WARRANTY, USE AT YOUR OWN RISK. Thanks.

#!/bin/sh
# cygrename : A Program to overcome limitation of cygwin's "rename" function.
# Author: Adam Teller
#
# Cygwin's "rename" doesn't work same way as *Nix "rename"
# because it cannot use wildcards. So, what can we do?
## Get the basename of the file if it matches search pattern.
## If 4th argument is set, change file to that suffix.
## else, match pattern within file name and keep same suffix
#
## Arguments: [search pattern] [replace pattern] [in suffix] [to new suffix]

LIST1=$(find . -type f -name "*${1}*\.${3}")
if [ -z "${LIST1}" ];
 then
   printf "Did not find any file matches, program will exit\n"
 exit 1
fi

for found in $LIST1;
 do
   MATCHEDFILE=$(basename $found);
   NEWFILENAME=$(echo $MATCHEDFILE | sed -e "s/${1}/${2}/");
   NEWFILENAMENOEXTENSION=$(echo $NEWFILENAME | sed -r "s/\.${3}//");

   ## TEST if $4 has been set, it means want a new file suffix
   if [ -z "${4}" ];
     then
       printf "$MATCHEDFILE :to be renamed as: $NEWFILENAME for files of type ${3}\n";
       mv $MATCHEDFILE $NEWFILENAME;
     else
       printf "${NEWFILENAMENOEXTENSION}.${3} will get $4 as its suffix, ";
       printf "and be renamed to ${NEWFILENAMENOEXTENSION}.${4} \n";
       mv $MATCHEDFILE "${NEWFILENAMENOEXTENSION}.${4}"
  fi
 done

 exit 0

Ok, I’ve copied the script and saved it, how do I use it?

  • Run your script from the same directory that has the files that you want to rename. I’ve named my copy “cygrename” (short for “cygwin rename”) but you may save your file with another name if you wish.  Make sure your script is executable. Use the cd command to get to the correct directory.
  • It accepts 3 or 4 command line arguments.
  • Invoke it in the shell using sh cygrename “what-to-find” “replace-it-with” “file extension” “new file extension” (optional).
  • The “file extension” argument does not include the dot (.), just use letters such as txt or doc or gif.
  • 3 arguments is the minimum and I’ve omitted the argument counting, you may however wish to add it into your program so that the 3 argument minimum is enforced.
  • In the even that the program does not find a match, the program exits.
  • The script uses the find command to look for files matching your pattern.  The result of a “find” usually includes the path information.  This is why we use the basename directive.  We just want the actual file name.  For example, a call to find might result in “./file1.pdf” and what we really want to use is “file1.pdf”
  • If the optional 4th argument is used, it indicates a desire to change the file suffix.  Otherwise, you can rename files and keep the file suffix as is.

Here’s a way to store & use the script.  This script is yours to use & modify as you wish.

  1. Store the script (assuming you’ve named it cygrename) in the “bin” folder in your user’s home folder.  In a windows environment, this is usually C:\cygwin\home\userName\bin.  When you first see this folder, it should be empty.  There is a cygwin “system” bin that will be full of executable files. THIS IS NOT what you’re looking for. You are looking for your user’s bin folder, which will be empty until you start putting files into it.
  2. Once you’ve used the cd command to get to the folder where the target files reside, you can invoke the script.  The script can be more easily invoked if you change the PATH variable in Cygwin.  Find instructions here.  Adding your user’s bin directory to your search PATH is recommended because it will allow you to call the script just by its filename, and won’t require the entire path name of the script.
  3. In this example, for simplicity, I’m assuming you have several files in the same “bin” directory as the script you have saved.  You have 4 image files named “picture1.jpg” “picture2.jpg” “picture3.jpg” and “picture4.jpg” .  You want to change “picture” to “img” and keep the files as *jpg  -From within the “bin”directory, Call the script as sh cygrename “picture” “img” “jpg”
  4. Your 4 jpg files should now be named “img1.jpg”, “img2.jpg” and etc.
  5. If (based on previous example) you wanted to change some web files (html) AND their file suffix, you could call the script as sh cygrename “website” “site” “htm” “html” and it would rename files such as “websiteLocation.htm” to “siteLocation.html” because you used the optional 4th argument.
  6. Please be aware that simply changing a file extension DOES NOT CONVERT a file.  What I mean is that if you change a file suffix of “img2.jpg” to “img2.gif” you are not converting to the gif file type.  The file becomes a jpg file named with an incorrect suffix.  This is not recommended.  However, changing an extension from .htm to .html is ok because it is a text-based file.  Image files, office suite files, and multimedia files should only have their suffix changed by using another application to convert the files.
Categories
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.

Categories
Applications Command Line (CLI) Using Linux Your Choice

Favorite CLI Linux Apps: Guake

Guake and Yakuake are drop-down terminal shells

Guake, or (if it’s a K Desktop Environment) Yakuake are “drop-down terminal shells” that appear when you press a button (usually F12).

If you usually open a terminal with every log in to your Linux desktop, there’s a more convenient (not to mention laaazy) way.  Simply tell the OS to run Guake after your user’s successful login. In Openbox, you’d add it to your autostart.sh file

The Guake terminal will notify you that it has started, and then auto-hides itself until you “un-hide it” by pressing the F12 key.  When you’re done with your current command-line stuff, simply press F12 again and Guake gets out of your way.

Categories
Applications Command Line (CLI) Using Linux Your Choice

Favorite CLI Linux Apps: Lynx

Lynx is a text-only web browser that runs in the shell.

Lynx is useful tool for those times when you want to extract only the web links from a web page.  Install lynx using the Synaptic (or other) package manager.

To view the hyperlinks of a given web page (google.com in this example), issue the command

lynx -dump http://www.google.com

It can also behave in a similar way to wget when you want to view the HTML source code of a web page.  The command to view the HTML source code is

lynx -source http://www.example.com

Click the following link to view a post where we collected links to mp3 files to build an unattended download list for the wget command.  Another feature of Lynx is that it allows you to view your pages as a web crawler/robot such as googlebot might see them.

Categories
Applications Using Linux Web Development Your Choice

Favorite Linux Apps: Web Coding

Linux Gui Applications for Website Design/Development

If you’ve got a site to develop or design, it’s easy to set up a working web server based environment in Linux. As stated in another post, you will need an IDE-style text editor (or at the very least you’ll need an editor that lets you keep several files open at the same time).

Run your pages using a web server such as Apache

While there are a few ways to do this, installing XAMPP for Linux by Apache Friends is simple.  It is free, well-documented, and you can set up a website very quickly.  By default, the web server is not started at boot time, (which to me is a plus) and starting/restarting can be controlled by issuing a command at the terminal.  XAMPP comes with MySQL and PHP 5, so it gives you just about everything you need to develop/create dynamic, database-driven web pages with the ability to reuse code (via PHP “include” directives).

Check your work in a few browsers to ensure consistency

If you’re running Linux it may seem a daunting task to evaluate your site’s appearance in Mac OS-X or Microsoft Windows.  Daunting yes, but you can come close.  Safari in Mac OS-X uses the “webkit” layout engine and you can view your pages in the konqueror web browser as a poor-man’s substitute.  It’s not perfect, but it’s close.  Mozilla firefox uses the Gecko layout engine so it won’t show you know how a site looks in Internet Explorer for Windows.  Firefox has a browser plugin called “IE Tabs” but I’m not sure if this reasonably captures the look and behaviour of the native IE.  Be sure to browse the Firefox Web Development add-ons page.

Categories
Applications Using Linux Your Choice

Favorite Linux Apps: Photo & Image

Linux Gui Applications for Photos and Image-based tasks

If you like to store your digital photos in “album-style” format, then I recommend Digikam.  It’s a KDE-based app so I suggest installing it using Synaptic on Debian-based systems.  Let Synaptic fetch other files as needed.  This saves you the headaches of “dependency hell”, which happens when an app you wish to install needs additional files and you have no idea which ones, where to find them, or how to properly install them.

To manipulate photos and images (such as cropping, or layering text captions onto them) I suggest you install the GIMP if it’s not there by default.  To find out if you have it, you can check your Applications menu or issue the following command in a terminal

which gimp

If the shell returns something like “/usr/bin/gimp“, then its installed.  If it returns nothing, you’ll need to install it.  The GIMP also does scaling (resizing images while keeping the aspect ratio) and is the closest freeware knockoff of Adobe Photoshop to my knowledge.

When I say “closest” I implore you to decide for yourself if your tasks can be done with free software.  The GIMP has a lot of great features such as transparency, layers, gradients, and more; but IT IS NOT CAPABLE of replacing Photoshop if that’s what your work requires.

If you’re doing commercial graphical work, your software choice (and budget) should be “an investment” and not “an expense”.  For many personal uses, the GIMP may be able to give you professional-looking results.

Categories
Applications Using Linux Your Choice

Favorite Linux Apps: Text Editors

Linux Gui Applications to write and edit text

For simple stuff I recommend Gedit that comes with GNOME-based Linux distros.  If you’re running a K-Desktop Environment (KDE) then Kate is really good.   Gedit works well for writing quick text snippets or copying text from websites to use later on.

For text writing in general, I like Pyroom which came pre-installed with Crunchbang Linux 9.04 and I happen to be writing this very blog post using Pyroom.  Pyroom reminds me of a typewriter interface—everything else on the screen (except for your text and a light border) is invisible.  Nothing to distract your eyes.  You only see the words that you type (non-formatted) on your (virtual) page.  Each time you hit “return”, your work just scrolls upward.  And it can “auto-save” your work at intervals that you set in preferences.

If you’re going to write scripts in various programming languages, then you’ll need a decent IDE-style code editor.  This comes in handy if you’re building a website, because you can create a “project” file that will group together the text-based files related to your website.

For this type of work, I use Geany, but have recently installed Aptana Studio to try it out.

The benefit of an IDE-style editor is that it can color the syntax so your commands don’t look like a big vat of text.  This makes reading, writing, documenting (or “in-code comments”) and troubleshooting your work much much easier.

Categories
Applications Using Linux

Upgrade Firefox: Ubuntu-style Linux

Upgrade your Firefox web browser.

Found this great tutorial site for upgrading Firefox web browser in Ubuntu (and Ubuntu-style) Linux distributions.  I’ve linked to the category page, because (to date) the latest version mentioned is Firefox 3.5.5 and currently (as of 2nd February 2010) Mozilla has released version 3.6 for download.  Click the following link to Mozilla Firefox downloads page.

Why is “Ubuntu and Ubuntu-style” relevant here?  Because the tutorial that I found applies to the Linux distro that I’ve been using for a while.  It’s called “Crunchbang Linux” and it’s also known by it’s symbolic alias of #!more info on Crunchbang Linux at this link.  The tutorial was written with Ubuntu in mind but will also work on Ubuntu derivatives such as Linux Mint and others.

The tutorial has 3 easy steps—possibly even 2 steps—depending on whether or not you’ve already upgraded Firefox or not.  If you have upgraded, you’ll need to follow all 3 steps.  If not, then you’ll only need 2 steps.  Note, I do not count the step of moving the downloaded Firefox *.tar.bz2 file to your /home/userName directory as a step because it’s something you will have to do (or not do) depending on your Firefox download folder preferences.  Early versions of Firefox have a default setting of downloading all files to the /home/userName/Desktop folder, however, depending on the flavor of Linux you are running, it may choose the /home/userName/downloads folder.

Thanks again to Jaxov for the awesome tutorial.  Just follow this link (or the link at the top of this post) and then look for the highest Firefox version available.  You should also visit the Mozilla download link above to see if a later version is available.  The steps in the tutorial are similar (if not identical) for recent versions of Firefox, so even if you are downloading version 3.6 you can still follow the directions in the tutorial for 3.5.5 and it will work.