A Better Windows Command-Line

Linux gurus who also use Microsoft
Windows
can have Linux-like command-line tools to make life
easier and less confusing.

Unless you live in a penguinista monastery, you probably use Microsoft Windows at least some of the time, maybe most of the time if it’s your desktop environment. If your mind and your fingers are trained for the speed of the Linux command-line and a shell like bash or zsh, what’s a penguin to do?

The January 2004 column “Cross-Platform Command Lines” covered ways to use Linux-like features of Microsoft’s shell CMD. That shell still requires some thought; you can’t just type a Linux command and expect it to work just as it would in, say, bash.

A powerful and free alternative that’s much better than CMD is the Cygwin package from http://www.cygwin.com/. A Linux-like environment for Microsoft Windows, Cygwin has two parts. One is a Linux API emulation layer, cygwin1.dll. The other is a collection of tools with a Linux look-and-feel: programming tools, including common Linux programming and scripting languages, system tools like cron, text editors, shells and command-line utilities such as ls.

For a long-time Unix/Linux geek like your columnist, opening a Cygwin shell window on a Windows box is like coming home. Windows drives become pathnames with the slashes in the right direction. For example, C: becomes /cygdrive/c or just /c). Files can be grep ped and paged with less. Filesystem searches with find do so much more than Windows’s Search tool. Shell setup files like .bashrc can be copied (or used via a network!) basically as-is.

Let’s dig in to this rich environment!

Installing Cygwin Correctly

Start the installation by downloading a Windows program named setup.exe (to avoid confusion later, you might rename it cygwin_setup.exe) and running it. The main interface is a huge list of the available packages and the corresponding version numbers. The list is a bit intimidating, but it’s worth looking through every category for tools you might want; a lot of common Linux packages have been ported to Cygwin, and there might be some that are new to you.

Cygwin installation is worth spending some time on — and, if you’ll be setting up more than one system, experimenting with one of the systems first to be sure you’ve got it right.

Start the installation by downloading a Windows program named setup.exe (to avoid confusion later, you might rename it cygwin_setup.exe) and running it. The main interface is a huge list of the available packages and the corresponding version numbers. The list is a bit intimidating, but it’s worth looking through every category for tools you might want; a lot of common Linux packages have been ported to Cygwin, and there might be some that are new to you.

Selecting a package generally selects any other packages required. For instance, choosing an X Window System utility also selects the X server. Be aware of this as you go through other categories, though, and notice that packages you’ve never seen have been selected for you. (A consistency check before installation should catch missing dependencies, but it’s harder to fix them at that late stage.)

Conveniently, your setup choices are saved, so you can re-run setup.exe later to add, delete, or update packages.

The next step in installation is to choose where the Cygwin files will be saved on your Windows disks. Cygwin builds a Linux-like filesystem tree and “hides” the Windows location from you, instead using the Linux-like pathnames.

For instance, from a bash shell you can type cd /usr/man/man1 and the shell will report that you’re in the /usr/man/man1 directory. You’re actually at a Windows location like C:\cygwin\usr\man\man1; the root of this Linux-like filesystem can be chosen when you install.

By default, the package files for installation are also saved to your hard disk. You might want to save these somewhere other than the default and separate from the actual installed programs. These package files are only needed when you update your installation by re-running setup.exe.

You can also choose whether Cygwin should use Windows-type or Linux-type pathnames and text file end-of-line characters. Your columnist’s favorite is to use forward slashes (/) and to create text files that end with Linux-like newline characters. (Windows-type files can still be created and handled by Linux tools; you can use sed (1), tr (1) and friends to remove or add the extra carriage return characters in files that will be handled by Windows-based tools.)

Once you’ve made your installation choices, setup.exe makes a network connection to one of the Cygwin servers (which you can choose) and downloads all of the package files. Next, the installer installs all of the packages and configures your Linux filesystem. This can take some time and hundreds of Megabytes of disk space. It’s a good idea to watch the installation process and keep a mental note of what’s happening; if the installation aborts for some reason, it can be helpful to have an idea of what’s taken place so far.

Though the entire Cygwin package must be installed as an Administrator (the privileged user), you can use Cygwin from Limited user accounts. Each account has its own setup and configuration files. Some of the Windows system variables, like HOME, will be propagated into your Cygwin setup. If the local usernames are the same as the ones on your Linux systems, access over a network (with ssh, for instance) is simpler. Though you can change the settings after installation, it’s better to have the Windows side configured the way you’d like it first. (This is part of why it helps to get familiar with a single installation before you install Cygwin on other machines.)

When the installation finishes, you’ll have (at least) a Cygwin “C” icon on your Windows Start Menu and/or desktop. When you click on the icon, you’ll get a standard Windows terminal window with (by default) a bash shell running inside. As soon as you get the familiar $ prompt, you’re ready to play!

Navigating the Filesystem

From within a Cygwin utility, the Cygwin-generated filesystem“ looks like Linux.” For instance, typing cd from a shell prompt takes you to the ~ directory. Typing pwd there tells you that you’re in, say, /home/jpeek. But, from a Windows perspective, your current directory is actually something like C:\cygwin\home\jpeek (or wherever you assigned it to be at installation time).

Windows drives will also be available through /cygdrive. For instance, you can get to the root of the Windows G: drive with the command cd /cygdrive/g. Listing One shows typical output from df.

