UNIX NOTES UNIX/Linux Notes: Page 1 Professor: Michael P. Harris v.20091109 UNIX is a multi-user, multi-tasking operating system originally developed at AT&T Bell Laboratories. It provides programs for editing text, sending electronic mail, preparing tables, performing calculations and many other specialized functions that require separate application programs in other operating systems. One of the key identifying features of UNIX is its standardized directory and file structure. Typical UNIX/Linux root / Directory structure: │ ┌────┬────┬────┬────┬───────┬┴────────┬───┬───┬───┬────┐ bin etc dev mnt lib lost+found home tmp sys boot unix │ │ │ default │(mounted ┌────┬────┬─┴───┬───┬───┬────┐ ┌───┬┴──┐volumes) mike teri ... etc bin lib spool hd0 fd0 sd0 (users home directories) There are many different versions of UNIX. One of the most important is the line of UNIX releases that started at AT&T, the latest being System V Release 4. Other important UNIX versions have come from the University of California at Berkeley; the latest is called BSD. Microsoft’s Xenix, a early popular microcomputer implementation of UNIX was originally based on an earlier AT&T release called System V. Linux is currently the leading popular “Open Source” implementation of a Unix-like standard operating system. POSIX is the validation standard, set for UNIX-like operating systems. Starting with Windows 2000 Pro, Windows 2000, XP, and 2003 are all now POSIX compliant. Other major versions of UNIX include SunOS, Solaris, SCO UNIX, AIX, HP/UX, ULTRIX, NOVELL UNIX (Univel), and the various implementations of the popular Linux OS. Since it started more than 35 years ago, UNIX has grown and changed in a different way from most operating systems. Many early UNIX users were computer and scientific professionals; more than a few of them extended UNIX by adding tools and functionality to do what they needed. UNIX was also used to develop a lot of the powerful networking systems that connect the world, such as Internet. So, whereas most operating systems were typically developed and controlled by one corporation, UNIX has been developed through a collective effort. This has meant more versions and somewhat more confusion than "standardized" operating systems like MSDOS & Windows. But it has also helped to make UNIX the flexible and incredibly rich and powerful operating system that it is today. UNIX and its derivatives were not designed as "user-friendly" operating systems, in fact, UNIX has, in the past, earned its label of "user-hostile." UNIX can be used the way it was originally designed, on typewriter-like terminals (ttys). Most modern versions of UNIX can also work with window system GUIs (Graphical User Interface) which allow each user to have more than one "terminal" on a single display. The X Window System (called X for short) is the most common UNIX window system. The appearance of X Windows may vary between systems due to different window managers. Four of the most common window managers are mwm (Motif Window Manager), olwm (Open Look Window Manager), CDE (Common Desktop Environment) and the new Linux windows shells Gnome, KDE, fluxbox, and others. UNIX NOTES Page 2 Getting Started: UNIX can be overwhelming with its barrage of details. To get started though, all you need to do is master just a few details: o o o o o How to Log in and out of your system Understand the Shell and how to control the system with control characters Learn the important UNIX specific directories and files Manage UNIX files and directories, including listing, creating, copying, printing and removing files, and moving in and out of directories Work efficiently using UNIX pipes, filters, and multi-tasking. Logging In: Before you can start using UNIX and its facilities, the System Administrator has to set up a UNIX account for you with your login name, password, and home directory. At many sites, there will be a whole network of UNIX computers so you may also have to know the hostname of the computer that has your account as well. When you turn on your terminal you should see a message from the UNIX computer that looks something like: login: Logging in is the process of making your self know to the UNIX computer and getting into your account. On remote systems you will need to run a remote login program like telnet, rlogin, cu, or tip. In this case your login session might look similar to the following: (what you type is in bold) telnet mercury.delmar.edu Sun Solaris OS mercury.delmar.edu: Solaris UNIX version 9.00.10 login: mpharris Password: ******** Last login: Wed Nov 28 14:34 CST 2005, from mercury.delmar.edu motd: The "crisis" of Today is the "joke" of Tomorrow REMEMBER !!! We will be shutting down at 4:30pm for system backups. Fri Dec 2 12:24:48 CST 2005 $_ The UNIX Shell: UNIX NOTES Page 3 Once you've logged into a UNIX computer, you're working with a program called the shell. The shell interprets the commands you enter, runs the program you've ask for, and generally coordinates what happens between you and the UNIX operating system. There are four shells in common use: the Bourne shell, Korn shell, Bash and C shell. For the novice the differences are slight however some to run some software or execute some program (especially shell scripts) you will need to know which shell you are using (ask your system administrator). The only immediate difference you will see is that the Bourne, Korn, and Bash shells prompt you with $_ while the C shell most often uses %_ as the prompt. Certain keyboard commands (keystrokes) are interpreted by the shell, these are commonly called control characters. The basic control characters are: Ctrl-C or [DEL] Ctrl-D Ctrl-H or [BACKSPACE] Ctrl-Q Ctrl-S Ctrl-U Interrupt character (interrupts or cancel a command) Returns you to UNIX command level. Used to signal end of input. ** Also logs you off of most UNIX systems Erase character to left of cursor Restart output after paused by Ctrl-S Pauses output to the screen Erase the whole input line so you can start over UNIX vs. MSDOS The UNIX operating system has been around much longer than MSDOS. While MSDOS owes much of its original heritage to a single-user operating system named CP/M, MSDOS evolved gaining more and more UNIX like capability. MSDOS is not case sensitive meaning that commands can be typed in either upper or lower case and MSDOS treats them all as if they were typed in UPPER CASE. UNIX is case sensitive meaning that there is a difference between a command typed in upper or lower case. The UNIX standard is that commands are typed in lower case. A brief comparison of MSDOS and UNIX command equivalents follows: CD CD DIRNAME CHKDSK COPY FILE1 FILE2 COPY FILE1 + FILE2 FILE3 COPY FILENAME PRN DIR DIR /W DIR /P DEL FILENAME MD DIRNAME REN FILE1 FILE2 RD DIRNAME TYPE FILENAME pwd cd dirname fsck cp file1 file2 cat file1 file2 >file3 pr filename | lpr ls ls -C ls | more rm filename mkdir dirname mv file1 file2 rmdir dirname cat filename UNIX NOTES Page 4 File Access and Permissions An important point to always remember is that in UNIX everything is a file. The textbook definition of UNIX usually goes something like this... UNIX - a multi-user/multi-tasking interactive operating system developed by AT&T. UNIX has a directory structure that is tree-like with files containing all the information that is part of the system. Most people familiar with microcomputers and MSDOS are familiar with the tree-like directory structure with directories, sub-directories and pathnames. But in UNIX the directory structure and file system is the heart of the system. UNIX is also device independent --there for no duplication of commands is necessary for different devices such as the screen, printer, or disk drives. For example, UNIX always keeps the files in its directory structure is a sorted order, there for to rename a file is really moving that file within the directory structure. To copy that file to another directory, to the screen, or to a floppy disk drive is the same command because UNIX is device independent, everything is a file, the destination directory, the screen, the auxiliary storage device are all files to UNIX. This concept may take a little getting used to before all that is means sets in. To log into a UNIX system your terminal must be described to the system. In the directory /dev the system device files (files that contain the hardware interfacing information UNIX needs) are contained. In addition, your terminal type is stored in /etc/ttytype, your terminal speed in /etc/ttys, and your terminal setup and capabilities data in /etc/termcap. The advantage of this system is obvious, if you change your terminal or enhance its capabilities, your just edit the system files. In addition you can have multiple definitions and settings for multiple different users. The UNIX executable commands are kept in the /bin directory with miscellaneous system data files kept in the /etc directory. UNIX commands are usually short and terse and most always in lower case. UNIX filenames may include A-Z, a-x, 0-9, . and _. After UNIX boots from the kernel code stored in /unix directory, the /etc/rc startup file is executed. This (reboot cold) startup file would be the equivalent of the MSDOS CONFIG.SYS and AUTOEXEC.BAT files. The system default setup information is stored in the /etc/default file. When users log into a UNIX system, there access is controlled by password and access information stored in the /etc/passwd and /etc/group files. Once logged in users are met by the users login message-of-the-day stored in the /etc/motd file and then the users personal startup and setup commands are executed from their own private .profile file stored in their /home/username login/home directory. UNIX NOTES Page 5 One logged into the UNIX system file access is controlled by the standard UNIX permissions: r w x -. The permission settings are for read, write (or change), eXecute, and permission denied (-). The permissions are grouped into a set of three rwxrwxrwx where the first three are the permissions for the user/owner of the file, the second three are the permissions for members of the group assigned to the file and the last three permissions are for all others who may wish access to the file. The command chown is used to change the owner of the file. The command chgrp will change the group assigned to the file. And the command chmod (for change access mode) will change the file permissions. Of course, you must have write permission to change the owner, group assignment, or permissions to a file. As an example, assume the ls command (list single file) gave you the following permission settings for the file /etc/motd... rwxr-x--x These permission settings would indicate that the owner (usually supervisor) has Read, Write, and eXecute permissions to the file. Those who have the assigned group access permissions can Read and eXecute the file, all others may only eXecute the file. When changing permissions to your files with the chmod command, you have the options of a, u, g, and o (for All, User/owner, Group, and Others) the syntax is: chmod options filename where options can include: (1) a, u, g, o (2) + or and (3) r, w, x in any combination. For example: chmod g+rx myfile would add Read and eXecute to the group portion of the permission settings. Alternate Input / Output, Piping, and Redirection The Unix commands for redirecting Input and Output are: > >> | For example: redirect output redirect output append pipe (output into input) < << tee redirect input redirect input append split output into two streams who | sort > wholist would execute the who command (generate a list of who is currently logged on to the system) and pipe the output into the input of the sort command (alphabetize) and the sorted list would be written to the file wholist. and... banner < wholist >> register the file contents of wholist would be fed into the command banner (which generates large bannerized letters from file contents) and add this output to the end of the file register. UNIX NOTES Page 6 UNIX DIRECTORY GUIDE Typical UNIX/Linux root / Directory structure: │ ┌────┬────┬────┬────┬───────┬┴────────┬───┬───┬───┬────┐ bin etc dev mnt lib lost+found home tmp sys boot unix │ │ │ default │(mounted ┌────┬────┬─┴───┬───┬───┬────┐ ┌───┬┴──┐volumes) mike teri ... etc bin lib spool hd0 fd0 sd0 (users home directories) / UNIX root directory /bin UNIX Commands (binary files) /boot UNIX file system bootstrap loader programs /etc miscellaneous system data files /etc/default system default setup files /dev peripheral device control files /lib libraries for the 'C' compiler /lost+found depository for misplaced files /mnt empty directory to mount file systems /sys code for UNIX kernel (operating system) /tmp temporary files (may be auto deleted) /home users master/personal directory (or /usr, or /user) / home /bin more commands usually user created / home /lib more language libraries and data files / home /spool print spooler master directory / home /tmp users temporary files / home /asm administration accounting files /unix executable code for UNIX kernel UNIX NOTES Page 7 UNIX FILE GUIDE . name of current directory .. name of current directory's parent .profile set up environment at login (personal startup file) /etc/group designate group 'names' /etc/logbook logbook of installed software /etc/motd (message of the day) login message for users /etc/passwd define system users and access /etc/rc define system initialization and startup commands /etc/systemid define system name /etc/termcap define standard terminal setup information and capabilities /etc/ttys define terminal port status/speed /etc/ttytype define default terminal type /home/lib/crontab time & date for auto execution /home/adm/messages record of console messages Sample UNIX / home directory with subdirectories: / │ home ┌─────────────┬──────────────┬┴─────────────────┬─────────┐ adam betty edward mark ... │ ┌───┼────┐ ┌──┴─┬────┐ ┌─────┴┬────┬────┐ ├demo1 memo etc letter util fax pgms news bin fax make └demo2 │ │ │ │ │ ├mm1 ├let1 ├fax1 ├jan ├mod1.c └mm2 ├let2 └fax2 ├feb └mod2.c └let3 └mar All users have a personal .profile file in /home/username/.profile UNIX NOTES Page 8 ALPHABETICAL LIST OF UNIX COMMANDS The commands in the Basic System are listed below in alphabetical order. asktime assign at atq atrm awk banner bc bdiff bfs cal cat cd chgrp chmod chown chroot cmp comm copy cp cpio cron crypt csplit cu date dc devnm df diff diff3 dircmp dirname disable dtype du echo ed egrep enable env ex expr false set system date and time assign a device to a user execute commands at a later time examine the "at" job queue remove a job from the "at" job queue pattern scanning and processing language print large letters arbitrary-precision arithmetic language compare very large files scan big files print calendar concatenate and print files change working directory change group change mode (change access permissions) change file owner change the process root directory compare two files (any type) select or reject lines common to two sorted files copy groups of files copy copy file archives in and out execute commands at specified times encode or decode a file split files according to context call the UNIX system print and set the date desk calculator identify device name report the number of free disk blocks compare two text files compare three text files compare directories deliver the directory part of a path name turn terminal use off print disk type (such as Xenix, MSDOS, tar) summarize disk use echo arguments invoke text editor (line editor) search a file for a pattern turn terminal use on set or print the environment for command execution text editor (line editor) evaluate arguments as an expression provide truth value by returning with a nonzero exit code UNIX NOTES fgrep file find finger fsck grep grpcheck haltsys hd head id join kill l lc learn line ln login logname look lpr ls mail mesg mkdir mkfs mknod mkuser more mount mv newgrp nice pack passwd pr ps pstat pwcheck pwd quot random remote restor rm rmail Page 9 search a file for a pattern determine file type find files find information about users check file system for consistency and repair if necessary search a file for a pattern check group file shut system down give hex dump of a file give first few lines of a file print user and group ID and name join two relations terminate a process list directory contents in long form (equivalent to ls -1) list directory contents in columns give computer-aided instruction about UNIX read one line make a link to a file give access to the system get login name find files in a sorted list send files to the line printer queue for printing list the contents of a directory send, receive, or dispose of mail permit or deny messages sent to a terminal make a directory make a file system make a special file add a new user account display a file one screen at a time attach a file system to a directory on the root subtree move or rename files and directories log into a new group run a command at a different priority compress files change login password print a file report process status print system facts check the password file print the name of the working directory summarize file system ownership generate a random number execute commands on another machine invoke incremental file system restorer remove a file send mail among users UNIX NOTES rmdir rmuser rsh sdiff sed setmnt settime sh shutdown sleep sort split stty su sum sysadmin tail tar tee test touch tr true tset tty unmask unmount uname uniq uucp uulog uux vi vsh wait wall wc what who whodo write xargs yes Page 10 remove a directory remove a user invoke a restricted a shell compare two files side by side invoke stream editor establish a mount table (/etc/mnttab) change file access and modification dates invoke the Bourne shell shut down the system suspend execution for an interval sort or merge files split a file into pieces set terminal options make the user root or another user temporarily calculate checksum and count blocks in a file perform file system backup and restore deliver last part of a file archive files create a tee in a pipe to save intermediate output test conditions update file access and modification times translate characters return with a zero exit value set terminal type get terminal name set default file creation mask detach a file system from the root directory print the current UNIX name report repeated lines in a file copy files from UNIX to UNIX copy files from UNIX to UNIX execute commands on remote UNIX invoke a screen-display editor based on ex invoke the visual shell wait for background jobs to finish write to all users count lines, words, and characters identify files list users currently logged on show who is doing what send a message to a user's terminal construct argument lists and execute commands print string repeatedly