Bringing Unix commands to a Windows world

You can make your life a little easier and more productive by adding some Unix power to your Windows system.

For a fairly extensive collection of Unix tools -- including most of the essentials like less, tail, awk and grep, mv, dd, bzip2 and bunzip2 -- on your Windows box, install cygwin. Cygwin is a collection of tools that provide Unix commands on a Windows system. These commands can be as useful on the Windows command line (i.e., within the command prompt window) or within scripts (e.g., .bat files) as they are on Unix.

To get cygwin, browse your way over to cygwin.com. You will find a setup.exe file that you can download and run. If you don't have administrator access on your Windows system, try renaming the setup.exe file to cygwin.exe or something like that before

double clicking on it. That might get you around any installation problems.

Clicking on the setup.exe (or cygwin.exe) file will open a GUI that allows you to look through a list of the tools that will be installed by default. Click on the + sign to the left of a software category to list its contents. You will then see version numbers next to the tools that will are to be installed. Click on the "Skip" marker if you want to add a tool to the installation rather than have it skipped.

          Skip      n/a   n/a   418k  gcc-tools.epoch1-automake (gcc-special) a tool for ...
          Skip      n/a   n/a   578k  gcc-tools-epoch2-automake (gcc-special) a tool for ...

Once the installation is complete, open a cygwin terminal by clicking on the link added to your desktop or the item (Cygwin Terminal) in our start menu. You should find

yourself sitting in your home directory (e.g., /home/myself) and if you type pwd, it will tell you just that. Try some Unix commands such as date, ls -la and man bash. Go ahead and cd .. and ls /usr. How about cat /etc/passwd? You might even start thinking you're working on a Unix system -- at least until you come across a command that's missing -- like clear (although there are other ways to accomplish the same thing).

Go ahead and cd over to /usr/bin and try ls and ls | wc -l commands. You're going to see a lot of files, some with .exe extensions, some without. You'll also see .dll files.

myself@WindowsBox /usr/bin
$ ls
[.exe                      dumper.exe                 peflagsall
addftinfo.exe              echo.exe                   pfbtops.exe
afmtodit                   editrights.exe             pgawk.exe
apropos                    egrep.exe                  pic.exe
arch.exe                   env.exe                    pic2graph
ash.exe                    envsubst.exe               pinky.exe
awk                        eqn.exe                    pldd.exe
banner.exe                 eqn2graph                  post-grohtml.exe
base64.exe                 ex                         poweroff
basename.exe               expand.exe                 pr.exe
bash.exe                   expr.exe                   preconv.exe
bashbug                    factor.exe                 pre-grohtml.exe
bunzip2.exe                false.exe                  printenv.exe
bzcat.exe                  fgrep.exe                  printf.exe
bzcmp                      file.exe                   ps.exe
bzdiff                     find.exe                   ptx.exe
bzegrep                    fmt.exe                    putclip.exe
bzfgrep                    fold.exe                   pwd.exe
...
$ ls | wc -l
393
$

You might even cd down to / and start to wonder what to do next.

me@WindowsBox /
$ ls
bin       Cygwin.bat  Cygwin-Terminal.ico  etc   lib   tmp  var
cygdrive  Cygwin.ico  dev                  home  proc  usr
myself@WindowsBox /
$ ls var
cache  cron  lib  log  run  tmp

myself@WindowsBox /
$ ls tmp

myself@WindowsBox /
$ ls dev
clipboard  console  full  mem     port  pty1    sda   sr0     stdout   windows
conin      dsp      kmem  mqueue  ptmx  random  sda1  stderr  tty      zero
conout     fd       kmsg  null    pty0  scd0    shm   stdin   urandom

That's quite a nice Unix playground sitting on our Windows box, but what can you do with it other than take a Unix break from Windows? Actually, quite a bit. For one thing, you can escape the confines of your C:\cygwin directory and go to your C: drive with a cd /cygdrive/c command. This takes you to C:, but still from within your Cygwin Terminal. Examine your Windows files:

$ cd /cygdrive/c
$ pwd
/cygdrive/c
$ ls
$Recycle.Bin            install.res.1036.dll
Boot                    install.res.1040.dll
bootmgr                 install.res.1041.dll
BOOTSECT.BAK            install.res.1042.dll
cygwin                  install.res.2052.dll
Dell                    install.res.3082.dll
Documents and Settings  Intel
...

You can then wander up to your home on your Windows system and examine your files with Unix commands:

myself@WindowsBox /cygdrive/c/Users
$ cd myself

myself@WindowsBox /cygdrive/c/Users/myself
$ ls
AppData
Application Data
AWS
BoogieMan
Contacts
Cookies
Cruise 2013
...
$ less testing123.txt
This is a test

In fact, you can even zap the contents of a file using /dev/null:

myself@WindowsBox /cygdrive/c/Users/myself
$ ls -l testfile
-rwx------+ 1 mself None 230 May 27 09:47 testfile
$ > testfile
$ ls -l testfile
-rwx------+ 1 mself None 0 May 27 09:49 testfile

Similarly, from Windows, you can open a command prompt and cd over to C:\cygwin or even go through the Windows GUI if you are so inclined.

You can also expand your Windows search path within a batch file in much the same that you would a Unix path, but with a couple small changes. Instead of $PATH, Windows uses %path%. Instead of : as a directory separator, Windows uses a ;. You can then use Unix

commands within your Windows scripts to add functionality and ease to your tasks.

path=%path%;C:\cygwin\bin

The beauty of cygwin is that you can use Unix commands to work on files on Windows systems. This can be a lot faster than moving the files you need to work on to a Unix system, processing them there, and moving them back -- especially if you have work you'd like to schedule to run off hours. All the awk'ing, sorting, grep'ing, wc'ing and uniq'ing that you want to do can be done in place.

An extensive online user's guide is available from the cygwin site and in several formats, including a PDF -- easier to use in my estimation -- or you csan just read the documentation online at http://cygwin.com/cygwin-ug-net/cygwin-ug-net.pdf.

By the way, you don't have to pay to use cygwin, but it's wise to read the copyright section of the FAQ if you are planning to use it for porting a proprietary application.

Top 10 Hot Internet of Things Startups
Join the discussion
Be the first to comment on this article. Our Commenting Policies