Unix Review, additional Unix commands CS101, Mock Introduction This handout/lecture reviews some basic UNIX commands that you should know how to use. A more detailed description of this and other commands is contained within the Introduction to Unix textbook by David Schwartz. In general, a Unix command has three parts as follows: commandname flags arguments Commandname is the command you wish to run. Examples you are probably already familiar with include ls (list the files in a directory) or cat (show the contents of a file). Flags allow you to specify certain options that are available for the command you have specified. For example, the l flag in addition with the ls command shows additional information about each file listed. Arguments allow you to tell Unix which items the command should operate on. For example, if you want to view the contents of the file somefile.cpp then you could use the cat command with the argument of somefile.cpp for a complete command of cat somefile.cpp Note that Unix is case-sensitive in most contexts, so be careful to use the proper upper or lowercase. File System The Unix file system is a hierarchical tree structure, just like you have probably encountered with Windows or Macintosh based computers. The root of the tree is named by the character /. An example from the directory tree on saturn.math.uaa.alaska.edu is shown below: /--------- usr/ etc/ bin/ dev/ var/ ls cp
File Types There are four types of file in the Unix file system: 1. Ordinary file. An ordinary file may contain text, a program, or other data. It can be either an ASCII file, with each of its bytes being in the numerical range 0-127 or a binary file where bytes may contain all possible values from 0-255. Example: ASCII source file named test.cpp Leftmost char is a - spooey /home/kenrick> ls -l test.cpp -rw-r--r-- 1 kenrick users 2056 Oct 3 03:03 test.cpp 2. Directory file. A directory is a file that consists of directory entries. Each file must have at least one directory entry. Example: Directory for HTML documents. Leftmost char is a d. drwxr-xr-x 12 kenrick users 1024 Jan 27 2000 public_html 3. Device file. A device file is an interface to a physical device such as a printer or disk drive. These devices are represented as files. For example, to read from a scanner you could read from it like it was a file, and to print to a printer you output like you are generating a file. One of the designs of Unix is such that almost everything can be treated like a file. Example: Hard drive 0. Leftmost char is a b (may be others) spooey /home/kenrick> ls -l /dev/fd0 brw-rw-rw- 1 root disk 2, 0 Sep 6 1998 /dev/fd0 4. Link file. Unix allows pointers to other files or directories. These link files are treated like they were the actual directory or file that is being pointed to. Example: zcat remapped to gzip. Leftmost char is a l. spooey /home/kenrick> ls -l /usr/bin/zcat lrwxrwxrwx 1 root root 4 Feb 27 1999 /usr/bin/zcat -> gzip The numbers in the long directory listing from left to right are: access permissions, number of file entries, owner of the file, group ownership, date last modified, and then the name of the file.
File Access Control In unix, all files are protected to some degree under access control mechanisms. This allows the owner of the file to allow or deny access to other users. The first column of the long directory list shows the access permissions: e.g. drwxr-x--x The leftmost character indicates that this is a directory. The remaining characters repeat with the pattern rwx three times. In this example, we have: rwx r-x --x The leftmost group of three are the permissions for the owner of the file. The middle group of three are the permissions for the group owner of the file. Multiple users might belong to the same group that are working on the same project, for example. Or there might be a special group for faculty, and another for students. The last group of three are the permissions for all users, regardless of what group they belong to. The r indicates read permission is granted. The w indicates write permission is granted. The x indicates execute permission is granted. For the example file, this means that the owner can read, write, or execute the file. Users in the same group can read and execute, but cannot write to the file. All other users can execute the file, but cannot read it and cannot write to it. Changing file permissions To change the permissions on the file, use the chmod command. We can actually exercise what we learned with number representation, and use octal to change file permissions! The r,w, and x flags are represented by three bits. They correspond to the following pattern:
Binary Octal 000 r=0, w=0, x=0 = 0 001 r=0, w=0, x=1 = 1 010 r=0, w=1, x=0 = 2 011 r=0, w=1, x=1 = 3 100 r=1, w=0, x=0 = 4 101 r=1, w=0, x=1 = 5 110 r=1, w=1, x=0 = 6 111 r=1, w=1, x=1 = 7 The octal numbers are used to represent the r, w, and x permissions for either the owner, group, or everyone. For example, to set the permissions so that the owner has full access (r,w,x) and the group/everyone can only read the file (r only), use the chmod command with the following parameters: chmod 744 filename If we want the owner to have full access (r,w,x) and the group to have (r,x) and everyone else to have no access to the file: chmod 750 filename There is another way to change the permissions of a filename using flags, but it doesn t exercise your knowledge of binary to octal, so we ll skip it for now Some Basic Commands We have already described the ls function. Let s just give a recap and list of other commands you will likely use: Listing directories ls ls l ls al ls public_html ls /bin - List the contents of the current directory - List the current directory in long format - List long, all files (normally files starting with. are hidden) - List contents of the public_html directory - List contents of the /bin directory Changing directories cd cd public_html cd.. cd ~ cd. - Change back to your home directory - Change into your public html directory - Change back one directory - Change to your home directory (~ = home directory) - Change to the same directory (. = current directory)
Misc directory commands pwd mkdir foo rmdir foo - Print current working directory - Create a new directory called foo - Remove the directory called foo File manipulation commands cp prog1 prog2 - Copy the file named prog1 to one named prog2 cp prog1 ~/temp/prog2 - Copy prog1 to your home directory / temp / prog2 mv prog1 prog2 - Move the file named prog1 to prog2, delete prog1 rm file1 - Removes the file named file1. There is no undelete! cat prog1.java - View the contents of prog1.java as an ASCII file more prog1.java - View the contents of prog1.java as ASCII, one screen at a time For all file manipulation commands, you can use wildcard characters. * matches an arbitrary number of characters, while? matches a single character. For example: mv *.html public_html Would move all files ending in.html into the public_html directory. Other commands man <command>!<pattern> script - Shows the manual pages on <command>, e.g. try man ls - Searches back and executes the last command you typed that started with the pattern. For example,!g would execute the last command you typed that started with the letter g. - Saves or records the results of all the commands you run into a file named typescript. Type exit to stop recording the script. Redirection, Pipe command > output - Redirects the results of command to the file named output e.g. ls > directorylist makes a file named directorylist with the contents of the directory. command1 command2 - Redirects the output of command1 into command2. Commonly used with more to pause: e.g. ls l more
Application Programs pine vi joe pico emacs ftp ssh scp - email reader, menu driven - Visual editor - Another text editor - Easy-to-use menu-driven editor - Text editor, supports mouse - File transfer protocol, transfer files between systems - Secure shell, login to a remote system securely - Secure copy, copy file to remote system securely