Introduction to UNIX ------------------------------------------------------------ Lab Computers * Before we actually get to programming in C, we will spend some time talking about the computers that you will be using. * The CSIF (AKA "The Lab") * CSIF = Computer Science Instructional Facility * web page: http://wwwcsif.cs.ucdavis.edu/ * location: basement of Kemper Hall (formerly "Engineering 2") (near South-East corner) * several rooms of computers (rooms 71, 75, 79 and others) * operating system = Red Hat Linux (on most) * window system is called "KDE" -- look and feel is quite similar to MS Windows * Quick Aside: Operating Systems * Operating System (OS): the software that runs the computer. * note: software = a set of computer programs * ex: Red Hat Linux, Microsoft Windows XP, FreeBSD, Mac OS X * note: UNIX is the name of a specific operating system created at AT &T Bell Labs. Many current OS's are derived from UNIX, so now UNIX refers to a large "family" of operating systems that share some characteristics. For example, all versions of LINUX are in the UNIX family. Mac OS X is also derived from UNIX. Thus, the phrase "UNIX computer" is not very specific: it means a computer that runs an OS which is in the UNIX family. * The Shell Program * Windows/Mac Computers: most operations are initiated by mouse clicks * Unix Computers: you can do the same thing. * But! Unix users often prefer to *type* their commands (more flexibility, more options) * Key program for this: the "shell program" * shell = a program that accepts typed commands from users * When you start up a shell window, the window is blank except for a "prompt" character or string. * The shell waits for you to type commands. * Example: $ date Sun Mar 30 21:43:32 PDT 2003 $ Explanation: * Here, $ is the shell's "prompt" character. and the user typed the "date" command. * The shell provides *many* useful commands: we'll learn a lot of them. * A basic session consists of these events: * the user logs in (enters user name and password) * the window system starts up * the user starts up a window running the shell program * the user uses the shell and possibly other programs (like editors and web browsers) for a while * the user closes all windows and then logs out We'll cover these steps in detail during the Lab Orientation sessions. For today's lecture, though, please just imagine that you have logged in, and started up a shell window. So you are ready to type shell commands. ------------------------------------------------------------ Introduction to the UNIX File System * Files are stored on a hard disk. The complete collection of files on the computer is called the file system. * Organization of the file system: * A directory is a container for files. (Same as a "folder" on Windows computers.) * A directory may also contain 1 or more sub-directories, each with their own files and sub-directories, and so forth. * The diagram below indicates how files and directories are organized on any Unix computer: / (the "root" directory) / \ / \ bin home (contains 1 subdirectory for each user) / \ / \ sanchez puketza (my "personal home" directory) / | \ / | \ News Temp ECS30 * samp.c * or1 * or2 (All of the names above represent directories, except for "samp.c", "or1", and "or2", which are files in the ECS30 directory.) Comments: * Note the "upside-down tree" structure. * The directory at the base of the tree is called the "root" directory. It is represented by a single symbol, the forward slash: / That is, the full name of the directory is just "/" ! * The "home" directory contains 1 subdirectory for each user. * Note: The CSIF has 1 huge hard disk for *all* the computers. Each computer is connected to that disk via a network. That's where your files are stored. * CWD and HD * Current Working Directory (CWD) = your current location in the file system tree. * Home Directory (HD) = your own directory (e.g., /home/puketza) * After you "log in", the shell is ready to get commands from you. At that time, the CWD = your HD. * The shell can "see" directly all the files in the CWD. So, often, if you want to do something to a file, you will first change the CWD to the directory that contains that file. * The pwd shell command * pwd = "print working directory" ( In other words: "tell me what the CWD is") * ex: Assume I have just logged in. The CWD starts at my home directory. So if I use pwd command, the shell should tell me that the CWD is my home directory! And that's exactly what happens below: $ pwd /home/puketza $ "/home/puketza" is the "full name" of my home directory. We'll come back to this idea of a "full name" soon. * The cd shell commmand * cd = "change directory" * Again, assume I have just logged in. Now I want to change to my "ECS30" directory: $ pwd /home/puketza $ cd ECS30 $ pwd /home/puketza/ECS30 $ ------------------------------------------------------------ More Shell Commands Below is an example of each command followed by a general description of the command. Note: below I'll use generic names for files and directories, such as "filename", "file1", and "dirname." $ cat filename The cat command just displays a file on the screen. It doesn't allow you to edit the file: it just shows the file, and then it is all done. $ ls The ls command lists the names of all files and sub-directories in the CWD. $ ls ECS30 Optionally, you can enter the name of a directory after typing "ls." Then, ls will list the contents of that particular directory. $ cp file1 file2 cp = "copy" The cp command above makes a copy of "file1", and names the new copy "file2". $ rm file1 The rm command deletes a file. $ mv name1 name2 The mv command renames a file, in this case from the original name of "name1" to a new name: "name2". * Quick aside: Filename rules Unix is *very* flexible about legal filenames. In particular, filenames: * can contain letters, characters, and punctuation marks, in any order * can be very very long * can contain multiple "dots": ex: myfile.version.2.revised Unlike other OS's, Unix does not examine the "filename extension" (e.g., .exe) to check the type of the file. $ mkdir ECS30 The mkdir command makes a new directory. The new directory is a sub-directory of the CWD. $ rmdir ECS30 The rmdir command deletes a directory. The directory must be empty first (i.e., the files must first be deleted). -------------------------------------------------------- Quick Review * shell = a program that accepts typed commands * CWD = current working directory * HD = home directory * some shell commands: * date * cat filename - displays a file * pwd - prints the CWD * cd directory - changes the CWD * ls - lists files/sub-dirs in the CWD * cp file1 file2 - copies a file to a new file * mv name1 name2 - renames a file * rm file - deletes a file * mkdir directoryname - creates a new directory * rmdir directoryname - deletes a directory ------------------------------------------------------------ File Names and Directory Names * In a shell command, you can refer to a particular file or directory in 3 different ways: 1 using the "simple name": the name of the file or directory (by itself) 2 using the "absolute path name (APN)": (the "full name") * Start with the / character to signify the root directory. * Write the name of each directory on the "path" from root to desired file (or directory) * Use slashes to separate names. * ex: /home/puketza/ECS30/samp.c 3 using a "Relative path name (RPN)": * An RPN is like an APN except: * start at the CWD instead of at the root * leave out the CWD itself and the first slash * ex: * Assume the CWD is currently "/home/puketza" * Then the RPN of samp.c is ECS30/samp.c * Important Rule: In shell commands, you can use the simple name if the file or directory is in the CWD. Otherwise, use the APN or the RPN. I like to think of it this way: the shell can "see directly" each file and sub-directory in the CWD. But it needs additional help, in the form of an APN or RPN, to find other files and sub-directories. * Thus, it's a little more convenient to issue commands on files that are in the CWD (less typing). However, please note: you can *always* use the APN. * Special directory name shortcuts/abbreviations * ~ = absolute pathname of your your home directory (saves a lot of typing!) for me: ~ is equivalent to /home/puketza * ~username = other person's home directory * ex: For me: * ~/ECS30/samp.c is short for: /home/puketza/ECS30/samp.c * ~sanchez/News/Temp is short for: /home/sanchez/News/Temp ------------------------------------------------------------ Quick Practice: File and Directory Names in Shell Commands / (the root directory) / \ / \ bin home / \ / \ / \ sanchez puketza * letter / | \ / | \ News Temp ECS30 * samp.c * or1 * or2 Note: there is a star in front of each file name. Other names are directory names. * Assume the CWD is /home/puketza/ECS30 $ ls samp.c or1 or2 $ cp samp.c /home/puketza/Temp Note: last command shows new way to use cp: $ cp filename directoryname This version of cp copies the file to the directory, and the copy has the same name as the original. $ ls ~/Temp samp.c $ cd (cd by itself takes you to your home directory) Let's look at the other use of "mv": to move a file. The general form is: mv filename directoryname $ mv ECS30/or1 Temp This moves samp.c from the ECS30 directory to the Temp directory. Here we used a RPN for "samp.c", and the simple name for Temp (because it is in the CWD). A little trickier: actually we can use "mv" to *both* move and rename a file, as in the following: $ mv ECS30/or1 Temp/intro We moved the "or1" file to the Temp directory, and re-named the file "intro" . $ ls ~/Temp intro samp.c $ ls /home/sanchez letter Note: Unix does have good security mechanisms. So, if you use "ls" to list another person's files (as we just did), ls will only show you the files that the user wants you to see. $ ls ~sanchez letter This command is equivalent to the previous one. ------------------------------------------------------------ The . and .. Shortcuts * 2 more useful abbreviations: . = the CWD .. = the "parent directory" of the CWD (the directory that contains the CWD as a sub-dir) Let's look at some examples. Let's start with the filesystem in this state again: / (the root directory) / \ / \ bin home / \ / \ / \ sanchez puketza * letter / | \ / | \ News Temp ECS30 * samp.c * or1 * or2 Assume the CWD is /home/puketza/ECS30 $ cp samp.c .. (copies samp.c to the /home/puketza dir.) $ cp samp.c ../Temp Here we used ".." to start the RPN of the Temp directory. The meaning of ../Temp is: * start at the CWD * go "up" to the parent directory * then go "down" to the Temp directory Thus, the above command is equivalent to this one: $ cp samp.c /home/puketza/Temp * Example: using the single dot: $ cd $ pwd /home/puketza $ cp ECS30/or1 . (copies the "or1" file to the CWD) ------------------------------------------------------------ Quick Summary * shell commands: * date * cat filename - displays a file * pwd - prints the CWD * cd directory - changes the CWD * ls directory - lists files/sub-dirs in a directory * cp file1 file2 - copies a file to a new file * cp file directory - copies a file to a directory * mv filename1 filename2 - renames a file * mv file directory - moves a file to a directory * rm file - deletes a file * mkdir dirname - creates a new directory * rmdir dirname - deletes a directory * File Names and Directory Names: * simple name - use this if the file/dir is in the CWD * APN - start at the root * RPN - start at the CWD * ~ = abrev. for your home dir. use it when typing an APN * ~user = abrev. for home dir. of some other user use it when typing an APN * . = abbrev. for the CWD * .. = abbrev. for the "parent directory" of the CWD Think: "go up one" when you see .. ------------------------------------------------------------ * * * * *