Intro to Scientific Computing
PHYS 27/193
Physics Department
University of the Pacific

Moving Around in the Unix World

Now that you have your Cygwin system running, let's learn to move around a bit. We've go to get used to doing things through a text based shell window. While that might seem extremely limiting at first, it's actually much more efficient, especially when you are connecting to another computer (like a supercomputer on the other side of the country). A shell terminal uses very little bandwidth since it only needs to transfer characters instead of pictures.

The Unix File System

You are probably used to the Point-and-Click (GUI) environment of Windows and Macs: when you want to do something, you (double) click on its icon, possibly enter some parameters (a file to edit for example), and then work within that appliction. At the end you might save your work to a file, or print it.

The other big difference is that to find your files, you use a Graphical File Browser, like the one below.

To find files and folders, you navigate your way around by clicking on icons, like this


In Unix, folders are called directories.

That's why the Unix command to move to a new directory is called cd, for Change Directory. This command moves you to another folder--which I will henceforth call by its proper name, a directory.

Using a Graphical File Browser, you usually see all the files and subfolders as little icons which often represent the type of file. You probably know how to change this behavior.

Open some Folder on your computer (My Documents, for example).
Click View at the top menu bar, and in the second group take note of what the View style you have set now (Thumbnails, Icons, Tiles, etc.). Notice the last two: List and Details.

Change your View to List.

Now change it to Details.

Notice that with Details you get size information, type, and date of last modification.

Now change the setting back to what it was before, or a new one if you like

We will do something very similar to this, listing files from the prompt in an xterm.

Working with Files and Directories in Unix

Open an xterm.

First we need some files to play with.
We'll come back to this in a minute.

Second, we need to put them somewhere.

Your Home Directory

The default directory for each user is her Home directory. This is why we set an environment variable during setup so that all programs would know where this directory is located.

Recall that your Home directory is C:\cygwin\home\YOURUSERNAME.

When you start an xterm, or if you login to a computer where you have a unix account, the terminal gives you a prompt and locates you in your Home directory. This is where your init files are, like .login and others.

In fact the way I have set up your system, the prompt tells you this.
A short hand symbol for your HOME dir is the symbol "~" (the tilda). Notice that in your prompt there is the following: [~], meaning that the prompt is located in your Home dir (I will often say "dir" for directory).

Two more shorthand notations are the dot "." and the double dot "..". These mean:


  .   The current directory
  ..  The directory above this one

For example, do this (meaning: enter the following commands at the prompt and hit Enter)

