Introduction to UNIX-like systems


Table of Contents

  1. Overview
  2. UNIX background
  3. Understanding directories
  4. Simple directory navigation
  5. Copying, deleting, moving and renaming files
  6. Getting help in UNIX
  7. Exiting the terminal
  8. Conclusion
  9. References

Overview

In the first lab of the semester, we looked at how to use the program PuTTY to access a UNIX system called data.cs.purdue.edu. For a lot of students in this course, this was their first time ever using a UNIX-type system. This tutorial is meant to act as a brief introduction to working with a system like this - by the end of this tutorial, you should be comfortable performing basic tasks such as the following:

We'll begin by discussing some background about UNIX systems.

UNIX background

The UNIX operating system was developed in 1969 by a group of programmers working for Bell Labs (a famous computer science research branch of AT&T) [1]. Many of the concepts we use in operating systems like Mac OS X and Windows 7 have their foundations in UNIX.

Originally, these systems were very big (they would take up large rooms), and they were accessed using a terminal - a device used to enter and retrieve data from the computer. These terminals did not have features like windows that we associate with computers today, but instead used only text. Although computers became more powerful and allowed the window-based systems used today, the terminal-related features were never removed, because they make it easy to accomplish certain things like working on a computer remotely.

Learning how to use a UNIX terminal is a very important skill, because very many computing platforms use UNIX-like systems. Any time you are working on a computer running Solaris, BSD, Linux or even Mac OS X, the underlying system is based off of UNIX, so you can use a terminal to work on the system in the same way you will learn in this tutorial.

In UNIX, every object you deal with is either called a process or a file.[2]

Understanding directories

The idea of a directory in UNIX is very similar to the idea of a folder in Windows or Mac OS X. In short, a directory is a file which contains other files and information about them. In this way, a directory can be used to organize data like a folder can. You can put directories within directories (the inner directories are then called subdirectories) to further organize your data. For the purposes of this class, the notions of a folder and a directory are interchangeable, as we will now see.

In the first lab, you did the following:

If you mapped the network drive to the drive letter Z, then your lab1 folder would be in the following path:

Z:\CS177\lab1

On the UNIX system, your directories are likewise organized in a path, but it looks slightly different:

/homes/your_purdue_login_here/CS177/lab1

As you can see, the folders you made earlier are treated appropriately as directories in the UNIX system. In the next part of this tutorial, we will see how to determine your current directory and change between directories.

Simple directory navigation

Open up PuTTY and connect to data.cs.purdue.edu like we did in the first lab. As soon as you access the UNIX system, you are given a prompt: the terminal is waiting for you to type in some sort of command. Typically, the prompt looks something like the following:

bash 1 $ _

You should see bash, some sort of number, a dollar sign, and then a blinking cursor. In the examples in this tutorial, we will precede the commands that you need to type with a dollar sign. If the system responds in some way after the command, we will put it on the next line:

$ example_command
Example system response

Once you have accessed the UNIX system, you are immediately taken to your home directory - basically, there is a very big system with a large number of users on it, and each user gets their own home directory where they are free to put their files safely (nobody else will be able to delete or copy their files). Your home directory on data.cs.purdue.edu is:

/homes/your_purdue_login_here

Showing your current directory (pwd)

