UNIX Information

UNIX Information

 


What the heck is this?

What is UNIX?

UNIX is an operating system (like Windows or Mac OS X, which is actually based on UNIX).  It was written in the early 1970s.
 
In the mid-1980s, Richard Stallman and the Free Software Foundation tried to write a free (as in "free speech," not "free beer") version of UNIX that anyone would be able to use, read, modify, etc.  But they were having a hard time developing the kernel (the central part of an operating system -- the part of the OS that coordinates between software and hardware).  
 
Then, in the early 1990s, Linus Torvalds wrote an open OS kernel that he named "Linux."  It behaved just like UNIX and worked with what Stallman/FSF had produced.  It wasn't UNIX, exactly -- but it behaved just like UNIX and worked.  It was a great boon to the open-source software community.

How can I learn the shell?

The shell is one way to control what an operating system does.  It's an alternative to point-and-click graphical interfaces, like you probably use most of the time on your computer.  Rather than moving a mouse around, you type commands.  The shell looks like this:
 
 
Many people (especially programmers) like to work in the shell because it's more precise and, once you learn how to work it, faster.  (Windows equivalent: cmd.  Mac equivalent: Terminal)
 
Also, if you become a real command-line hero, strange things will start to happen a la The Matrix.
 
If you want to learn the shell, refer to this and read through it.  It's worth your time.  We'll use our own Linux box -- binx -- for problem set 00.  So you should continue reading this document.
 
Also, you should DEFINITELY be a sysop.  They are the best.  
 
 

Accessing the UNIX-like command line from Mac and Windows

You can control your own physical computer -- that is, your Windows or Mac machine -- using a shell.  On Mac, you'll open up Terminal.  On Windows, Start > cmd (stands for "command line").  But we'll actually use binx to work with the shell.

What is  binx?

We've got a server (a really heavy-duty, reliable computer connected to the internet) called binx set up on Blair's campus.  That computer has a lot of stuff on it, including our websiteBILL, and a Java compiler.  

One thing we'll want to be able to do is use that computer, and we can do that remotely from another computer.  That way we can do things like write/compile/run Java programs, check our @mbhs.edu email addresses, etc.  

From a Mac machine

Open up Terminal.  Then:
 
SSH = "secure shell."  Google if you care.
Plenty of info here on how to use Terminal like a champion.

From a Windows machine

You'll need a program like PuTTy, which will let you log in (over the Internet) to binx.  See the demo before for instructions on how to use PuTTY for our purposes.

 

Basic UNIX commands

List

Courtesy of Mr. Fowler

Remember that Unix is case sensitive

pwd : print the working (current) directory - tells you where you are.

cd child  : change directory, where 'child' is a directory below the current one. Directories are separated by "/". "cd  .." takes you to the parent directory.  "cd" (no arguments) takes you to the home directory for your account.

mkdir child : creates a directory below the current one.

chmod permission-codes filename : changes the permissions for a file or dir. See full list of permission codes

e.g. chmod 700 myfile.txt : set 'myfile.txt' to be readable/writable only by me. 

chmod 777 stuff/ -R : set all files in 'stuff' directory to be readable/writable by everyone.

ls : lists files and directories in the current directory.

ls -l :  lists files with details of size, date, etc.

rm filename : removes a file permanently.

mv filename newname : renames a file

mv filename newlocation : moves a file to a different directory

cp filename destination: copies a file to the destination

chmod options filename : changes the permissions on a file

man topic : read the help pages on a command

pico filename : starts the text editor and opens the specified file.

cat filename : displays the text in a file

pine : starts the email program

 

Demo -- Hello World on binx -- starts with logging in

Your binx account

Your username is the first 2 letters of your first name followed by 6 letters of your last name.

George Washington would be gewashin

Joe Lee would be jolee

Your password is -- at the start -- the same as your school computer password.  You should feel free to change it.

Thanks, MDS!

ssh'ing into binx

Windows using PuTTy

  1. Run PuTTy

  2. Type binx.mbhs.edu in Host Name and click Open

  3. Type your username and hit Enter.  Then type your password.  Nothing will show up when you type it in.  Hit enter.

  4. Now you've logged in!  You're on binx!  You'll see your cursor blinking in front of The Prompt -- yourUsername@binx:~$.  That's where you'll type your commands to the computer.

Mac using Terminal

Open up Terminal and type

ssh username@binx.mbhs.edu
Then follow the same steps as for the PuTTy screenshots above.

Listing files

You'll do this often to get a sense of what's in your current directory.

ls

tells us what files are present.  In other words, it lists everything in the current directory.  At first, when you do this, you won't see any files, because nothing currently exists in your home directory.  We'll change that shortly.

Creating a directory

Type mkdir folderName to make a new folder.  Of course, that makes a folder named "folderName."  So replace "foldername" with whatever you want.  For us, let's call the folder adsa.
 

Then you can ls again to confirm that the new folder appeared.

Make the directory private only to you

Right now, anyone can see your folder and what's inside.  We want to make it so only you can see that information.  To modify the folder's permissions, use:
 
    chmod 700 <directory name>
 
or in this case...
 
    chmod 700 adsa
 
Afterward, you'll go right back to the command prompt.  If that happens, it worked.

Changing directory

cd stands for "change directory."  It's the equivalent of double-clicking a folder in Windows Expolorer to go into that folder.

