Archive: ‘Using Linux’

Cygwin rename command help

03.17.10

Posted by adamlinuxhelp  |  5 Comments »

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.

Using Linux at Work

02.24.10

Posted by adamlinuxhelp  |  No Comments »

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.

Favorite CLI Linux Apps: Guake

02.19.10

Posted by adamlinuxhelp  |  No Comments »

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.

Favorite CLI Linux Apps: Lynx

02.19.10

Posted by adamlinuxhelp  |  1 Comment »

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.

Favorite Linux Apps: Web Coding

02.12.10

Posted by adamlinuxhelp  |  No Comments »

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.

Favorite Linux Apps: Photo & Image

02.12.10

Posted by adamlinuxhelp  |  No Comments »

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.

Favorite Linux Apps: Text Editors

02.12.10

Posted by adamlinuxhelp  |  No Comments »

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.

Upgrade Firefox: Ubuntu-style Linux

02.02.10

Posted by adamlinuxhelp  |  No Comments »

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.

Dual-boot Linux

02.01.10

Posted by adamlinuxhelp  |  No Comments »

What do we mean by “Dual boot” anyway?

Dual boot means that our computer has 2 (two) Operating Systems.  We choose which one to “Boot” into when the computer starts.

Many Linux users dual-boot as a way of sharing a computer.  This saves you money.  Why? Because you keep the computer you already own and install Linux on it.  I have dual booted with Windows XP and more recently, Mac OS-X Snow Leopard.  Since I don’t have a PC running Vista or Windows 7, this post will only discuss Win XP & Snow Leopard dual booting with Linux.

How to dual boot with Linux and Windows (or Mac)

There are many online tutorials available.  I won’t post a tutorial here because that would be  “reinventing the wheel”.  I will only describe the fine points.  If I’ve done my job, then the process might be less intimidating when it’s time to set up your dual-boot environment.

Dual booting with Windows and Linux

  • First things first.  If your computer already has Windows or Mac OS-X on it, then backup your documents.  While the process of dual-booting is better than it was years ago, things can still go wrong.  Don’t lose your files.  Be safe, not sorry.
  • If you’re running Windows XP (yes, I know this post may be outdated as we are already within the era of Windows 7) clean up your hard drive (junk files, temporary files) and then defragment.  It’s very important to defragment.  You don’t want documents and Windows Operating system files scattered all about the hard drive.
  • Open a web browser and get a cup of coffee or tea.  Why?  Because I think it’s a good idea now to view some online tutorial (with screenshots) about dual-booting.  You may even want to print out the pages of the tutorial so you can refer to them while performing the various steps involved with dual-booting.  The tutorial is easy to follow.

Dual booting with Mac OS-X and Linux

I recently purchased a MacBook and I wanted to dual-boot this machine.  Don’t get me wrong, I love the Mac OS-X.  There are just things that I needed to set up quickly that I’m still learning how to do under Mac OS-X.  Under Linux there are things that I’ve done enough times that it’s become second nature.  Here are some notes and suggestions.

  • To dual-boot Mac OS-X and Linux, you will be creating at least one partition on your hard drive.  It’s not hard to do.  Here’s a link to the tutorial that I used.
  • I suggest following the instructions in the tutorial.  It helped me set up my Mac OS-X and Linux dual boot machine very quickly.  Same approach as before; backup your documents, do a cleanup if necessary, and read the tutorial completely before performing the steps.
  • Pay extra attention to the bootloader installation step at tutorial’s end.  If you install the bootloader in the wrong place, things can break—badly.
  • Even though my MacBook is a 64-bit machine, I installed a 32-bit Linux distro.  I would recommend a 32-bit distro as it’s likely to be more stable for uses such as these.
  • The tutorial assumes that you’re running an Intel-based system (also called “mactel”).  Be sure that this is the case before following any tutorial.

Helpful Resources

01.23.10

Posted by adamlinuxhelp  |  No Comments »

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 google.com/linux 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.