Many of the commands you use in UNIX pertain to the current directory that you are working in. To check your current directory, type the following command (don't type the dollar sign, just the command):

$ pwd

pwd stands for "Present Working Directory". If you have just accessed the UNIX system, you should be in your home directory. If you now type in the pwd command and your username is suversky, you should see something like this:

$ pwd
/homes/suversky/

Again, we typed pwd after the prompt that was given to us (the dollar sign), and the UNIX system returned our current directory as output on the next line.

Changing directories (cd)

This shows you that you are currently in your home directory. If you have a subdirectory called CS177 in your home directory, you can navigate to it using the cd command, as follows (again, don't type the dollar sign):

$ cd CS177

cd stands for "Change Directory". Notice that you needed to put a space then type the name of the subdirectory you wanted to change to after typing cd - this is called an argument, and it's basically an additional instruction so that the command knows exactly what to do. Also, notice that the system didn't tell you anything after you changed directories. However, you can use the pwd command you just learned to make sure you are in the CS177 directory. Continuing with our example:

$ pwd
/homes/suversky/CS177/

You could keep doing this if you wanted to go into the lab1 subdirectory (note that the argument you put in is case-sensitive, so make sure you type it carefully):

$ cd lab1
$ pwd
/homes/suversky/CS177/lab1/

Again, note that we can use pwd to figure out where we are if we are ever unsure about our current location. With more practice, you will start to build a mental map of where you are in the directory structure, and you will not need to type pwd as often.

Parent directories

Now that you are in here, how do you get back to the directory containing lab1 (the parent directory of lab1)? There is a special directory called .. (that's two dots), which always refers to the parent directory of the current directory. We can use cd with the argument .. to take us up a level back to the parent directory. Continuing with our example:

$ pwd
/homes/suversky/CS177/lab1
$ cd ..
$ pwd
/homes/suversky/CS177/

(Likewise, . (one dot) always refers to the present directory, but this isn't as important now.)

Returning to your home directory

What if you want to get all the way back to your home directory? This is even simpler - simply type cd without any arguments at all, and by default it will take you back to your home directory. Continuing with our example:

$ cd
$ pwd
/homes/suversky/

Listing files in the current directory (ls)

Naturally, once you have a large number of files in your home directory, it's not quite as easy to navigate up and down directories only by memory. Instead, you can list all of the files in the current directory by using the ls command (that's LS, but lowercase) as follows:

$ pwd
/homes/suversky/
$ ls
CS177

ls stands for "List". Notice that after entering the ls command, we got a list of everything inside our home directory (which in this case is just the subdirectory CS177). You can combine the ls command with the name of a directory as an argument to list all of the files inside the subdirectory, like so:

$ pwd
/homes/suversky/
$ ls
CS177
$ ls CS177
lab1

Of course, you could also navigate to the CS177 directory using cd, and then type ls in there, but this would change your present directory.

Notice how there is no distinction between a directory and a file when using ls. Like we said before, a directory is a file, just one that can be treated like a folder.

Hopefully the link to the folder system used in Windows and Mac OS X is clear:

Navigation is not very useful without being able to manipulate the files in some way, so we will discuss how to copy, delete, move and rename files next.

Copying, deleting, moving and renaming files

Copying files (cp)

In order to copy a file, we will use the cp command, which takes two arguments as follows:

$ cp name_of_file_to_copy name_of_new_file

cp, of course, stands for "Copy". Let's suppose we have a file lab1.py in the /homes/suversky/CS177/lab1/ directory. Let us try copying this to lab1copy.py:

$ cd
$ cd CS177/lab1
$ pwd
/homes/suversky/CS177/lab1/
$ ls
lab1.py
$ cp lab1.py lab1copy.py
$ ls
lab1.py lab1copy.py

You can see that the file has now been copied to lab1copy.py. However, what if you want to get rid of this copy?

Deleting files (rm)

We can use the rm command to delete a file. The command works as follows:

$ rm file_to_be_removed

rm stands for "Remove". Continuing on from the last example:

$ rm lab1copy.py $ ls lab1.py

Please be very careful when using the rm command. If you accidentally delete the wrong file, you may lose some or all of your work.

Moving and renaming files (mv)

Now let's suppose you have a file inside of one directory you want to move to another directory. You can accomplish this using the mv command. The command looks like this:

$ mv file_to_be_moved directory_to_move_file_to

''mv'' stands for "Move". For example, let's say we accidentally put lab1.py in the CS177 directory, and want to move it to the lab1 subdirectory.

$ cd
$ cd CS177
$ pwd
/homes/suversky/CS177/
$ ls
lab1 lab1.py
$ mv lab1.py lab1
$ ls
lab1
$ cd lab1
$ pwd
/homes/suversky/CS177/lab1/
$ ls
lab1.py

Likewise, you can move a file to its parent directory using ..:

$ pwd
/homes/suversky/CS177/lab1/
$ ls
lab1.py
$ mv lab1.py ..
$ cd ..
$ pwd
/homes/suversky/CS177/
$ ls
lab1 lab1.py

However, let's put the file back where it belongs:

$ mv lab1.py lab1

Renaming a file is equivalent to moving it within the same directory but giving it a new name. Let's rename lab1.py to testrename.py and back.

$ ls
lab1.py
$ mv lab1.py testrename.py
$ ls
testrename.py
$ mv testrename.py lab1.py
$ ls
lab1.py

Copying and removing directories (the -r flag)

You may want to copy a directory (maybe to backup the files that are in it, in case you accidentally rm them!) This is also done using the cp command, but you must add in a -r argument like so:

$ cp -r directory_to_be_copied new_directory_name

The -r tells the cp command that you want to copy everything that's inside of the directory, as well as the directory itself. Let's try it out by backing up our lab1 directory.

$ cd
$ cd CS177
$ ls
lab1
$ cp -r lab1 lab1backup
$ ls
lab1 lab1backup

Likewise, you can remove an entire directory using rm with the -r argument.

$ ls
lab1 lab1backup
$ rm -r lab1backup
$ ls
lab1

Again, BE VERY CAREFUL! If you delete the directory containing your labs, or your entire CS177 directory, you could potentially lose an enormous amount of work!

Getting help in UNIX

For just about any command that exists in UNIX, there is a corresponding man page - a manual page which describes, in detail, how to use the command. These man pages are often very dry and technical, but they follow a very specific format and teach you exactly what each command is capable of. Taking the time to read a man page very slowly and carefully when you are confused about how a command works can be very informative (if a bit tedious!)

To access the man page for a command, type:

$ man name_of_command

So, for example, an in-depth manual for the ''cd'' command can be found at:

$ man cd

Exiting the terminal

To exit the terminal, simply type:

$ exit

Conclusion

This tutorial may make things seem a bit overwhelming, but remember that even if you are working on a system like Windows or Mac OS X that you are very comfortable with, this comfort did not come overnight - in order to learn how to use the system efficiently, you had to use it repeatedly. In the same way, working in a UNIX system gets easier the more you do it. If you keep practising navigating directories and using file commands in UNIX, it will soon be second nature to you.

For further instructions, please consult the second reference (UNIX Introduction) below; if you are still having trouble, feel free to contact the TAs via Piazza.

For direct feedback on this tutorial - if anything written is unclear, if additional examples are requested, or if you simply liked it and want to tell me (and make my day!), feel free to send me an email at suversky at purdue dot edu.

Good luck and happy coding from your friendly neighborhood TA!

-- Sergei Uversky

References

  1. "UNIX" on Wikipedia
  2. UNIX Introduction