File Under: Software

Install and Use Cygwin

Windows provides a little command line utility called cmd.exe, but it has nothing close to the power of the Unix command line and its integrated free tools and applications. Fortunately, especially for addicts of the Unix way, there’s a way to use a lot of the Unix tools in a Windows environment. That way is a free piece of software called Cygwin. Cygwin is a Unix-style command line for Windows; it comes with a selection of hundreds of free tools as well.

Installing Cygwin

Installation is shockingly easy. Download onto your Windows computer, and then run the program.

The setup utility walks you through installing Cygwin. When prompted to choose a download source, select “Install from Internet”. Choose a reasonably local download mirror from the choices it offers.

Next, it’s time to choose which software packages you want included in your Cygwin installation. Choosing All is the easiest option, and it means you won’t get that annoying “command not found” error often — but it takes a long time to download everything, and it takes a lot of drive space. The packages are broken down into categories — Database, Devel, Editors — for ease of picking and choosing if you want to grab just the packages you need. I recommend blowing up the window to full screen for this process.

After you’ve picked some or All and clicked the Next button, all those packages will download from the mirror site and be installed on your machine. You can watch a captivating progress bar while this happens.

Finally you should see the “Installation Complete” message.

If, later on, you realize that now you want a package you opted not to install the first time around, just run setup.exe again and choose it. It’ll be added to your Cygwin setup, and any updates will be downloaded too, while your existing installation is left intact.

X and Rxvt

The X Window System is available via xorg. To get it installed, install the X-startup-scripts package from the X11 category. Then, the batch file C:cygwinusrX11R6binstartxwin.bat will start Xorg for you, indicated by a black X in the system tray. The X server will sit around waiting for X applications to use your display.

The default Cygwin shell window may be a little cumbersome sometimes, so the rxvt program provides a window that is more compatible with traditional Unix/Linux programs, especially those that might be running remotely. To give yourself a convenient Rxvt icon, make sure that the Rxvt package is installed (categorized under Shells), and create a windows shortcut to C:CygwinbinRun.exe. Then edit the shortcut’s properties so the Target reads: C:cygwinbinrun.exe /bin/rxvt -display :0.0 -e bash -login (If you decide not to use X, then leave off the -display :0.0, but some graphical programs may not work.)

Using Cygwin

Launch Cygwin in the way you ordinarily launch programs, be that the Start menu or a shortcut system. It’ll bring up a terminal window running the bash shell. Using bash, just a few keystrokes can accomplish things it takes unassisted Windows megabytes of shareware to do.

There’s a little bit of rethinking necessary if you’re used to Windows’ conventions. The C: drive is referred to as /cygdrive/c/ and so forth; your Desktop is at /cygdrive/c/Documents and Settings/User Name/Desktop

To create a whole series of nested directories:

mkdir -p /cygdrive/c/Documents and Settings/User Name/My Documents/One/New/Directory/After/Another

(note that you need to put a before space characters in directory and file names)

You can rename multiple files, too. Say you have a whole folder called mypics full of JPEGs with names like IMG_0203.JPG, and you’d like to add the prefix “vacation2006″ to each file:

for i in mypics/IMG*.JPG; do mv $i vacation2006-$i; done

To remove all empty files and folders from a directory:

find . -empty -maxdepth=1 -print0 | xargs -0 rm -rf