cd ..
ls (that's ell ess)

What you just did was to cd (change directories) to the directory above your Home directory. You then made a list of what files and directories were there. Notice that your prompt changes to [home], meaning that you are in the directory called home.

When you do the listing, you see your own Home direcory "YOURUSERNAME", whatever that is.
If you were logged onto a machine with many users, like the Pacific SunRay system, or if you have other users using cygwin on your PC, you would see their Home directories too. For now, you probably only see yours.

Now do

cd

Any time you do cd without specifying a directory, you are taken to your home dir. This is handy; wherever you are, a bare cd command will take you Home.

We'll come back to these things again below.
For now I just wanted you to move around the filesystem a bit.

A Home Directory Shortcut

It might make things easier for you if you have a Windows Shortcut to your Home directory. That way if you get lost, as you are learning about the Unix file system, you can find your files using the GUI Windows file browser that you are used to already.

As we learned above, your Home directory should be:
C:\cygwin\home\YOURUSERNAME

Using the Windows file browser (Start->My Computer), navigate your way to
Drive C: -> cygwin -> home

Find your Home directory there and right click on it, selecting "Create Shortcut". Once you've made a shortcut to your Home dir, you can drag the shortcut to your Desktop (and remame it to just "Home" if you like).

With this shortcut you can easily find your unix files in the graphical folder browser.

A note about "/" and "\"
Notice that above when talking about a Windows Folder the path given was
C:\cygwin\home\YOURUSERNAME,
with "\" (Backslashes).

In Unix, a directory path uses Forward Slashes:
/home/jhetrick/projects/quarks/.

Just keep it in mind when you are working in a mixed (Unix and Windows) environment such as Cygwin.


Unix commands for files and directories

ls (list)

Let's review:

When you first login, your prompt is placed in your home directory. Your home directory has the same name as your user-name, for example, jhetrick (or "~" for short; more on this below). This is where your personal files and subdirectories are. It's kind of like "My Documents" on you Windows machine.

To find out what is in your home directory, type

ls

The ls command lists the contents of your current working directory.

Note that your listing will look different than mine (shown here) since I have more files! However the general stucture will be the same.

If there were no files visible in your home directory, the prompt would simply be returned. However, we put some there by unpacking the tar command so there should be some files and directories there.

ls does not, in fact, cause all the files in your home directory to be listed, but only those ones whose name does not begin with a dot (.) Files beginning with a dot (.) are known as hidden files and usually contain important program configuration information, such as .login which I had you download. These are hidden because you should not change them unless you are fairly familiar with UNIX!!!

To list all files in your home directory including those whose names begin with a dot, type

ls -a

As you can see, ls -a lists files that are normally hidden.

ls is an example of a command which can take options: -a is an example of an option (sometimes called a switch). The options change the behaviour of the command. There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command. (More on this later)

ls -F

Another option I like to add to you ls is the -F (Format) option. This option puts "/"s at the end of each directory, which makes it easier to distinguish files from subdirectories. It also puts a "*" at the end of each file which is executable meaning that it's a program that can be run.

As you can see this option makes it easier to see which files are just files and which are subdirectories. It's particularley useful when you are working in a terminal which doesn't support color coding. Which files are directories? Which are programs?


Making Directories

mkdir (make directory)

We will often need to make a subdirectory in your current directory to hold new files, or start a new project.
To make a subdirectory called newdir in your current working directory type

mkdir newdir

To see the directory you have just created, type

ls -F

so you can see the files formated by type.

Is newdir/ there?

Changing to a different directory 

We've already covered this, but we put it here for completeness.

cd (change directory)

The command cd directory means change the current working directory to 'directory'. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

cd newdir

Type ls to see the contents (which should be empty)

In the following pages I have highlighted a number of Exercises which I would like you to carry out. Please do them! At the end of this module on Unix, I will have you make a tar file of your home directory and send it to me. That will constitute your homework for this module.

Exercise 1a:

Make another directory inside the newdir/ directory called subdir


The directories . and .. (again)

Still in the newdir directory, type

ls -a

As you can see, in the newdir directory (and in all other directories), there are two special directories called (.) and (..)

The current directory "."

In UNIX, "." means the current directory, so typing

cd .

NOTE: there is a space between cd and the dot

means stay where you are (the newdir directory).

This may not seem very useful at first, but using "." as the name of the current directory will save a lot of typing, as we shall see later.

We also need the . when trying to copy files to the current directory.

The parent directory ".."

".." means the parent of the current directory, so typing

cd ..

will take you one up on level in the directory hierarchy (back to your home directory in this case).

Try it now.

Now cd back down to the newdir directory.

You should see the name newdir in the [] part of the prompt, helping you to remember where you are.

Going Home

typing cd with no directory argument always returns you to your home directory. This is very useful if you get lost in the file system.

Try it


Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your newly made subdir directory in the newdir directory (below your home directory) navigate to subdir using the cd, then type

pwd

The full pathname will look something like this

/home/jhetrick/newdir/subdir

which means that subdir is a subdirectory of newdir which is itself a subdirectory, located in your Home directory jhetrick. Your Home directory is, along with any other user's Home directories, in the home directory, which is located below the top-level "root" directory called " / " .

Unix File structure

Notice that in the above picture, I've shown directories as rectangles with rounded edges, and I have put a "/" a the end of their names. This is common practice--to lable a directory with a trailing "/" as in subdir/. I've shown files as sharp rectangles.

Exercise 1b

Use the commands cd, ls and pwd to explore the file system.

For example you can start at the top level by doing cd /
(Remember, if you get lost, type cd by itself to return to your home-directory--in fact try to get lost and use cd to "go home"!)

More about home directories and pathnames

Understanding pathnames

First type cd to get back to your home-directory, then type

ls newdir

to list the conents of your newdir directory.

Now type

ls subdir

You will get a message like this -

subdir: No such file or directory

The reason is, subdir is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your subdir directory, you must type

ls newdir/subdir

 

~   (your home directory)

Home directories can also be referred to by the tilde ~ character. It can be used to specify paths starting at your home directory. So typing

ls ~/newdir

will list the contents of your newdir directory, no matter where you currently are in the file system.

Try this:

cd down into subdir/
Try to list the contents of newdir/ by doing ls newdir.
This fails since newdir/ is not in subdir. Now use the full pathname with the ~ shortcut:

ls ~/newdir

This time it works.


What do you think

ls ~

would list?

What do you think

ls ~/..

would list?

Summary

Command Meaning
ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory

Get a pack of files and directories

Now, let's get some files to play with.

Download the file below and save it to your Home directory

somefiles.tar

We're using a very common unix way of getting a set of files.
They are packed in a Tar File (sometimes called a tarball), which you can tell from the .tar suffix.

In an open xterm window, type

ls

You should see the somefiles.tar file in the list.

To unpack the tar file, we'll use the command tar with the -x (extract) option, along with -v (verbose) and -f (extract from a file).
Do

tar -xvf somefiles.tar

Because you used the verbose option, you should see a report of the contents as the files are extracted.
More on the tar command later.

Now you have some files to play with, let's learn more Unix command.

Next: Copy, Move, Delete, and Examine Unix Files