cd adsa
(you could type cd a and then hit "tab" to autocomplete.  Hit tab again if you want the next option.  etc.)

ls again to see what's there.  There's nothing yet.  See how it shows your location next to your username?  The prompt now reads:username@binx:~/adsa

 

If you wanted to change back up to the parent directory, type:
 
cd ..

Just make sure to change back to /adsa before the next step!

Creating a file

We'll use Pico, a common command-line text editor.  Here are some common commands.  
 
Type pico and hit enter to launch the editor:
 
 

Type your code here.  You'll only be able to navigate with the keyboard.
 

/*
 * My first Java program -- written in Pico!
*Hello, World -- Displays "Hello, World!" to the terminal
*/
public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello, World!");
    }
}

 

(Careful -- there's an error in this code that you'll have to fix!)

 

Saving the file

ctrl+O (written ^O in lots of documentation) saves.  
When you're prompted for a File name, type HelloWorld.java.  (no period at the end)  The name has to be the same as the class name from inside the file.  

You could add a filepath to the name of the file if you wanted to save it somewhere else -- /home/chen/javastuff/HelloWorld.java
 
We won't, though.  Just save it as HelloWorld.java.
 

Now Pico knows that this is a Java file, so it's colored our code nicely!  Thanks, Pico!

Type ^X (ctrl+X) to quit Pico.  
 

Viewing a file

Let's quickly see what's inside the file we just wrote:
 
cat HelloWorld.java
 

Compiling the Java code you wrote

Remember, before we can run a Java program, we have to translate it into a language that the Java Virtual Machine can understand -- that's Java bytecode.  
 
The Java compiler is called javac, so that's the command we use to compile our code.
 
If you ls again, you should see HelloWorld.java.  Now compile the program you just wrote:
 
javac HelloWorld.java

(remember, you could type javac H and then tab-complete it, just like before!  mmmm feel that SPEED)

Now, ls again.  You'll see that HelloWorld.class has appeared.  That's the file that the Java Virtual Machine will use.

Running Java code

We get to the Java Virtual Machine via a Java interpreter called java.

To run your program, type  

java HelloWorld

(remember: java H TAB YEAH SON I AM LIKE A CHEETAH)

 

Hooray, "Hello, World!" prints!

Editing a file you already made

Suppose you need to go back into HelloWorld.java and make some edits.  Just type

    pico <filename>
 

or, for us...

    pico HelloWorld.java

That will take open up HelloWorld.java in pico.  If you just type pico, it'll give you a blank document again.

Note: Make sure you remember to re-compile your code (javac) before you run it again (java) if you make any changes.

drawn from http://linux.about.com/cs/unix101/a/javaonunix.htm

Renaming a file

Let's rename a file.  I used pico to create and save a text file named TestFile.txt 

(You can do this too, in order to practice.)
 

The mv command both moves and renames a file.  For renaming, it's in the form mv <oldname> <newname>.  Let's first use it to rename:
 
mv TestFile.txt RenamedTestFile.txt

 

Moving a file

We use mv to move a file, too.  It will be in the format
 
mv <filename> <newlocation>
 
First, let's make a place for it to go.  Remember that ".." refers to the "parent directory."  So let's make a new directory:
 
mkdir ../testfolder

 

 

Normally, we'd trust that the folder was created in the right place (i.e. inside the current location's parent directory).  But this is our first time, so let's confirm.
 

Type cd to go back to your home directory.  In this case, we also could have typed cd .. to back to the immediate parent directory, but they happen to be the same place in this situation.  Then ls again:

Look!  testfolder exists now!  Awesome.  Now let's move that file RenamedTestFile.txt from /adsa into /testfolder.

Two ways you could do this.  (Remember!  Use tab complete!)

  1. TWO STEPS
    Move back into adsa and do the move from there
    cd adsa
    mv RenamedTestFile.txt
     ../testfolder

    OR

  2. ONE STEP
    Stay in the home directory and do the move from here
    mv adsa/RenamedTestFile.txt testfolder

    Remember: Use tab complete!  type mv a, then hit tab and you'll see mv adsa/
    Then type R and hit tab again, and you'll see mv adsa/RenamedTestFile.txt
    Then type t and hit tab, and you'll see mv adsa/RenamedTestFile.txt testfolder/

cd into testfolder to make sure it worked!
 

If you wanted to move the file from the testfolder to the parent directory, you could type:

mv testfolder/RenamedTestFile.txt ..

Deleting a file

Let's now delete that useless test file.  

BE CAREFUL WHEN DELETING FILES.

You could do this from the home directory by using the filepaths, but let's just navigate into testfolder.

rm RenamedTestFile.txt

ls again and you'll see the file is no longer there.

Huzzah!

Deleting a folder

Hey, that folder testfolder is pretty useless.  Let's get rid of it.  

cd back to the home directory.  

rmdir testfolder

This will delete a directory...but only if there's nothing in it.

If you want to delete a directory that has stuff in it, you'll type

rmdir -rf directoryname

BUT BE CAREFUL.  It's unforgiving.  Don't make a mistake.

 

Clear the screen

If you want to clear the screen, just type clear

Email in the command line

Type pine.   Hit Enter.

 
Scroll around.  Commands are at the bottom.  Send some emails.  You'll use pine to submit ps00; you can find instructions on how to do it on the ps00 page.  
 

Quit pine when you're done.

Changing your binx password

Type passwd and follow the steps.

Exiting the shell

Just type exit.