Unix Intro
Unix Intro
Scott D. Anderson
Wellesley College
Scott.Anderson@acm.org
c Fall 2002
1
4 Shell Commands % ls
HelloWorld.class public_html
Back in the olden days, when you logged into a computer, HelloWorld.java read-only
there were no windows, and you typed commands to a % pwd
“command line” and results were typed back to you (or put /students/wwellesl
into files). You still get that when you log in before you % cd public_html
start X Windows and when you login remotely using Tel- % pwd
net. Unix still bears that legacy, and the place where you /students/wwellesl/public_html
type commands is called “the shell.” (It’s called the shell in % ls
contrast to “the kernel,” which is the core of the operating % mkdir newdir
system; the shell is a command-line interface to the kernel.) % ls
Nowadays, each shell runs in a window and you can have as newdir
many shell windows as you like. % cd newdir
The shell prompts you for input and responds to your % pwd
commands. You can customize your prompt. Here at /students/wwellesl/public_html/newdir
Wellesley, we’ve defined it to be your username and host % cd ..
(machine name) and the name of the directory you are in. % rmdir newdir
If I’m logged into Teddy and I’m in my public_html % cd
directory, my prompt would look like: % pwd
/students/wwellesl
[anderson@teddy public_html] % ls -1a
.
In this document, however, I will pretend that the prompt ..
is a percent sign. .bash_profile
Here are a few of the shell commands that people use ev- .bashrc
ery day: .emacs
HelloWorld.class
ls This prints a listing of all the files in a directory (called HelloWorld.java
“folders” in other operating systems). By default, .kde
it lists the files in the current directory, but with a .login
command-line argument, it lists the files in the direc- .profile
tories named on the command line.. public_html
read-only
pwd Prints the complete name of the current working di- % cp .profile dot-profile
rectory; in other words, what the current directory is. % ls
dot-profile public_html
mkdir This creates a new directory, contained in the current
HelloWorld.class read-only
one. That is, the new directory is a sub-directory.
HelloWorld.java
cd This changes your current directory. In other words, it % mv dot-profile renamed-file
moves you from one place to another, like changing % ls
your location. HelloWorld.class read-only
HelloWorld.java renamed-file
cp This copies a file from one place to another. public_html
% rm renamed-file
mv This moves a file from one place to another (or one
name to another). You can rename a file by using mv. The first ls shows that we have two things in our cur-
rent directory. The pwd shows us that the current directory
rm This removes (deletes) a file. Warning: you can’t get it is /students/wwellesl. It happens that public_
back again! html is a directory within our current directory, so we can
cd into it. The pwd shows that the cd command worked.
rmdir This removes (deletes) a directory, but only if it’s The second ls shows that the private directory is empty.
empty. We make a sub-directory and use ls to check that it exists.
We can cd to it, as the pwd confirms. The Unix file struc-
Now, let’s see these in action. See if you understand what ture is a tree, just like all computers, with the directories in
is happening at each step here. Afterward, I’ll go over them a path separated by slashes (Windows uses backslashes and
and interpret. Here’s a session by Wendy Wellesley: MacOS uses colons).
2
The cd.. changes to the directory above the current one. “control” key while typing “x” then “w”). This is what other
(The “..” is a special name for the parent directory.) This programs call “Save As.” To just save a file you’re working
brings us back to our public_html first directory. The on, without specifying a name, type “C-x C-s.” You can also
lsnewdir shows that ls can be followed by a directory access these commands via the “Files” menu.
name, to list the contents of that other directory. Since I highly recommend running the Emacs tutorial. To start
newdir is empty (we just created it, after all), we can use the tutorial, type “C-h t,” which means to hold down the
rmdir to remove it. control key while typing “h,” then release the control key
The bare cd command changes to the original directory, and type a “t.”
also called the “home” directory. We confirm this with pwd. Note: a single Emacs can edit any number of files, so un-
We then do an ls-al and discover that there are additional, less you have a good reason to, it’s not necessary to start
invisible files in our home directory. The -a option to ls up multiple Emacs applications, and it will just slow down
means to show all files. Usually, ls hides any files whose your machine unnecessarily. Instead, switch among differ-
name begins with a dot. The -1 option means to give a ent Emacs buffers.
listing in one column, which I did just for convenience in
this document; in real life, -1 is rarely used. However, -l
(the letter “l,” for a “long” listing) is often used and gives a 6 Compiling C Programs
lot more information about each file.
We can use cp to copy one of these files, use mv to re- There are two ways to compile a C program. There’s the
name (or move) the copy to a new name, and finally use rm old-fashioned way, and the spiffy new way (actually, both
to delete the copy. are older than you are). I’ll explain both. For concreteness,
let’s assume you’re editing a file called foo.c.
The old-fashioned way is to switch to the shell window,
5 Creating/Editing Files make sure you’re in the directory that contains foo.c (use
cd if you’re not, and ls to check), and run the C compiler,
Having seen how to rename and remove files, how do we which is called cc. Either of the following will do it:
create them in the first place? This is done with an “editor,”
% cc foo.c -o foo
which is any program that allows you to change the contents
% make foo
of a file. One of the two standard editors in Unix is Emacs.
(The other is vi.) I’ve written a separate document intro- The first command compiles foo.c and puts the binary (ex-
ducing Emacs, so I will describe it even more briefly here. ecutable) file in foo. Note that foo is not the same thing
There are quite a few ways to start Emacs, depending on as foo.c! One is your source code and the other is your
what effects you want. I’ll just tell you two. executable code. If you make a mistake and accidentally do:
If you’re logged in remotely via ssh, and you’re on a to figure out how to run cc in order to make foo, which is
Unix machine running X Windows, you can start up a what you wanted. Highly recommended.
new window running Emacs by typing its name as a The new-fangled way to compile a C program is to ask
command, followed by an ampersand. Emacs to run it for you. Type the following in Emacs: “M-
x compile RET.” If you’ve run the tutorial, you know that
% emacs & “M-x” means to hold down the “meta” key (usually “alt”)
and type an “x,” then to type “compile” in the mini-buffer,
3
7 Running Your Programs can do anything to the disks that you can do with your own
files and directories, using the same commands.
Once you’ve compiled your program and you have a binary When you’re done, you can unmount the disks and pop
file called foo, you can run it by switching to your shell them out:
window and typing:
% umount /mnt/zip
% ./foo % umount /mnt/floppy