Introduction to UNIX Contents * Objectives of this Tutorial * Unix File System o Unix Filenames + Shortcuts o File Attributes * General Concepts o Upper and Lower Case o Binary or Object Files * Basic Unix Commands o Man Pages + man + xman * Further Reading and Advanced Topics Objectives of this Tutorial This tutorial is designed to give the reader a basic introduction to the Unix environment. Special attention will be paid to the Unix file system and how to use these commands in order to accomplish basic tasks. Most of the material was adapted from the document Introduction to Unix and X-Windows in the ACL which was produced and published by the Department of Computational Science. Unix File System The Unix File System is a system for organizing files. In Unix, everything is one of three file types. Ordinary Files: Ordinary files contain the text of documents or program instructions. Executable programs (binary files) are stored as ordinary files. There is no record structure imposed on files. Directory Files: A directory holds the names of other files or directories. A directory can be read but not written. Special Files: Special files corresponding to devices. The Unix File System organizes files hierarchically to make finding them easier. This is achieved with the use of directories and subdirectories (a subdirectory is just a directory within a directory). The top level directory in Unix is /, the root directory (sometimes called slash). Subdirectories of / listed below. The first 8 are common to most versions on Unix. The remainder are locally created subdirectories. Directory Contains /bin most commonly used Unix commands /dev names of hardware devices /etc Unix administration files and programs /home location of machine home directories /lib system libraries /sys system commands, kernal interface /tmp temporary Unix administration files and programs files /var variable length files /usr(/usr/local) installation specific commands and directories Back to Contents Unix Filenames Unix filenames can be composed of any character except a blank or a /. A blank is a separator in Unix. A / is an indicator of a subdirectory. The maximum length of a filename is 256 characters. Whenever you reference a file in Unix, you can specify just the file name or you can specify the directory path and the file name. If you do not specify the directory, the default is the current directory. When a filename starts with a dot (period, .), it becomes a hidden file. That is, it is not normally listed when looking at the contents of a directory (ls) or when pattern matching is being done. This is typically used for system control or configuration files so that you don't have to see them every time you look at your directory. Shortcuts Often, you want a command to process many files at once, or you need to refer to a file that is located in a different directory. There are many shortcuts that can be used to reduce the amount of typing necessary. The asterisk (*) matches all characters in a file name, and a question mark (?) matches a single character in a file name. For example to delete all files that have the common identifier ``test'' (such as test.data and test.rainfall and test.rain.march), you could use the command: rm test.* Note that in Unix this would delete test.1 and test.fred.1, but not test or testrain.march. To delete all files that have `test' as the first part of the filename, use the command: rm test* Another shortcut when referring to files in the parent directory (the first subdirectory to the left in the full path name) is "../filename" A more powerful shortcut is the "~" (tilde) directory. The "~" refers to your home directory, the directory that you are using when you log in. The "~" combined with a user name ("~mickey") refers to the home directory of the user. File Attributes File protection is an important part of any operating system. To understand how file protection works on Unix, we must first look at file attributes (aka file mode). drwx------ 2 hovdesta 1536 Nov 18 15:09 Mail drwxr-xr-x 7 hovdesta 512 Aug 23 09:47 Tex -rw-r--r-- 1 hovdesta 3389 Oct 28 21:50 X.Defaults -rw-r--r-- 1 hovdesta 3389 Nov 1 16:57 X.defaults -rwxr-xr-x 1 hovdesta 274 Oct 30 21:16 addext The file mode is represented by the first ten characters in each line (e.g., -rwxr-x--x. This information consists of four parts: * A one character field describing the type of entry. The most common types are d (directory) and - (file). * A three character field representing the file-access permissions for the user (the owner of the file) (e.g., rwx). * A three character field representing the file-access permissions for the group (all other members of the user's assigned group (e.g., r-x). * A three character field representing the file-access permissions for all others (all other users on the system) (e.g., --x). The access privileges are as follows: r permission to read the file. w permission to write the file (this includes deleting the file). x permission to execute the file. - the indicated permission is not granted. Thus the file mode -rwxr-x--x can be interpreted as follows: - the directory entry is a file. rwx the user (i.e., owner) can read, write and execute the file. r-x the group can read and execute the file. --x all others can execute the file. The default settings for files and directories depends on the umask value. To change the file mode, the command chmod is used. The man page for each of these commands provides more detail. General Concepts This subsection is intended to introduce some general concepts about Unix in order to prevent some of the most common errors made by beginner Unix users. Upper and Lower Case Unlike DOS and MacOs, Unix is type case sensitive. Thus the following filenames represent different files. Filename filename FILENAME FileName FILEname FiLeNaMe Case sensitivity applies to commands as well as files. Typically, Unix commands are in lower case. Basic Unix Commands There are far too many Unix commands to describe in detail. This section lists some of the basic commands with a brief description. To obtain more details about any of these commands use one of the commands described in this section to access the man pages. Commands which are very important and are commonly used have an asterisk beside their entry. Pay special attention to these commands because you will probably need them soon. Command Function * cd Change working directory chmod Changes file permissions * cp Copies a file date Displays the date du Summarizes disk usage find Finds files (also try ~/bin/ffind) find has a horrendous format - to use ffind- type 'ffind filename'. It will then look in the current and all sub-directories for the file "filename" finger Displays information about a user or users ftp File transfer protocol kill kill, destroy, terminate processes ln Creates a link * ls Lists contents of directory man get information on a command * mkdir Creates a new directory more, page, less Filters used to display files on a screen mv Moves or renames files ps Gives status of running processes * rm Removes files and links * rmdir Removes a directory whereis Locates binary and/or manual for program which Locates a program file, including aliases and paths who Lists users who are currently on the system whoami Prints the effective current username w Shows what users of the system are doing ---------------------------------------------------------------------------- Examples of Usage The following command makes a copy of the file names old_name.file to another new file names new_name.file. Note that there is now two copies of the same file. cp old_name.file new_name.file The first command below changes the current directory to the home directory of the user. The second command changes the current directory to a sub-directory called programs/ass1 that is located in the home directory of the user. cd ~ cd programs/ass1 The following command displays a listing of the current directory. The -al modifies the command slightly. The a makes the lscommand list all entries including hidden files. the s makes the ls command display the size of all of the files. ls -al The following sequence of commands creates a directory called exercises off the home directory of the user: cd ~ mkdir exercises The following sequence of commands removes the directory which was created in the last example: cd ~ rmdir exercises The following commands remove all of the files that begin with assignment from the home directory of the user: cd ~ rm assignment* Man Pages There are on-line manual pages that may be accessed in several ways. man The command man displays information from the reference manuals. It can display complete manual pages that you select by title, or one-line summaries selected either by keyword (-k), or by the name of an associated file (-f). (You will rarely use man -f, and usually only man or man -k.) xman xman is a manual page browser. You can leave it running throughout your entire login session. In the initial window there are three options: Help will pop up a window with on-line help, Quit will exit, and Manual Page will pop up a window with a manual page browser in it. Typing Control-S will pop up a window prompting for a specific manual page to display. You may display more than one manual page browser window at a time from a single execution of xman. An excellent way to browse through many UNIX commands with instant access to their description is to run xman with the following parameters: xman -bothshow -notopbox