Introducing UNIX
Total Page:16
File Type:pdf, Size:1020Kb
CHAPTER 2 – BECOMING FAMILIAR WITH UNIX COMMANDS Updated by: Eng. Rana AlQurem Command Basics Commands are mostly implemented as disk files which contains executable code Mainly written in C language Loaded to memory when invoked, then executed Commands are case sensitive Usually 4 characters or less UNIX Doesn’t require command names to have extension Some applications (not UNIX) require files to have extensions such as C and JAVA compilers The shell is a special command: runs continuously as long as you are logged UNIX provides a full-duplex terminal Separate channels for input/output Users do not have to wait for a command to complete before typing the next one; just continue typing even if the output of the previous command clutters the display The PATH: Locating Commands One of the most important environment variables is the path. It defines where the shell looks for commands. Set to a colon-delimited list of directories Shell looks at PATH only when command is not used with a pathname and is also not a shell built-in. To see the path value, type “echo $PATH” Ex: echo $PATH /usr/local/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/usr/dt/bin:/bi:. Common Directories /bin This is the directory where you typically find essential user commands such as ls and man. /usr/bin Holds most of the common user commands and basic applications /usr/local/bin Holds commands that are local to the current machine and are not common amongst machines. May not be used. $HOME/bin Where you should store any executable programs that you create. $HOME indicates your home directory. Types of Commands External commands are those that execute by running programs located in the PATH. They exist as actual files with execute permission. a binary executable (written in C, C++). a script file (like a shell or perl script). Examples: ls, cat, more Internal commands are those that are written into the shell and don’t correspond to an executable file an alias defined by the user that invokes the disk or internal version in a specific manner. Examples: echo, type, cd, pwd How the Shell Determines the Command to Run If command is invoked with a pathname (like /bin/echo), the shell runs program at the specified location. If command is invoked without a pathname, the shell first checks whether it is an alias or built-in (internal): If alias or built-in, the shell runs it without looking in disk. If not, the shell looks at the PATH variable for directories where the command may reside. If found, it is executed. Otherwise, an error is output Where is the command? Three commands that provides help on the location of another command: which, whereis, and type which searches the directories of PATH in sequence Ends on the moment it locates the command, or an error Ex: which grep will return/usr/bin/grep whereis Defined in BSD-based UNIX systems (solaris) Not confined to the list of directories in PATH. It uses a larger list Where is the command? (contd) Both of which and whereis don’t consider whether the command is external or internal Ambiguity arises when the command is also a shell built-in Type Indicates if a command is built into the shell or gives its location if known. For example, type echo will return “echo is a shell builtin” Which echo will return /usr/bin/echo even though this is not actually what is run when you type echo Structure of a Command Command -Options Argument1…ArgumentN Command = the name of the command, usually 4 characters or less and need no specific extension Options = parameters with a fixed meaning that change how the command works Arguments = parameters used by the command. These are typically file or directory names on which the command acts. Structure of a Command (contd) A command’s behavior is determined by its arguments and options. Command and arguments must be separated by whitespace. Generally possible to combine multiple options into a single one (like ls -l -u -t == ls -lut) Order of combining is generally not important (like ls -lut == ls -utl) Some options have their own arguments If multiple options that include parameters are combined, then the parameters must be given in same order Recall that commands are case sensitive !!! Command Examples ls ls –la ls –la m* lpr –Pspr –n 3 proposal.ps Flexibility of Command Usage Special characters (; > | etc) are not considered as arguments since they are not seen by commands. the shell does some processing of the command line before executing the commands Run multiple commands by specifying them in the same line: date ; echo $PATH Split a command into multiple lines: $ echo “Hello > Dolly” Whenever you find ? or > appearing after pressing “enter”, it indicates the missing of a matching quote or parentheses Save command output in a file (not always on the screen): date > foo.tx Use output of one command as input of another: date | cut -d” “ -f2 Run a command in the background with &: ls -lRa / > $HOME/ls-lRa.txt & Man - Online Help Displays documentation of commands, configuration files, system calls and library functions. Organized in a number of sections. Commands are found in Section 1. May need to use section number when entry exists in multiple sections (e.g. man passwd and man -s 5 passwd). man pages are available on the Internet man documentation not available for most internal commands of the shell. Use man man first to know how man should be used. Man - Online Help (contd) Type man followed by the name of the command to get help documentation. Man pages are viewed using more ( Linux uses less) and can be navigated using the following commands: Spacebar or f, go forward one screen (page) b, go back a screen (page) Period, repeat the last command /string, searches for (locates) the pages that contains the “string” q, quit man –k keyword: searches the NAME section of all pages that contain the keyword apropos command emulates man -k man –f cmd : display a one line header from the NAME section whatis command emulates man –f Man Sample Reformatting page. Please Wait... done User Commands wc(1) NAME wc - display a count of lines, words and characters in a file SYNOPSIS wc [-c | -m | -C] [-lw] [file...] DESCRIPTION The wc utility reads one or more input files and, by default, writes the number of newline characters, words and bytes contained in each input file to the standard output. The utility also writes a total count for all named files, if more than one input file is specified. wc considers a word to be a non-zero-length string of char- acters delimited by white space (for example, SPACE, TAB). See iswspace(3C) or isspace(3C). --More--(23%) Man Sample Explained Example: man wc Syntax/Synopsis wc [ -c | -m | -C ] [ -lw ] [ file ... ] Most useful information available in SYNOPSIS and DESCRIPTION. When options grouped in [ ] without a |, one or more of them can be used. (-l, -w and -lw are valid.) The | signifies an OR condition. (Only one of -c, -m or -C can be used.) The ... means that multiple occurrences of the preceding item are possible. (wc can be used with multiple files.) EXIT STATUS indicates values returned on error has importance when you use the command in a shell script Info-Another Help Option In some Unix systems the documentation is given in the GNU info format and not in a man page. Thus, try info cmnd if the man command returns “No manual entry”. To navigate an info page use the following: Use the cursor and enter to select nodes p, go to the previous page n, go to the next page u, go up a level q, quit echo: Displaying Messages echo: used to display files or diagnostic messages. In shell scripts it is used to issue prompts for taking user inputs or evaluating shell variables (variables starting with $) Usage: echo [string to be displayed or filename] $ echo "hello" hello $ echo $SHELL /bin/bash printf: Alternative to echo Like echo, it exists as an external command Except Bash has it built in Unlike echo, you must use \n – escape sequence $printf "Testing" Testing$printf "Testing\n" Testing $printf "My current shell is %s\n" $SHELL My current shell is /bin/ksh printf uses format specifiers similar to what used in C language (e.g %s) script – Recording Your Session Lets you “record” your login session in a file The file is specified an argument to the command If not specified, the default is typescript Keeps the log of your activities Once called, a script is started and the prompt returns All keystrokes entered are saved in the script file At the end you can terminate the session with exit command The script file can be viewed now If you don’t exit, the file can get bigger and consumes your space script Example $ script $ cat typescript Script started, file Script started on Wed Jan is typescript 23 18:21:26 2008 $ ls $ ls Mail foo public_html Mail foo public_html userlist.txt userlist.txt PUTTY.RND foo.sh PUTTY.RND foo.sh typescript typescript $ echo $SHELL $ echo $SHELL /bin/ksh /bin/ksh $ exit $ exit Script done, file is script done on Wed Jan 23 typescript 18:21:40 2008 Other Commands passwd: changing your password uname: display Machine’s Name and Operating System uname –r : shows current release uname –m: shows the machine name (hostname or domain name) who: Know the Users currently logged in who am i: info about you date: display current date and time date +%m – display current month date +%h – display current month name stty: When Things Go Wrong (shows terminal characteristics) Exercise Use man to find out what the following commands do ls grep cd sort rm who rmdir whoami cp head mv tail cat date cal tar more script .