Listing One: Output of df running within Cygwin
jpeek@xp /g/jpeek/linuxmag/0612_cygwin
Filesystem       1K-blocks      Used Available Use% Mounted on
C:\cygwin\bin     35840980  22569316  13271664  63% /usr/bin
C:\cygwin\lib     35840980  22569316  13271664  63% /usr/lib
C:\cygwin         35840980  22569316  13271664  63% /
c:                35840980  22569316  13271664  63% /cygdrive/c
d:                 5004212   1773308   3230904  36% /cygdrive/d
e:               204067136 163636968  40430168  81% /cygdrive/e
f:                 7349696   1614400   5735296  22% /cygdrive/f
g:                32692424  11300096  21392328  35% /cygdrive/g

Typing /cygdrive over and over can be a pain. One time-saver is to set the shell’s CDPATH variable from within your .bash_profile, for instance. Then you can cd to a different drive by typing the drive letter, as shown in Listing Two. (An empty element in the bash CDPATH means“ the current directory”. So, starting the CDPATH with a colon tells bash to try subdirectories of the current directory before it tries subdirectories of /cygdrive. This is normally what you want.)

Listing Two: Using the bash CDPATH variable to handle /cygdrive

$ pwd
/home/jpeek
$ grep CDPATH .bash_profile
export CDPATH=:/cygdrive
$ echo $CDPATH
:/cygdrive
$ cd d
/cygdrive/d
$ pwd
/cygdrive/d

An even handier technique for avoiding /cygdrive is making symbolic links in the root directory of the Cygwin filesystem. A symlink at /d can point to /cygdrive/d, for instance. Making a whole series of these, from /a through /z, gives quick access to whatever drives are currently mounted. This setup has an advantage over CDPATH, too: it works from applications other than the shell.

Cygwin Symbolic Links and Windows Shortcuts

Symbolic links that you make with ln -s are implemented as Windows shortcut (.lnk) files. And a shortcut you make from within Windows proper appears as a symlink in Cygwin, too.

For instance, Listing Three shows a file named photo.jpg that links to panel.jpg. Running ls -l shows the result from within Cygwin.

Listing Three: Making and listing a symbolic link

$ ln -s photo.jpg panel.jpg
$ ls -l p[ah]*
lrwxrwxrwx 1 jpeek None 9 Nov 16 15:25 panel.jpg -> photo.jpg
-rw-r-r- 1 jpeek None 123531 Nov 13 13:29 photo.jpg

Figure One shows that same symlink from within Windows Explorer, as a shortcut file and two thumbnails.

Figure One: A Cygwin-created shortcut viewed from Windows Explorer

The actual Windows filename of the shortcut is panel.jpg.lnk. You can see that by running the Windows DIR command from a CMD prompt, as in Listing Four.

Listing Four: A Cygwin symbolic link viewed with Windows DIR

D:\tmp\tmp>dir
Volume in drive D is 5GB temp files
Volume Serial Number is 70AB-72A1

Directory of D:\tmp\tmp
.
11/16/2006 03:25 PM 309 panel.jpg.lnk
11/13/2006 01:29 PM 123,531 photo.jpg

You might even consider sharing parts of your home directories or shell-script directories over a network or between filesystems on a dual-boot machine, between Linux and Windows. As an example, you could make symbolic links from Linux to the Windows filesystem where your Cygwin home directory is stored.

Again, though, the different pathname styles in the Windows and Cygwin sides can bite you; it might be best not to make symlinks or shortcuts that “cross sides.” A symlink with a target in another directory can cause grief. Test carefully and think about what might happen if a user opens or modifies one of these files without realizing that it’s cross-system.

Executables

You can run the same Cygwin executables from Windows as from your Cygwin shell window. For instance, you can run a zsh script from the Windows “Run” dialog box by giving the pathname and arguments, as Figure Two shows. You can create a Windows shortcut on the desktop or Start Menu that runs Cygwin utilities. It can help to add C:/cygwin/bin and C:/cygwin/usr/bin to your Windows PATH environment variable. That lets you run a Cygwin utility from a standard Windows CMD prompt.

Be aware that the pathname conventions can be different in the Windows and Cygwin sides. For example, in Figure Two used a Windows-type path to the executable but a Linux-type pathname to the shell script file: C:\cygwin\bin\bash /d/tmp/zsh-script. (That’s because zsh operates in the Cygwin filesystem space, and zsh opens the script file after it starts.) You may need to translate backslashes to slashes and vice versa. All of this takes some tweaking and getting used to, but it’s worth the effort.

Figure Two: A Cygwin command line in the Windows Run dialog

Configuring Cygwin Shell Windows

Interactive Cygwin shells run in standard Windows text windows (the same kind that CMD uses). You might want to do some configuration to the Windows shortcut that starts a Cygwin shell. For instance, set a Windows key combination like Ctrl-Alt-C to open a Cygwin shell window without using your mouse.

Figure Three shows the Properties dialog that can be opened by clicking on the Cygwin icon at the top-left corner of the terminal window. If you copy and paste from Linux terminal windows, setting the Quick Edit mode will save you some pain. Quick Edit mode lets you select text with the mouse and copy it to the Windows clipboard by pressing the Enter key, and you can paste text from the clipboard by right-clicking.

Figure Three: Cygwin shell window configuration

To Be Continued...

We’ll continue this topic next time with a look at the shells and utilities.

Jerry Peek is a freelance writer and instructor who has used Unix and Linux for more than 25 years. He's happy to hear from readers; see https://www.jpeek.com/contact.html.

[Read previous article] [Read next article]
[Read Jerry’s other Linux Magazine articles]