1 / 95

TACCT 330 – UNIX OVERVIEW OF A UNIX SYSTEM

TACCT 330 – UNIX OVERVIEW OF A UNIX SYSTEM. UNIX Operating System - core concepts. Unit 1 Introduction to UNIX. What Is UNIX?.

Download Presentation

TACCT 330 – UNIX OVERVIEW OF A UNIX SYSTEM

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TACCT 330 – UNIXOVERVIEW OF A UNIX SYSTEM UNIX Operating System - core concepts

  2. Unit 1Introduction to UNIX

  3. What Is UNIX? • Textbook Definition: “A time sharing operating systems kernel: a program that controls the resources of a computer and allocates them among its users…[and] also [its] compilers, editors, command languages, programs… and so on” (Kernighan & Pike). • “An Operating System designed by Programmers for Programming” (Ken Thompson). • UNIX Motto: “Live Free or Die!!” • Open systems concept • Customizable environments • UNIX Philosophy: “Leave me alone… I know what I’m doing!!” • Minimal user interface • Minimal dialog with system • Cryptic and short commands • Less than friendly to beginning users • UNIX Caveat: “You are given enough rope to hang yourself.” Stanberry U1-1

  4. Why UNIX? • Portability: Open Systems Concept • Scalability: Mainframe to PC • Flexibility: Customizing, source code available • Compatibility: Standards • Security: Built-in safeguards • Performance: Robustness • Availability: Fault-tolerance • Multiplicity: Multi-user and multi-threaded • Efficiency: Minimal overhead • Modularity: Small programs accomplish complex tasks • Cost: Minimal, vendor independence Stanberry U1-2

  5. Comparison of Current Operating Systems/Network Operating Systems Stanberry U1-3

  6. UNIX History • Bell Laboratories in the late 1960’s and early 1970’s • Ken Thompson and Dennis Ritchie worked on the MULTICS project that was canceled. They ported pieces of the operating system to a smaller PDP-7 machine. In a play on words, they called the scaled down version UNICS which later became UNIX. • In 1970, UNIX was ported to a PDP-11 to support the Bell Labs patent office. • In 1973, the language was re-written in C language (developed by Dennis Ritchie) to facilitate porting in the future. • UNIX in the mid 1970’s to early 1980’s • UNIX becomes very popular “for educational purposes” in the academic environment. • In 1977, the University of California at Berkeley releases the early versions of the Berkeley Software Distribution (BSD) UNIX which eventually becomes an independent form of the operating system. • In 1983, Bell Labs release System V which becomes the modern incarnation of the original UNIX system. • UNIX Today • UNIX systems today are a combination of the System V and BSD versions of UNIX that attempt to be cohesive through the adoption of standards. Stanberry U1-4

  7. Flavors of UNIX • SVR4: SunOS (which is largely based on BSD UNIX) combines with Bell Labs (now AT&T) System V to provide the first hybrid system • SunOS/Solaris: Sun Microsystems • AIX: IBM • HP-UX: Hewlett-Packard • FreeBSD: Free, Internet • Linux: Free, PC platform • ULTRIX: Digital • XENIX: Microsoft • OS/390 Open Edition: IBM, Mainframe Stanberry U1-5

  8. Unit 2UNIX Concepts

  9. Open Systems Concept Open Systems: Through the use of standards, applications can interoperate and port data across a wide variety of hardware and software platforms. • Operational Goals • Portability across hardware • Device-independent file system • Tasks performed on distributed machines (client/server). • Efficient balancing of multiple users and tasks • Business Objectives • Maximize investments (scalability and portability) • Vendor independence • Standardization (re-work costs, training) • Support long-term life of applications • Maintain flexibility to respond to business needs Stanberry U2-1

  10. Major Components of the UNIX Operating System • Kernel • Applications • File System • Communications/Networking • Shell • Programming Environment • Utilities • Text Processing • Documentation • System Management Stanberry U2-2

  11. The Kernel • Definition: The central core of the operating system. • It is loaded into memory at boot time, although may be dynamically linkable. • It manages users, processes and security. • It controls I/O access to peripheral devices. • It is mostly written in the C language to facilitate portability. Applications Shell Kernel Hardware Stanberry U2-3

  12. The UNIX File System • Definition: The system of organizing files within the operating system. • In UNIX, every object is represented to the operating system as a file. All I/O on the UNIX system is file I/O. • There are only three types of files at the operating system level: • Ordinary - containing text. • Directory - containing pointers to other files. • Special - containing pointers to devices. • Communications/Networking is facilitated through the file system. Other machines and network objects are represented as files to the operating system. Stanberry U2-4

  13. The Shell • Definition: The interface between the user and the kernel of the operating system. It runs as a UNIX program that interprets commands and initiates user processes. Some shells facilitate programming. • Standard UNIX Shells • sh - Bourne Shell, written by Steve Bourne, default shell of original Bell Labs UNIX. • ksh - Korn Shell, written by David Korn, an extension of the Bourne Shell in System V UNIX that added scripting capabilities. Now it is generally the default shell in the System V descendants such as IBM’s AIX. • csh - C Shell, written by Bill Joy of UC Berkeley, generally the default shell of BSD descendants such as Sun Microsystems. Based largely on the C language, it most robustly supports operating system modifications. • bash - Bourne Again Shell, written by FSF/GNU (Free Software Foundation/GNU’s Not UNIX) founded by Richard Stallman of MIT. Used in extensively Linux and other free versions of UNIX. • Other UNIX shells include menu-driven and graphical interfaces that generate commands. Stanberry U2-5

  14. The UNIX Programming Environment • Components of the UNIX Programming Environment: • Shell Programming: Programs written in the shell language to process UNIX commands. • Programming Languages: Programs written in languages such as C, C++, Java, assembler, etc. and compiled to run within the UNIX operating system. • Programming Utilities: Tools in the UNIX environment that facilitate programming. Categories include: • Text Editors - create and modify source files in ASCII text. Standard editors are ed and vi. Other editors include INed, ISPF and GNU emacs. • Source Code Control - manages versions of software as programs are being developed. Also keeps programming teams from stepping on each others changes. • Debuggers and Code Checkers - check syntax and compilation errors and facilitate the correction of these errors. • On-line Documentation - manual pages document commands, system calls and other programming environment details. Some UNIX systems have graphical or menu-driven on-line help facilities such as InfoExplorer in IBM’s AIX. Stanberry U2-6

  15. System Management • Definition: The tasks and complex procedures involved in managing and maintaining UNIX systems. Included are the following functions: • Software installation • Defining user accounts • Configuring peripheral devices • Data storage management • Back up and archival of data • Performance and system monitoring • Problem determination and resolution • On many UNIX systems, software packages and tools are added on top of the shell to support these functions. These can be operating system specific such as SMIT and Logical Volume Manager (LVM) that are used with IBM’s AIX, or multi-vendor products such as Veritas Volume Manager, Control-M and ReelBackup. Stanberry U2-7

  16. UNIX PROGRAMMING(JUST FOR THE TRUE TECHIES)

  17. Logging In and Out • Login: All users must provide a user name and a password to log into the system. Below is a dialogue: login: ds59478 ds59478’s Password: (password does not echo) $ • In this case, the login has been demonstrated with my user name of ds59478. The dollar sign prompt represents the shell (default of ksh in AIX). • Logout: The logout command can be invoked with any of the following commands: $ <ctrl-d> (press ctrl key and letter d together) $ exit $ logout login: • The logout commands will remove the user from their current shell. If the current shell is the only session, the result will be a new login prompt as above. Stanberry L1-1

  18. Invoking the Shell and the passwd command • Type the name of the shell to invoke a new shell. The C shell appears as a % at the login prompt by default. Below is a dialogue: $ ksh $ csh % <ctrl-d> $ exit $ logout login: • To create or change the password for the account, use the passwd command. Below is an example: $ passwd Changing password for ds59478 ds59478’s Old password: (no echo to terminal) ds59478’s New password: (no echo to terminal) Enter the new password again: (no echo to terminal) $ Stanberry L1-2

  19. Who Else is on the System? • With the who command, you can see who is on the system and some information about each user: $ who ds59478 pts/1 Dec 26 14:39 a209045 pts/9 Nov 17 10:27 $ • The output of the who command is the username, terminal information, their login date/time and the network address of the login point. • The pipe operator (c1 | c2) takes the output of one command (c1) and uses it as the input of another command (c2). The command wc counts the number of lines, words and bytes in from the input. The -l option on wc gives just the number of lines. Therefore, we can combine the who and wc (with -l option) programs with a pipe to get the number of users on the system: $ who | wc -l 2 $ • We will cover the pipe as well as other operators in more detail in later sections. Stanberry L1-3

  20. Date and Time Functions • With the date command, the shell returns the current date and time as below: $ date Sun Dec 26 15:05:40 EST 1999 $ • Note that there is a time command, but it performs a totally different function. The time command returns the amount of CPU time that it takes to execute a command. As you can see below, the CPU time to execute the command is negligible: $ time real 0m0.00s user 0m0.00s sys 0m0.00s $ Stanberry L1-4

  21. Month and Year Functions • With the cal command, the shell returns the specified month or year: $ cal 1 2000 January 2000 Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ • To get the output for an entire year, leave out the month parameter: $ cal 2000 <Try it to get the output> • Note: Try the cal command for September 1752. • Can you explain the output? Stanberry L1-5

  22. Other Simple UNIX Commands • With the clear command, the user clears the screen: $ clear • The echo command writes to the screen: $ echo Out to Lunch Out to Lunch $ • The banner command writes to the screen in large letters: $ banner Out to Lunch ####### # # # # ##### # # # # # # # # # # # # # # # # # # # # ####### #### # ##### #### # # # # # # # # # # # # # #### # # # # # # #### # # # # # ## # # # # # # # # # # # # ###### # # # # # # # # # # # # # ## # # # # ####### #### # # #### # # $ Stanberry L1-6

  23. Editing Ordinary Files – The vi editor • Although there are a variety of editors available on UNIX systems, it is important to learn vi for the following reasons: • It is a standard editor available on all UNIX systems • It is the default editor for many programs • It is the only editor available in some situations • The vi editor is an extremely robust program that is rich in features, options and customizability. In this course, we will cover only the most basic features of vi to build a working knowledge from which we can edit files. • The vi editor works in two modes: command and edit. Thecommand mode is the default when vi is initiated as follows: $ vi file1 • Once vi is initiated, the following keystrokes will transfer to edit mode: • i – inserts text at the cursor • I – inserts text at the start of the line • a – appends text after the cursor • A – appends text after the end of the line Stanberry L1-7

  24. Command Mode – The vi editor • To transfer from editing mode to the command mode, use the escape (<esc>) key. Once in command mode, keys control navigation as follows: • h,b,0  left, beginning of word, beginning of line • j,G  down, end of document • k,1G  up, beginning of document • l,w,$  right, end of word, end of line • Text editing can be done with the following commands: • r – replace letter with a new letter • cw – change word (replace word with new word) • dw – delete word • x – delete letter • . – repeat the last command (u will undo the last command) • Commands for the file are preceded with a colon: • :q! – quit out of the file without saving • :x,:wq, ZZ – exit and write (or save) the file • Searches for can be done with the /<text pattern> command: Stanberry L1-8

  25. G 1G w,$ b,0 h l j k Summary – The vi editor • Directional keys • Edit mode • i – inserts text at the cursor • I – inserts text at the start of the line • a – appends text after the cursor • A – appends text after the end of the line • Command mode • <esc> key places you in command mode • :q! – exit without saving • :x, :wq, ZZ – exit and save • To correct mistakes • x – delete letter • dw – delete word Stanberry L1-9

  26. Customizing the Environment – editing ~/.profile • By using the vi editor to edit UNIX files, we can learn how to customize the user environment by editing up the ~/.profile file. Follow these instructions carefully: • Bring up the .profile file in vi: $ vi ~/.profile • Browse the file using the arrow keys. Do not make any changes to the file at this time. • Press the <esc> key to initiate command mode and enter the command to exit without writing to the file: :q! Stanberry L1-10

  27. Lab 2The UNIX File System

  28. UNIX Files • Definition: All data on UNIX systems is structured as files. Ordinary files are simply a collection of bytes with no structure imposed by the operating system. • Ordinary files are determined to be in structures such as flat text, arguments, source code or binary executable by the UNIX programs (including the shell) that read them rather than by the operating system itself. This is different from other operating systems such as MVS or Windows. • All files are considered ordinary except for two types: • Directories are special files that contain pointers to the filenames of the files contained in the directory. Directory files are represented with a special character (/). • Special files are used to represent peripheral devices such as storage devices, printers and terminals. • All files contain a special descriptor called an inode. Each inode contains the following information about the file: • The userid of the owner of the file • The groupid of the group of the file • The size of the file (in blocks) • The permissions of the file • The date and time of the last modification to the file • The link count • All directories contain pointers to the filename and the inode number of each file. Stanberry L2-1

  29. Directory Navigation - cd and pwd commands • To change the current directory, use the cd command: $ cd /home/ds59478 $ • To print the current directory, use the pwd command: $ pwd /home/ds59478 $ • There are different ways to use cd: $ cd .. (takes you to parent directory) $ cd - (takes you to last directory you were in) $ cd ~ (takes you to home directory) $ cd /home/bob (full pathname) $ cd ../bob (relative pathname) $ cd ~bob (bob’s home directory) Stanberry L2-2

  30. File System Hierarchy - the df command • The file system for each UNIX machine is laid out in a hierarchy that begins with the root (/) directory. To display all of the mounted filesystems (and the logical volumes to which they are mapped), use the df command: $ df -k (The -k option displays disk space in kb) • Each UNIX machine has a different file system hierarchy, but below is an example of a typical AIX structure: / /usr /dev /home /sbin /etc /var /tmp /nfs ... /home/user1 /home/user2 ... .profile file1 ... /home/user1/bin file2 prog1 ... Stanberry L2-3

  31. Creating and Removing Directories - mkdir and rmdir • To create a new directory, use the mkdir command: $ pwd /u/ds59478 $ mkdir stuff $ cd stuff $ pwd /u/ds59478/stuff $ • To remove a directory, use the rmdir command: $ cd .. $ pwd /u/ds59478 $ rmdir stuff $ cd stuff _cd[2]: stuff: not found. $ • For the rmdir command to work, these must be true: • The directory must be empty • The directory must not be the current directory. Stanberry L2-4

  32. Creating and Removing Files - echo and rm • There are many ways to create new files. For a simple example, the echo command can be used: $ mkdir stuff $ cd stuff $ echo This is a test > test • In this case, the output of the echo command is redirected to a new file that is called test. The (>) symbol facilitates this redirection. Using this technique, any UNIX program that produces output can create a file. • To remove a file, use the rm command: $ echo This is a test > test $ rm test $ • Note: These are the first destructive commands that we have covered to this point. Unlike other operating systems, UNIX does not ask for confirmation before it removes the file. Also, when you create a file via redirection (>), it will overwrite a file that already exists without asking for confirmation. Stanberry L2-5

  33. A Word on Filenames • In the spirit of UNIX, filenames have very few restrictions on them. There is a limit as to the size of the filename that is usually ridiculously large (AIX is 255 characters). In general, all characters are fair game, but it is important to follow certain guidelines to prevent troubles down the road. Generally, filenames should: • not contain imbedded blanks • generally be restricted to alphanumeric characters • should not include metacharacters (*?></:$![]{}| \`”) • be case sensitive (usually lowercase unless special) • only start with . if meant to be hidden • never begin with a + or - • generally be descriptive of the content • The reasons for these restrictions will become clearer as we learn more about UNIX and using the shell. For now, it is a good idea to remember these guidelines when creating files. Stanberry L2-6

  34. Listing Files and Their Contents - ls, cat and touch • The cat command prints the contents of the file to the screen. Below is an example: $ echo this is a test > test $ cat test this is a test $ • The ls command lists the files and sub-directories in the current working directory: $ ls newdir test $ • To further illustrate this, we can use the touch command which creates files of binary length zero: $ touch file1 file2 file3 file4 $ ls file1 file2 file3 file4 newdir test $ Stanberry L2-7

  35. Long Listing of Files - ls -l • With no other options, the ls command gives you no information about the files it is listing other than the file name. With the -l option, there is much more information provided about each file: $ ls -l total 16 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file1 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file2 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file3 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file4 drwxrwxr-x 2 ds59478 dasd 512 Dec 27 13:12 newdir -rw-rw-r-- 1 ds59478 dasd 15 Dec 28 23:59 test $ • The long listing provides in fields all of the information that is stored in the inode of the file. Below is a summary of each field: (1) The file type and the permission bits (2) The link count (3) The name of the owner of the file (4) The name of the group of the file (5) The size of the file in bytes (6) The date the file was last modified (7) The filename Stanberry L2-8

  36. File Types and Permission Bits • The first bit of the first column on a long listing of a file indicates the file type. It is as follows: • blank (-) indicates an ordinary file • d indicates a directory file • other letters (such as b,c or l) indicate a special file • The next 9 bits indicate the permissions of the file. They consist of a read, write and execute bit for the owner of the file, the group of the file and all others. Each bit is either represented by the r,w or x (if on) or the blank (-) if off. Below is a summary of their meanings: • For an ordinary file: • r = permission to look at the contents of the file • w = permission to change the contents of the file • x = permission to execute file as a command • For a directory file: • r = permission to list the files in the directory • w = permission to create and remove files in directory • x = permission to make it the current directory Stanberry L2-9

  37. Octal Notation and Permission Bits • The permission bits are often summarized by looking at each group of three bits as an octal number. In octal notation, each bit holds a place as follows: • r = 2**2 = 4 + • w = 2**1 = 2 + • x = 2**0 = 1 = octal permission • Each bit is either on or off, which represents a number for the group of three. There are a total of three numbers with one representing owner, group and other. • Therefore, we can read the permissions as 664 for each file and 775 for the directory for the example below: $ ls -l total 16 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file1 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file2 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file3 -rw-rw-r-- 1 ds59478 dasd 0 Dec 29 00:05 file4 drwxrwxr-x 2 ds59478 dasd 512 Dec 27 13:12 newdir -rw-rw-r-- 1 ds59478 dasd 15 Dec 28 23:59 test $ Stanberry L2-10

  38. Changing Permission Bits - chmod • To change the permission bits, you can use the chmod command with the octal notation below is an example: $ ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 27 13:12 newdir -rw-rw-r-- 1 ds59478 dasd 15 Dec 28 23:59 test $ chmod 775 test $ ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 27 13:12 newdir -rwxrwxr-x 1 ds59478 dasd 15 Dec 28 23:59 test $ • By changing the permission bits to 775, the execute bit has been turned on for the owner, group and others. • Permission bits can also be changed with symbolic notation. By using u for owner, g for group, o for other and a or blank for all, any of the read, write or execute bits can be added using a (+) sign, reduced using a (-) sign or set explicitly using the (=) sign. Below are examples: $ chmod o+w (permits write for others) $ chmod go-r (restricts read from group and others) $ chmod +x (adds execute for all) Stanberry L2-11

  39. Linking Files - ln command • The second column of the long listing is the count of links to a file. Use the ln command to add a link. This command creates a file that has the same inode number as an existing file. The link file is the same physical file as the original file that can be accessed by a different name. Below is a dialogue to demonstrate: $ echo this is a test > test $ ls -l total 8 -rw-rw-r-- 1 ds59478 dasd 15 Dec 29 16:52 test $ ln test link_to_test $ ls -l total 16 -rw-rw-r-- 2 ds59478 dasd 15 Dec 29 16:52 link_to_test -rw-rw-r-- 2 ds59478 dasd 15 Dec 29 16:52 test $ rm test $ ls -l total 8 -rw-rw-r-- 1 ds59478 dasd 15 Dec 29 16:52 link_to_test $ Stanberry L2-12

  40. Changing the Owner and Group - chown and chgrp • The third and fourth columns of the long listing are the owner and the group with permissions to the file. The owner can be changed with the chown command: # ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 29 17:05 newdir -rwxrwxr-x 1 dasd dasd 15 Dec 28 23:59 test # chown ds59478 test # ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 29 17:05 newdir -rwxrwxr-x 1 ds59478 dasd 15 Dec 28 23:59 test # • The group can be changed with the chgrp command: # ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 29 17:05 newdir -rwxrwxr-x 1 ds59478 dasd 15 Dec 28 23:59 test # chgrp sys test # ls -l total 16 drwxrwxr-x 2 ds59478 dasd 512 Dec 29 17:05 newdir -rwxrwxr-x 1 ds59478 sys 15 Dec 28 23:59 test # Stanberry L2-13

  41. Default Permissions - umask • The umask specifies what permissions will be set by default on new files and directories. It is represented as an octal number from which the default file (666) and directory (777) permissions are subtracted. For example: • umask 000 = 666 for files and 777 for directories • umask 002 = 664 for files and 775 for directories • umask 022 = 644 for files and 755 for directories • In AIX, a default umask of 022 is automatically set up. This can be changed by the root user of the system. • The default umask can be set for each user in their user profile. User profiles will override the default. • On many UNIX systems, the umask that users have specified in their profile has become the subject of security audits. Stanberry L2-14

  42. Hidden Files – ls –al command • On UNIX file systems, filenames that begin with a dot (.) are hidden. In order to see the hidden files on a system, the –a option must be used with the ls command. When combined with the long listing, the command is ls –al as seen below: $ cd ~ $ ls -al total 88 drwxr-xr-x 6 ds59478 dasd 512 Dec 29 16:52 . drwxr-sr-x 128 root tla 2560 Nov 10 11:56 .. -rw------- 1 ds59478 dasd 400 Aug 5 17:06 .Xauthority -rw-r----- 1 ds59478 dasd 185 Nov 11 1998 .Xdefaults -rw-r----- 1 ds59478 dasd 74 May 21 1998 .exrc -rw-r----- 1 ds59478 dasd 1144 Nov 11 1998 .profile -rw------- 1 ds59478 dasd 2880 Dec 31 03:20 .sh_history drwxr-xr-x 2 ds59478 dasd 512 Mar 8 1999 .ssh drwxr-sr-x 2 ds59478 dasd 512 May 21 1998 bin drwxr-sr-x 2 ds59478 dasd 512 May 21 1998 etc drwxrwxr-x 3 ds59478 dasd 512 Dec 29 04:32 stuff $ • The . file represents the current directory and the .. file represents the parent directory in the file system. • The ~/.profile file contains the user profile. Statements executed at login time set the user’s preferences. Stanberry L2-15

  43. Lab 3UNIX Commands

  44. General Format of UNIX Commands – man entries • Because of the way in which UNIX evolved, there are very few standards in the command syntax. In general, command formats follow a convention: $ command –options argument1 argument2 ... etc • However, this is not always true. Also, the same option may mean totally different things to different commands. • As a result, most commands have a manual entry. The manual entry is a file that is stored in a particular directory (that is customizable on every system) that contains documentation for the command. To view this documentation for a particular command, you can use the man command. The following example looks at the manual entry for the ls command: $ man ls • Manual entries usually include the proper syntax for the command, and explain all of the options and arguments that the command expects. It may also include special notes and/or about warnings about certain uses of the command. Stanberry L3-1

  45. Copying Files – cp command • To create a copy of a file, you can use the cp command. The format is as follows: $ cp source_file target_file • The above command creates a copy of the file in the current working directory. By using pathnames, cp can create a copy of a file in a different directory as follows: $ cp source_file ~/bin/target_file • The above example creates a copy of the file called target_file in my home directory. If you want to create a file of the same name, but in a different directory, you can use the dot (.) instead of a target file name: $ cp same_filename ~/bin/. • Note: The cp command leaves the source file alone, but it still can potentially be destructive because it will overwrite any target file that already exists. To prevent possible data loss, you can use the –i option of cp that warns the user if the target file exists. Below is a sample: $ cp –i source_file target_file Stanberry L3-2

  46. Moving (or Renaming) Files – mv command • The cp command creates a new file that is an independent copy of a file without changing the source file. By contrast, the mv command logically makes a copy of the file and deletes the original source file. Internally, it does this by simply changing the filename (or renaming the file) in the inode. The format is as follows: $ mv old_file new_file • By using pathnames, mv can place a file in a different directory as follows: $ mv old_file ~/bin/new_file • The mv command can also move the file to a different directory using the same name by using the dot (.) instead of a target file name: $ mv same_filename ~/bin/. • Note: The mv command also will overwrite any target file that already exists. Therefore, you can also use the –i option of mv that warns the user if the target file exists. Below is a sample: $ mv –i old_file new_file Stanberry L3-3

  47. Displaying Files - pg and more commands • Thus far, we have seen how to view the contents of an ordinary file using the cat command and the vi editor. There are some other commands that better facilitate looking at files (particularly large files). • The pg command displays a file a page at a time. The file can be scrolled using the enter key or searched using the /<search pattern> text. On AIX systems, there is a special file called /etc/filesystems that stores information on all of the filesystems. This is generally a large file, so we can use it as an example: $ pg /etc/filesystems (type <ctrl-c> to cancel) • The more command is similar to the pg command. The main difference is the fact that the file can be scrolled either a line at a time with the enter key, or a page at a time with the space bar. Below is an example: $ more /etc/filesystems (type <ctrl-c> to cancel) • Often, the more command is combined with another command using the pipe to control the output: $ cat /etc/filesystems | more Stanberry L3-4

  48. Displaying Files – head and tail commands • There are a few other commands that can be useful when displaying large text files. The head command, by default, displays the first ten lines of a file. For an example, we can use the /etc/filesystems file again: $ head /etc/filesystems • The head command is useful for glancing at the format of a large file without having to go into it. • The tail command displays the last ten lines of a file by default: $ tail /etc/filesystems • There are not too many cases where the tail command by itself is useful. However, some files are continuously updated. The –f option of the tail command will update the screen as output is added to the file. An example of this can be found with the system log (in AIX it is under /var/sysadm/messages). The system log is constantly updated, and the command below allows the log to be followed: $ tail –f /var/adm/messages (type <ctrl-c> to cancel) Stanberry L3-5

  49. Searching for Text Patterns – grep command • The grep command is very common utility used for searching out patterns in text files. There are many options and uses of the grep command that we will expand upon throughout the class. For now, we can look at some simple examples. • Let’s say that I have a file called birthdays in which I store all of the names and dates of the birthdays that are important to me. If I want to find a person’s birthday without looking at the entire file, I can use the grep command as follows: $ grep India birthdays India 12/10 $ • The –i option of the grep command says to ignore case: $ grep –i india birthdays India 12/10 $ • To look at all of the January birthdays: $ grep 01 birthdays Matt 01/28 Rob 01/27 Stanberry L3-6

  50. More Searching for Text Patterns – egrep command • The egrep command allows for multiple patterns to be searched. These patterns are separated by an or (|) symbol in the text string that is represented in single quotes (‘’). Below is an example with the –i option: $ egrep –i ‘rob|india’ birthdays India 12/10 Rob 01/27 $ • One of the more useful ways in which to use grep and egrep is to redirect the input of another command using the pipe (|) symbol. Therefore, commands which generate a great deal of output can be filtered for the relevant data. Below is an example with the df command. The df command produces output for every filesystem (with the –k option specifying the size in kilobytes). If you are only interested in the home filesystems, then the command below will filter the output (in this case only 1 home filesystem): $ df –k | grep home /dev/Less 1228800 1137740 8% 809 1% /home/ess $ Stanberry L3-7

More Related