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
class="story_link">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
"i">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
"i">/cygdrive/c or just /c). Files
can be grep ped and paged with
"i">less. Filesystem searches with
"i">find do so much more than Windows’s
"i">Search tool. Shell setup files like
"i">.bashrc can be copied (or used via a network!) basically
as-is.
Let’s dig in to this rich environment!
Installing Cygwin Correctly
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. ygwin 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 / 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 (
"c">/) 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,
"i">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
"i">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
"i">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
"i">/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.
jpeek@xp /g/jpeek/linuxmag/0612_cygwin
$ df
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
"i">CDPATH variable from within your
"i">.bash_profile, for instance. Then you can
"i">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
"c">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.)
$ 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
"i">/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 (
"i">.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
"i">panel.jpg. Running ls –l
shows the result from within Cygwin.
$ 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.
The actual Windows filename of the shortcut is
"i">panel.jpg.lnk. You can see that by running the Windows
DIR command from a
"i">CMD prompt, as in Listing
Four.
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
"i">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">C:/cygwin/usr/bin to your Windows
"c">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">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.
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.
To Be Continued…
We’ll continue this topic next time with a look at the
shells and utilities.
No comments yet.