These are a few basic Unix commands compiled from Unix web sites, and printed materials. The main purpose is to help a beginner to go around with fewer difficulties. Therefore, I will be adding to this document base on our users experience, and suggestions. I have tried to adopt these material to our specific environment, however, it does not mean that all the commands and syntax would work on our system. If you encounter any problem, please email me. Afshin. Tel: 978-6618 Email: afshin@power.ele.utoronto.ca IMPORTANT: Do not switch off any of the workstations. Do not reboot / shutdown any of the workstations. Do not modify or rearrange the network cables. Do not send print jobs larger than 2.5 Meg to the printer. Make sure you logout before you leave a workstation. If you do not get your print output, make sure you remove the original print request by using "lprm", before re-sending it. Retrieve your old emails from the server. Pack and compress them in your own home directory. LOGGING IN To use the workstation, you must first login. To do so, you must have (at the minimum) a login name by which the computer and other users will know you as well as a password which you should keep to yourself. In fact, since the password protects your work and the entire system from unauthorized and careless users, and since it is relatively easy for other users (all throughout the networked world no less!) to move through Unix systems, you should carefully guard your password. In addition to your login name, the system will also assign you a user ID number and a group ID number. The user ID is a number which has a unique correspondence to your login name and which the system uses to keep tabs on you. When personal information is printed, such as in response to ls -l or ps commands, the system matches the user ID to the login name and prints out the login name. The group ID is a higher level identification shared among users which automatically permits (or forbids) a number of users permission to read and write the same files. Essentially all Unix commands require you to end or activate them by hitting the key labeled Enter (or Return or Carriage return or [ret]). If you make a mistake keying in your command, you can correct it with the Backspace key if you have not yet entered in [ret]. After you have given the computer your login name and password, there is a short delay while the computer validates your account. It then responds by introducing itself, telling you if you have mail, possibly asking you to identify the type of terminal you are on, and setting off a chain of actions as commanded by a file labeled.login or.profile. Finally, with a typically verbose Unix response, the computer presents you with a prompt. Logging Off Before we continue we should mention how to quit your workstation. The command logout will log you off the system. The command exit also works; it terminates the shell you are using and may be used to close xterm windows and in other situations where logout will not work. You can also use bye to terminate your session. 1
ANATOMY OF A UNIX COMMAND command- name -option( s) filename( s) or arguments Example: wc -l sample The first word of the command line is usually the command name. This is followed by the options, if any, then the filenames, directory name/ names, or other arguments, if any, and then a RETURN. Options are usually preceded by a dash and you may use more than one option per command. The examples on this reference card use bold case for command names and options and italics for arguments and filenames. Important Note about UNIX Commands UNIX commands are case sensitive. Type commands exactly as shown; most UNIX commands are lower case. File and directory names can be lower, upper, or mixed case but must be typed exactly as listed. Naming Files & Directories In general, file and directory names should be composed only of letters of the alphabet, digits, "." s, and "_" s. Be aware that files that begin with a "." do not appear in the directory list unless a special flag has been set when doing the list command. The period is often used to add a suffix on to a base filename. For example, the source code for C programs have a.c suffix added to them, e.g. prog.c. Separating a filename by a "." is particularly useful when using wildcard selections. Printing Files When you give the print command lpr, your print job is placed in the printer queue. If the printer is set and working properly, it will print out your job in turn. To check the status of your print job and to determine its print job number, use the lpq command. To stop a job from printing (say because you submitted the Encyclopedia Britannica by mistake, or because the printer is chewing up your output), try the command lprm or cancel, followed by your job number. Stopping a print job by turning off the power to the printer will certainly stop the printing, but it may disable the printer from further use by the system or confuse a (not so) smart printer or printer spool, or both. The printer can be enabled with the lpc or enable command, but it may take some system work to get the print spooler program working again. BSD PRINT (All But ultra) # lpr -Pprinter file(s) (Use lj for printer or type in " lpr file) Place file(s) in line printer queue. # lpq -Pprinter List files in line printer queue. # lprm -Pprinter job # CANCEL a printing job (get # from lpq). # lpc Line printer control to enable printer. 2
SYSTEM V PRINT (ultra) # lpstat List printers, devices, files in queues. # lp -dprinter filename(s) Place file(s) in line printer queue. # cancel job # CANCEL a printing job (get # from lpstat). # enable ENABLE a printer (may have to be superuser). example, % lpr prog.f (no -P, using default printer) Print prog.f on line printer. % lpq Tell me files being printed. % lprm 94 Stop printing prog.f. The printer is busy so try elsewhere. % lpq -Pmath (-P identifies a printer) Check the math dept.'s printer. no entries OK, it's idle. % lpr -Pmath prog.f Send the file to be printed. Search and Compare Commands You can try any of these. If there are filenames or other variables you need to change, they are indicated by italics. # diff file1 file2 List lines in file2 differing from file1. # file fname List fname's type (ASCII, text, directory, object). # grep pat fname Print lines in file fname containing pattern pat. # sort fname 3
Sort lines of fname based on options. Job Control Commands You can try any of these. If there are filenames or other variables you need to change, they are indicated by italics. # ctrl-z Stop active foreground job. NOTE: ctrl-z suspend the foreground job, it is still active. To stop it completely use "ps" to get the process id number, and then "kill" it. # bg Place stopped foreground job into background. # fg job Place background job into foreground. # jobs Return names and job no. of suspended jobs. File Manipulation Commands You can try any of these. If there are filenames or other variables you need to change, they are indicated by italics. # mv fold fnew Copy fold to fnew, remove fold (i.e. rename), files can be in different directories. # rm fold Remove (delete) fold. # rm -rf directory Remove directory and everything in it. # cp fold fnew Copy fold to fnew, destroy previous fnew. # cat filea > fileb Copy filea to fileb. # cat filea >> fileb Append filea to end of fileb (add to end). # ls List files in present directory. 4
# ls -l A long list of files. Displaying files # display file contents cat myfile # determine file type file myfile # display file, a screen at a time (Solaris) pg myfile # display first 100 lines of a file head -100 myfile # display last 50 lines of a file tail -50 myfile # display file that is changing, dynamically tail errlog.out -f System status check Listed here are a few system monitoring commands which should give you a rough idea of how the server is running. # server information uname -a # disk free, listed in KB df -kt # processes currently running ps -elf # user processes w whodo who am i finger ps File permissions Permission flags: r = read, w = write, x = execute Permissions are displayed for owner, group, and others. 5
# display files, with permissions ls -l # make file readable, writeable, and executable for group/others chmod 777 myfile # make file readable and executable for group/others chmod 755 myfile # make file inaccessible for all but the owner chmod 700 myfile # make file readable and executable for group/others, # user assumes owner's group during execution chmod 4755 myfile # change permission flags for directory contents chmod -R mydir # change group to staff for this file chgrp staff myfile # change owner to jsmith for this file chown jsmith myfile Note: You can use the umask command to set the default mode for newly created files. umask specifies the permissions to TAKE AWAY from all new files. For example, to get the permission mode of 751 by default, compute 777-751= 026, this is the value you give to umask: # umask 026 Listing files LIST FILES AND DIRECTORIES ls directory- name - list contents of directory options: -a list all files including invisible files -l long list - shows ownership, permission, and links -t list files chronologically -f append * to executable file name, / to directory name -d list directories -u list files using time of last access instead of time of last modification -F mark directories with forward slash, mark executables with an @- sign. -g lists ownership of each file or directory pwd -display the name of present working directory Examples; # list all files, with directory indicator, long format 6
ls -lpa # list all files, sorted by date, ascending ls -lpatr # list all text files ls *.txt Moving/copying files # rename file to backup copy mv myfile myfile.bak # copy file to backup copy cp myfile myfile.bak # move file to tmp directory mv myfile /tmp # copy file from tmp dir to current directory cp /tmp/myfile. Deleting files See scripting examples for group dissection routines. # delete the file rm myfile # delete directory rd mydir # delete directory, and all files in it rm -r mydir Disk usage # display disk free, in KB df -kt # display disk usage, in KB for directory du -k mydir # display directory disk usage, sort by largest first du -ak / sort -nr pg 7
Using tar # display contents of a file tar tvf myfile.tar # display contents of a diskette (Solaris) volcheck tar tvf /vol/dev/rdiskette0/unnamed_floppy # copy files to a tar file tar cvf myfile.tar *.sql # append files to a tar file tar rvfn myfile.tar *.txt # extract files from a tar filem to current dir tar xvf myfile.tar Redirecting output Output can be directed to another program or to a file. # send output to a file runbackup > /tmp/backup.log # also redirect error output runbackup > /tmp/backup.log 2> /tmp/errors.log # send output to grep program runbackup grep "serious" View Commands # cat file Print file on screen (or as directed by >, >>). # more file Print file, one screenful at a time. # view file Use the vi editor for viewing a file. # head file Print first 10 lines of file. # tail file Print file's last 10 lines on screen. 8