Introduction to UNIX                                            UNIX Power User Training                                  ...
Introduction to UNIX                                    What is an Operating System?                              • Softwa...
Introduction to UNIX                                  Different Types of Operating Systems                              • ...
Introduction to UNIX                                        UNIX Operating System                              • UNIX is a...
Introduction to UNIX                                  Components of UNIX: perspective 2                                   ...
Introduction to UNIX                                      What Can UNIX Do for You?                             • User’s S...
Introduction to UNIX                                 Command Source and Destination                                       ...
Introduction to UNIX                                                 Correcting Mistakes                                  ...
Introduction to UNIX                                            The UNIX File System                                      ...
Introduction to UNIX                              Sample UNIX File Structure (NIU, CS)                                UNIX...
Introduction to UNIX                                                      File Types                         - Text     - ...
Introduction to UNIX                                       Relative Pathnames for file3                                   ...
Introduction to UNIX                             Display Current Directory’s Full Pathname                              • ...
Introduction to UNIX                                                  Long List Option                                    ...
Introduction to UNIX                                       List Contents of a Specific Directory                          ...
Introduction to UNIX                                            The mkdir Command                                         ...
Introduction to UNIX                                                 Directory Names                              • Use th...
Introduction to UNIX                               Example: Create a Directory Creation                                   ...
Introduction to UNIX                                                 Changing Directory                                   ...
Introduction to UNIX                                  Operations Common to Directories and                                ...
Introduction to UNIX                                              Copying Files                             • “source-file...
Introduction to UNIX                                                Moving Files                              • Syntax: mv...
Introduction to UNIX                                            Rename Directories                              • To chang...
Introduction to UNIX                                         Removing/Deleting Files                             • You sho...
Introduction to UNIX                                     Recap: Common Operations on Files                                ...
Introduction to UNIX                                                  Finding Files                                Example...
Introduction to UNIX                                            Unix file organization                             • Compu...
Introduction to UNIX                                                        inode                              • Index (or...
Introduction to UNIX                                  inode Contents: where is the file data ?                            ...
Introduction to UNIX                                              Directory representation                             Dir...
Introduction to UNIX                                     Example: user view vs. system view                               ...
Introduction to UNIX                                             Howto share Files ?                              • Duplic...
Introduction to UNIX                                     Create Common Login Name                              • All team ...
Introduction to UNIX                                            Create Common Group                              • This so...
Introduction to UNIX                                                          Hard Link                             Advant...
Introduction to UNIX                                            The ln Command                                            ...
Introduction to UNIX                                                         Hard Link                                    ...
Introduction to UNIX                                                   Symbolic Link                              • A hard...
Introduction to UNIX                                             User’s Disk Quota                                 • A dis...
Introduction to UNIX                                  Operations Unique to Regular Files                                  ...
Introduction to UNIX                                          Creating A File With cat                                 Exa...
Introduction to UNIX                                  Displaying Contents of Text Files                                   ...
Introduction to UNIX                                    Viewing Contents of Text Files                              • The ...
Introduction to UNIX                                    Viewing Contents of Text Files                              • The ...
Introduction to UNIX                               Other Operations Unique to Text Files                                  ...
Introduction to UNIX                                     Combining Contents of Files                              • Method...
Introduction to UNIX                                              The cut Command                                         ...
Introduction to UNIX                                            Comparing Files: diff                              • The c...
Introduction to UNIX                                              The wc Command                                          ...
Introduction to UNIX                                      Viewing a Compressed File                              • The com...
Introduction to UNIX                                                  Sorting Files                             • To sort ...
Introduction to UNIX                                             Fields Within A Line                                     ...
Introduction to UNIX                                        Global and Local Options                                      ...
Introduction to UNIX                                            The unique Command                                        ...
Introduction to UNIX                                             Decrypting Files                              • The proce...
Introduction to UNIX                                                Editor Concepts                          • Editing – c...
Introduction to UNIX                                                     Vi Buffers                             • Work Buf...
Introduction to UNIX                                                      Vi Mode                              • In vi, to...
Introduction to UNIX                                  Commonly Used File Extensions                               Extensio...
Introduction to UNIX                                    Recovering Text After a Crash                              • If th...
Introduction to UNIX                                             Access Permissions                                       ...
Introduction to UNIX                                                     Users                                            ...
Introduction to UNIX                                             Security Levels                              • There are ...
Introduction to UNIX                                        Access Permission Code                              • The prot...
Introduction to UNIX                                                      Access Types                             Access ...
Introduction to UNIX                                            Changing Permissions                                      ...
Introduction to UNIX                                Changing Permissions: Symbolic Mode                                   ...
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Unix  introduction
Upcoming SlideShare
Loading in...5
×

Unix introduction

906

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
906
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unix introduction

  1. 1. Introduction to UNIX UNIX Power User Training Unix Courseware Version 1.2 1 System Consultant Introduction to UNIX 2Copyright Department of Computer Science,Northern Illinois University, 2004 01-1
  2. 2. Introduction to UNIX What is an Operating System? • Software that manages (allocates and de-allocates) system resources in an efficient and secure manner • System resources consist of hardware (e.g. terminals, printers, storage device, etc) and software (e.g. application programs, language libraries, etc.) 3 System Resources System Resources Hardware Software Components System Application Software Software 4Copyright Department of Computer Science,Northern Illinois University, 2004 01-2
  3. 3. Introduction to UNIX Different Types of Operating Systems • Single-user, single-process operating systems: allow only one user at a time to use the computer system. The user can execute/run only one process at a time. – Examples: DOS, Windows 3.1 • Single-user, multi-process operating systems: allow a single user to use the computer system; however, the user can run multiple processes at the same time. – Example: OS/2 5 Different Types of Operating Systems • Multi-user, multi-process operating systems: allow multiple users to use the computer system simultaneously. Each user can run multiple processes at the same time. – Examples: UNIX, Windows XP 6Copyright Department of Computer Science,Northern Illinois University, 2004 01-3
  4. 4. Introduction to UNIX UNIX Operating System • UNIX is a multi-user, multi-processing, portable(?) operating system. • UNIX is designed to facilitate programming, text processing and communication 7 Components of UNIX: perspective 1 8Copyright Department of Computer Science,Northern Illinois University, 2004 01-4
  5. 5. Introduction to UNIX Components of UNIX: perspective 2 9 History of UNIX • Invented by Ken Thompson at AT&T in 1969 • First version written in assembly language – single user system, no network capability • Thompson, Dennis Ritchie, Brian Kernighan – rewrote Unix in C: processor/architecture independent • Unix evolution: I. Bell Labs, USL, Novell, SCO II. AIX, Ultrix, Irix, Solaris, … III. BSD, FreeBSD, Mach, OS X IV. Linux, Redhat, Suse, … 10Copyright Department of Computer Science,Northern Illinois University, 2004 01-5
  6. 6. Introduction to UNIX What Can UNIX Do for You? • User’s Support Tools – Text processing (vi, sed, awk) – Filing system – E-mail and networking – Electronic databases • Programmer’s Support Tools – Programming languages & compilers (C, C++) – Shell scripts – Programming Workbench • Source Code Control System (SCCS) • Revision Control System (RCS) 11 Some Standard UNIX Shells 12Copyright Department of Computer Science,Northern Illinois University, 2004 01-6
  7. 7. Introduction to UNIX Command Source and Destination 13 Command Line Structure mp% command [-options] [arguments] Arguments can be: Command Command Command modifier; 1. More information prompt name usually one character 2. Object identifiers preceded by + or - sign 3. Names of files Notes: •UNIX is case sensitive!!!! •Example: the command “ls –l” is not the same as “LS –L” • Must be a space between the command, options and arguments • No space between the plus or minus sign and the option letter • Option letters must be typed exactly as they are indicated, uppercase or lowercase •Fields enclosed in [ ] are optional •Must press [Return] after you have completed entry of a command 14Copyright Department of Computer Science,Northern Illinois University, 2004 01-7
  8. 8. Introduction to UNIX Correcting Mistakes Note: UNIX is case sensitive (use lowercase) Key Pressed Result Backspace, Ctrl-h Back up & erase last character Ctrl-c Terminates the current command Ctrl-r Redraws the current command line Ctrl-s Stops scrolling of output on screen (Ctrl-q to resume/start scrolling) Ctrl-w Erases a word on command line Ctrl-u Erases/deletes entire command line 15 Command Line Structure • How do the results of the following commands differ? • According to the synopsis of the “sort” command, what parts of the “sort” command are required? sort list Command sort –f list argument sort –o sorted list Command Command Option name option argument 16Copyright Department of Computer Science,Northern Illinois University, 2004 01-8
  9. 9. Introduction to UNIX The UNIX File System 17 A Directory Hierarchy 18Copyright Department of Computer Science,Northern Illinois University, 2004 01-9
  10. 10. Introduction to UNIX Sample UNIX File Structure (NIU, CS) UNIX supports “tree-like” directory structure Root Directory Root subdirectories dev etc home usr tty null skel mp ux bin local ucb Home Directory z036473 Files in the z036473 csci330 .cshrc .logout z036473 directory subdirectory 19 Directory Types • Root Directory: / – The first directory in any UNIX file structure – Always begin with the forward slash (/) • Home Directory: $HOME or ~ • Created by system administrator • This is where you are when you first log in! • Under $HOME, you may create your own directory structure • Type: cd [Return] takes you $HOME • Current Working Directory: . – The Directory you are currently working in – Also called Current Working Directory (cwd) • Parent Directory: .. – The directory immediately above your current working directory. 20Copyright Department of Computer Science,Northern Illinois University, 2004 01-10
  11. 11. Introduction to UNIX File Types - Text - Root (/) - Binary - Home (~) - Working (.) - Parent (..) 21 Paths and Pathnames Two ways of locating a file or a directory: • By Using Absolute Pathname – Full pathname – Traces a path from root to a file or a directory – Always begins with the root (/) directory! – Example: /home/ux/krush/unix/assignments/assign1.sp04 • By Using Relative Pathname – Traces a path from the ‘cwd’ to a file or a directory – No initial forward slash (/) – Two dots (..) goes up one level on file structure – Dot (.) points to current working directory (cwd) – Example: unix/assignments/assign1.sp04 22Copyright Department of Computer Science,Northern Illinois University, 2004 01-11
  12. 12. Introduction to UNIX Relative Pathnames for file3 Absolute Pathname: /usr/staff/joan/file3 23 Directory Operations 24Copyright Department of Computer Science,Northern Illinois University, 2004 01-12
  13. 13. Introduction to UNIX Display Current Directory’s Full Pathname • To determine the full pathname of the current working directory, use the command named “pwd” • pwd stands for print working directory Example: To display the full pathname of the current working directory ux% pwd /home/ux/krush/unix 25 The ls Command 26Copyright Department of Computer Science,Northern Illinois University, 2004 01-13
  14. 14. Introduction to UNIX Long List Option 27 List ALL Contents of Current Directory ux% ls -la List contents of the current . Points to cwd, total 126 directory in long format .. Points to parent’s drwxr-xr-x 13 krush csci 1024 Apr 26 15:49 . dir. drwxr-xr-x 15 root root 512 Apr 24 15:18 .. File names -rwx------ 1 krush csci 1120 Apr 12 13:11 .cshrc begins with -rwxr--r-- 1 krush csci 885 Dec 2 13:07 .login a dot (.) are -rw-r--r-- 1 krush csci 141 Mar 14 13:42 .logout hidden files --rwx------ 1 krush csci 436 Apr 12 11:59 .profile drwx------ 7 krush csci 512 May 17 14:11 330 Directories drwx------ 3 krush csci 512 Mar 19 13:31 467 drwx------ 2 krush csci 512 Mar 31 10:16 Data -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron Regular text file 28Copyright Department of Computer Science,Northern Illinois University, 2004 01-14
  15. 15. Introduction to UNIX List Contents of a Specific Directory Listing contents of a subdirectory named ux% ls -l unix/grades “unix/grades” total 10 -rwxr-xr-x 3 krush csci 72 Jan 19 19:12 330assign-graderun -rwxr-xr-x 1 krush csci 70 Jan 19 19:13 330exam-graderun -rwxr-xr-x 2 krush csci 70 Jan 19 19:12 330quiz-graderun -r-x------ 1 krush csci 468 Feb 1 11:55 test-330grade -r-x------ 1 krush csci 664 Feb 1 11:55 test-330grade,v 29 File Name Expansion & Wildcards Allows you to select files that satisfy a particular name pattern (wildcards) Character Description Example * Match zero or more char. ls *.c ? Match any single character ls conf.? [list] Match any single character in list ls conf.[co] [lower-upper] Match any character in range ls lib-id[3-7].o str{str1,str2,…} Expand str with contents of { } ls c*.{700,300} 30Copyright Department of Computer Science,Northern Illinois University, 2004 01-15
  16. 16. Introduction to UNIX The mkdir Command 31 Creating a New Directory • To create a directory, use the command named “mkdir” Example: To create two new directories called “csci330” and “test-data” ux% mkdir csci330 test-data ux% mkdir /home/ux/krush/unix/demo must already exist 32Copyright Department of Computer Science,Northern Illinois University, 2004 01-16
  17. 17. Introduction to UNIX Directory Names • Use the following characters: – Uppercase letters (A-Z) – Lowercase letters (a-z) – Numbers (0-9) – Underscore ( _ ) – Period/dot ( . ) 33 Directory Names • When naming a directory, avoid the following characters: & * | [] {} $ <> () # ? / “ ‘ ; ^ ! ~ Space Tab 34Copyright Department of Computer Science,Northern Illinois University, 2004 01-17
  18. 18. Introduction to UNIX Example: Create a Directory Creation dev etc home usr tty null skel mp ux bin local ucb z036473 You are here csci330 .cshrc .logout Temp Data Create a directory called Data under csci330 a) Using Absolute Pathname: mkdir /home/mp/z036473/csci330/Data b) Using Relative Pathname: 35 mkdir csci330/Data The cd Command 36Copyright Department of Computer Science,Northern Illinois University, 2004 01-18
  19. 19. Introduction to UNIX Changing Directory dev etc home usr tty null skel mp ux bin local ucb z036473 csci330 .cshrc .logout Temp You are here Data In the Data directory, go to $HOME directory a) Using Absolute Pathname: cd /home/mp/z036473 b) Using Relative Pathname: 37 cd $home cd ../.. cd cd ~ cd ~z036473 Remove Directories • To remove an empty directory – a directory that does not contain user-created files, use the command named “rmdir” Example: To remove a directory called “test”, which does not contain user-created files. ux% rmdir test • To remove a non-empty directory, use the command named “rm –r” Example: To remove a non-empty directory called “old- data” ux% rm –r old-data 38Copyright Department of Computer Science,Northern Illinois University, 2004 01-19
  20. 20. Introduction to UNIX Operations Common to Directories and Regular Files 39 Copying Files • To copy a file, use the command named “cp” • Syntax: cp source-file new-file • Commonly used options: -i if “new-file” exists, the command cp prompts for confirmation before overwriting -p preserve permissions and modification times -r recursively copy files and subdirectories 40Copyright Department of Computer Science,Northern Illinois University, 2004 01-20
  21. 21. Introduction to UNIX Copying Files • “source-file” must have read permission. • The directory that contains “source-file” must have execute permission. • The directory that contains “new-file” must have write and execute permissions. • Note that if “new-file” exists, you do not need the write permission to the directory that contains it, but you must have the write permission to “new- file”. 41 Moving Files • To move files from one directory to another directory, or to re-name a file, use the command named “mv”. • The directory that contains the source file and the destination directory must have write and execute access permissions. 42Copyright Department of Computer Science,Northern Illinois University, 2004 01-21
  22. 22. Introduction to UNIX Moving Files • Syntax: mv source-file destination-file • If the destination file exists, “mv” will not overwrite exiting file. Example: Move “assign1.txt” a different directory and rename it to “assign1.save” ux% mv assign1.txt $HOME/archive/assign1.save ux% mv assign1.txt $HOME/archive 43 Moving a File 44Copyright Department of Computer Science,Northern Illinois University, 2004 01-22
  23. 23. Introduction to UNIX Rename Directories • To change the name of an existing directory, use the command named “mv” Example: To rename the file called “unix” to “csci330” ux% mv unix csci330 • For the above example, what happens if “csci330” already exists in the current directory and it is the name of a directory? 45 The mv Command 46Copyright Department of Computer Science,Northern Illinois University, 2004 01-23
  24. 24. Introduction to UNIX Removing/Deleting Files • You should remove un-needed files to free up disk space. • To remove/delete files, use the command named “rm”. • Syntax: rm file-list • Commonly used options: -f force remove regardless of permissions for “file-list” -i prompt for confirmation before removing -r removes everything under the indicated directory 47 Removing/Deleting Files • If “file-list” contains pathname, the directory components of the pathname must have execute permission. • The last directory that contains the file to be deleted must have execute and write permissions. Example: Remove the file named “old-assign” ux% rm unix/assign/old-assign 48Copyright Department of Computer Science,Northern Illinois University, 2004 01-24
  25. 25. Introduction to UNIX Recap: Common Operations on Files 49 Finding Files • The command named “find” can be used to locate a file or a directory. • Syntax: find pathname-list expression • “find” recursively descends through pathname-list and applies expression to every file. • For syntax of expression, see Course Notes pp. 13- 9 50Copyright Department of Computer Science,Northern Illinois University, 2004 01-25
  26. 26. Introduction to UNIX Finding Files Example 1: Find all files, in your directory hierarchy, that have a name ending with “.bak”. ux% find $home –name “*.bak” –print Example 2: Find all files, in your directory hierarchy, that were modified yesterday. ux% find $home –mtime –1 -print 51 The “ln” command • Allows file to listed in multiple directories • 2 types: – Hard link – Symbolic link • First: understand Unix file storage and organization 52Copyright Department of Computer Science,Northern Illinois University, 2004 01-26
  27. 27. Introduction to UNIX Unix file organization • Computer has one or more physical hard drives • Hard drive is divided into partitions • Partition holds file system – File system is set of data blocks – Data blocks contain • general information • actual file data • directory information 53 Blocks in a file system 54Copyright Department of Computer Science,Northern Illinois University, 2004 01-27
  28. 28. Introduction to UNIX inode • Index (or information) node: one inode per file • Each inode has unique number • contents: – File type, access permissions, link count – UID, GID – Date and time of the file’s last • Data access (read and execute) • Data modification (written) • I-node modification (permission change) – Data blocks assigned to the file 55 Inodes in a filesystem 56Copyright Department of Computer Science,Northern Illinois University, 2004 01-28
  29. 29. Introduction to UNIX inode Contents: where is the file data ? Inode may store: – 10 addresses of data blocks that belong to file – 1 address of a block that contains data block addresses – 1 address of a block that contains addresses of blocks that contain data block addresses – 1 address of a block that contains addresses of blocks that contain addresses of blocks that contain data block addresses 57 I-node Structure I-node blocks blocks Access, Links, and other information 1 2 blocks blocks . . indirect 9 block 10 11 double indirect block blocks 12 blocks 13 blocks triple indirect 58 blockCopyright Department of Computer Science,Northern Illinois University, 2004 01-29
  30. 30. Introduction to UNIX Directory representation Directory is a file: – Has inode like regular file, but different file type – Data blocks of directory contains simple table: Name Inode number 59 Example structure . I-node list 2763 Contents of dir1 2764 1076 . 2765 2083 . .. myfile 2764 . Data blocks on disk 60Copyright Department of Computer Science,Northern Illinois University, 2004 01-30
  31. 31. Introduction to UNIX Example: user view vs. system view 61 Output: ls -li ux% ls -li crontab.cron 118282 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron I-node 62Copyright Department of Computer Science,Northern Illinois University, 2004 01-31
  32. 32. Introduction to UNIX Howto share Files ? • Duplicate shared files. • Create common login name for members of the team. • Set appropriate access permissions on shared files. • Create common group for members of the team. • Share files via links. 63 Duplicate Shared Files • Make copies of shared files and give them to all members of the team. • The simplest way to share files. • Does not work well if members of the team work on the files at the same time. 64Copyright Department of Computer Science,Northern Illinois University, 2004 01-32
  33. 33. Introduction to UNIX Create Common Login Name • All team members use a common login that gives them access to shared files. • This is a simple solution that works well if the size of the team is small and stable. • A disadvantage is team members have to use a separate account, not their regular account. 65 Set Appropriate Access Permissions • Team members put all shared files in one place and set access permissions so all team members can access them. • This scheme works well if all team members are in the same group (group permissions can be used). • A disadvantage is if the group used has other users in it, they will also have access to the files. 66Copyright Department of Computer Science,Northern Illinois University, 2004 01-33
  34. 34. Introduction to UNIX Create Common Group • This solution is similar to setting appropriate permissions except a new group is created that contains just the members of the team. • All team members have individual logins. • This is an effective solution, especially if it is with version control. 67 Linking Files • To share a single file with multiple users, a link can be used. • A link is: – A reference to a file stored elsewhere on the system. – A way to establish a connection to a file to be shared. • Two types: – Hard link – Symbolic link (a.k.a. “soft link”) 68Copyright Department of Computer Science,Northern Illinois University, 2004 01-34
  35. 35. Introduction to UNIX Hard Link Advantages Disadvantages Allow access to original file name via the file name or the I-node Cannot link to a file in a different number file system The original file continues to exist as long as at least one directory contains its I-node Prevents owner from truly deleting it, and it counts against his/her disk Checks for the existence of the quota original file 69 Hard Link home Syntax: ln shared-file link-name z036473 From dir3, link to the file ‘aa’ in dir1 name it ‘bb’: dir1 dir2 % ln /home/z036473/dir1/aa bb aa dir3 bb 70Copyright Department of Computer Science,Northern Illinois University, 2004 01-35
  36. 36. Introduction to UNIX The ln Command 71 A Hard Link 72Copyright Department of Computer Science,Northern Illinois University, 2004 01-36
  37. 37. Introduction to UNIX Hard Link Contents of dir1 . home 1076 . 2406 2083 2407 z036473 .. 2408 aa 2407 dir1 dir2 . aa Contents of dir3 . dir3 1070 bb . 2050 .. bb 2407 73 Symbolic Link Advantages Disadvantages Allow access to original file name Created without checking the existence of the shared file Can use either relative or absolute path to access the original file Cannot access the shared file if its path has restricted permissions Can cross partition and drives Can be circular linked to another Allows the creation of a link to a symbolic linked file directory 74Copyright Department of Computer Science,Northern Illinois University, 2004 01-37
  38. 38. Introduction to UNIX Symbolic Link • A hard link may not be created for a file on a different file system • Use symbolic link • The linked files do not share the same I-node number Syntax: ln –s shared-file link-name Also called source-file Also called target-file 75 Symbolic Links to Different File Systems 76Copyright Department of Computer Science,Northern Illinois University, 2004 01-38
  39. 39. Introduction to UNIX User’s Disk Quota • A disk quota is set for each user account • The command: quota –v displays the user’s disk usage and limits • 2 kinds of limits: – Soft limit: ex. 3MB • Maybe exceeded for one week • System will nag – Hard limit: ex. 4MB • Cannot be exceeded 77 Operations Common to Directories and Regular Files 78Copyright Department of Computer Science,Northern Illinois University, 2004 01-39
  40. 40. Introduction to UNIX Operations Unique to Regular Files Display Create Edit Print Others Contents 79 Creating New Files Create Regular Files Redirect cat vi pico Command Output See Text Editors See the C shell Section 03 Section 06 80Copyright Department of Computer Science,Northern Illinois University, 2004 01-40
  41. 41. Introduction to UNIX Creating A File With cat Example: mp% cat > myfile This is line 1 of input Line 2 of input ^d mp% 81 Editing Text Files Editing Text Files vi pico sed awk See Text Editors See course notes See course notes Section 03 section 08 section 10 82Copyright Department of Computer Science,Northern Illinois University, 2004 01-41
  42. 42. Introduction to UNIX Displaying Contents of Text Files Display Text File contents cat more less pg head tail 83 The cat Command 84Copyright Department of Computer Science,Northern Illinois University, 2004 01-42
  43. 43. Introduction to UNIX Viewing Contents of Text Files • The command named “cat” can be used to display/concatenate one or more files, displaying the output all at once. Example: Display the contents of a file called “assign1.txt”. ux% cat assign1.txt 85 Viewing Contents of Text Files • The commands named “more” and “less” can be used to display the contents of one or more files one page at a time. Space bar – to advance to next page b – to go back a page Enter Key – to advance to next line Example: Display the contents of a file called “assign1.txt” one page at a time. ux% more assign1.txt 86Copyright Department of Computer Science,Northern Illinois University, 2004 01-43
  44. 44. Introduction to UNIX Viewing Contents of Text Files • The command named “head” can be used to display the beginning portion of indicated file(s); the default head size is 10 lines. Example: Display the first 20 lines of a file called “assign1.txt”. ux% head –20 assign1.txt 87 Viewing Contents of Text Files • The command named “tail” can be used to display the ending portion of indicated file(s); the default tail size is 10 lines. Example: Display the last 10 lines of a file called “assign1.txt”. ux% tail assign1.txt ux% tail –10 assign1.txt 88Copyright Department of Computer Science,Northern Illinois University, 2004 01-44
  45. 45. Introduction to UNIX Other Operations Unique to Text Files Other File Operations Combine Extract Compare File Compress sort contents contents contents size contents Unique Encrypt/ lines decrypt 89 Combining Contents of Files • Method 1: To vertically concatenate the contents of two or more files, use the command named “cat” with output redirection (>). • Syntax: cat file-1 file-2 file-3 > all-file • “all-file” will contain the combined contents of file-1, file-2, and file-3 in top-down (vertical) fashion • See demo 90Copyright Department of Computer Science,Northern Illinois University, 2004 01-45
  46. 46. Introduction to UNIX Combining Contents of Files • Method 2: To horizontally concatenate contents (columns/fields) of two or more files, use the command named “paste”. • Syntax: paste file-1 file-2 • See demo 91 Extracting Contents of Files • To extract one or more fields in a file, use the command named “cut”. Example: Extract the month and year fields from the output of the “date” command. ux% date Mon Feb 2 20:37:38 CST 2004 ux% date | cut -d -f2,6 Feb CST 92Copyright Department of Computer Science,Northern Illinois University, 2004 01-46
  47. 47. Introduction to UNIX The cut Command 93 Comparing Files: comm • The command named “comm” can be used to compare lines that are common in two sorted files. • Syntax: comm [options] file-1 file-2 • The output contains three columns: – Column1 contains lines unique to file-1 – Column 2 contains lines unique to file-2 – Column 3 contains lines common to both files • See Demo 94Copyright Department of Computer Science,Northern Illinois University, 2004 01-47
  48. 48. Introduction to UNIX Comparing Files: diff • The command “diff” can be used to compare two files line by line. • Syntax: diff [options] file-1 file-2 • If file-1 and file-2 are the same, no output is produced. • If file-1 and file-2 are not the same, diff reports a series of commands that can be used to convert the first file to the second file. • See Demo 95 Determining File Size • Recall: The “ls” command with the option –l gives the file size in bytes. • Use the command named “wc” to display the size of files as number of lines, words, and characters. • Syntax: wc file-list • Commonly used options: -l display the number of lines -w display the number of words -c display the number of characters • See demo 96Copyright Department of Computer Science,Northern Illinois University, 2004 01-48
  49. 49. Introduction to UNIX The wc Command 97 Compress File Contents • The command named “compress” can be used to reduce the size of one or more files. • Syntax: compress file-name • To display compression percentage and the names of compressed files, use the option named –v. • The file extension .Z is automatically appended to file-name. • See Demo 98Copyright Department of Computer Science,Northern Illinois University, 2004 01-49
  50. 50. Introduction to UNIX Viewing a Compressed File • The command named “zcat” can be used to display the contents of a compressed file in a readable format. Example: Display the contents of a compressed file called “customer-data.Z”. ux% zcat customer-data.Z 99 Un-compress File Contents • To uncompress one or more compressed files, used the command named “uncompress”. • Syntax: uncompress file-list Example: uncompress a compressed file called “customer-data.Z”. ux% uncompress customer-data.Z 100Copyright Department of Computer Science,Northern Illinois University, 2004 01-50
  51. 51. Introduction to UNIX Sorting Files • To sort a text file in ascending or descending order, use the command named “sort”. • Syntax: sort [options] file-name • Commonly used options: -r sort in reverse order -n numeric sort +x [-y] specify a field as the sort key; skipping x fields and start sorting with field y -f consider lowercase and uppercase to be equivalent 101 The sort Command 102Copyright Department of Computer Science,Northern Illinois University, 2004 01-51
  52. 52. Introduction to UNIX Fields Within A Line 103 Field Specifier Examples 104Copyright Department of Computer Science,Northern Illinois University, 2004 01-52
  53. 53. Introduction to UNIX Global and Local Options 105 Removing Repeated Lines • The command named “uniq” can be used to remove repetitious lines from a sorted input file, sending unique (unrepeated) lines to standard output. • Syntax: uniq sorted-file-name • Commonly used options: -c place a count of repeated lines at beginning of each output line -d display the repeated lines -u display the lines that are not repeated 106Copyright Department of Computer Science,Northern Illinois University, 2004 01-53
  54. 54. Introduction to UNIX The unique Command 107 Encrypting Files • Encryption is a process that transforms a file to an unreadable form. • The transformed file is called an “encrypted” file. • You encrypt files to prevent other users from reading their contents. • The command named “crypt” can be used to encrypt files. • Syntax: crypt key < original-file > encrypted file • See Demo 108Copyright Department of Computer Science,Northern Illinois University, 2004 01-54
  55. 55. Introduction to UNIX Decrypting Files • The process of transforming an encrypted file to its original format is called “decryption”. • To decrypt an encrypted file, use the command named “crypt”. • Syntax: crypt key < encrypted-file > original-file • See Demo 109 UNIX Text Editors 110Copyright Department of Computer Science,Northern Illinois University, 2004 01-55
  56. 56. Introduction to UNIX Editor Concepts • Editing – creating a new file and modifying an existing text file. • An editor – a utility that makes the editing task possible. • A text editor differs from a word processor in that it does not perform text formatting, such as bold, center, underline, etc. • A line editor – a utility that applies changes to a line or group of lines; two common line editors: ex and sed. • A screen editor – shows a whole screen of text at a time; we can move cursor or select part of text, search for text, etc. 111 The Vi Editor • A screen editor available on most UNIX systems. • When invoked, it copies the contents of a file to a memory space know as a work buffer. • All editing are applied to the contents in the work buffer. • If the file does not exist, an empty buffer is created. • When we exit vi, the work buffer is erased. • At exit time, we can do two things: 1. Quit without saving – the original contents remain unchanged. 2. Save the file – the original contents are replaced by the new version in the work buffer. 112Copyright Department of Computer Science,Northern Illinois University, 2004 01-56
  57. 57. Introduction to UNIX Vi Buffers • Work Buffer – This is where vi performs all editing – Vi makes a copy of your file in the work buffer – Makes all changes to the copied version – Replaces original copy with edited copy when you save (:w) • General Purpose Buffer – Stores recent deleted, or copied text – Used by the ‘undo’ command to restore text • Name Buffers – 26 named buffers, each named by a letter of the alphabet – Used to store a different block of text to recall later 113 The UNIX vi Text Editor Esc Command Input Mode Mode Insert (i, I) Append (a, A), : Return Open (o, O) Change (c), Replace (r, R) Last-Line Mode 114Copyright Department of Computer Science,Northern Illinois University, 2004 01-57
  58. 58. Introduction to UNIX Vi Mode • In vi, to find out what mode you are in, you need to create a vi start-up file (.exrc) • In your home directory or in your cwd directory, create “.exrc” file with contents: set showmode 115 File Names • When naming a file, avoid the following characters: & * | [] {} $ <> () # ? / “ ‘ ; ^ ! ~ Space Tab 116Copyright Department of Computer Science,Northern Illinois University, 2004 01-58
  59. 59. Introduction to UNIX Commonly Used File Extensions Extension File Type .a An archive or library .c C program source .cc C++ program source .csh C shell script .f FORTRAN program source .sh Bourne shell script .bsh Bash shell script .ksh Korn Shell script 117 Commonly Used File Extensions Extension File Type .o Object file of compiled program .ps Postscript source .shar Shell archive .tar Tar archive .txt ASCII text file .Z Compressed file 118Copyright Department of Computer Science,Northern Illinois University, 2004 01-59
  60. 60. Introduction to UNIX Recovering Text After a Crash • If the system crashes while you are editing a file using vi, you may be able to recover your text • The system sends you an e-mail telling you how to recover your file • To recover a file after a system crash: % vi –r filename 119 Vi Demo Demo 120Copyright Department of Computer Science,Northern Illinois University, 2004 01-60
  61. 61. Introduction to UNIX Access Permissions 121 Terminology • A user – any one who has Unix account on the system. • Unix recognizes a user by a number called user id. • A super user: – has the maximum set of privileges in the system – also know as system administrator – can change the system – must have a lot of experience and training • Users can be organized into groups. • One or more users can belong to multiple groups. 122Copyright Department of Computer Science,Northern Illinois University, 2004 01-61
  62. 62. Introduction to UNIX Users 123 Terminology • To find out group information, use the command named: groups user-id • Example: To find out what groups the user z036473 belongs to. ux% groups z036473 student csci467a csci330c • Information about groups is stored in the Network Information Service (NIS) file named group.org_dir. 124Copyright Department of Computer Science,Northern Illinois University, 2004 01-62
  63. 63. Introduction to UNIX Security Levels • There are three levels of security in UNIX: system, directory and file. • System security – controlled by the system administrator, a super user. • Directory and file – controlled by the user who owned them. 125 Security Levels 126Copyright Department of Computer Science,Northern Illinois University, 2004 01-63
  64. 64. Introduction to UNIX Access Permission Code • The protection on a file is referred to as its file modes • File modes are set with the “chmod” command • UNIX supports three types of access permissions: r read w write x execute - permission denied 127 Directory and File Permissions 128Copyright Department of Computer Science,Northern Illinois University, 2004 01-64
  65. 65. Introduction to UNIX Access Types Access Type Meaning on File Meaning on Dir. r (read) View file contents List directory contents (open, read) w (write) Change file contents - Change directory contents - Be careful !!! x (execute) Run executable file - Make it your cwd - Access files (by name) in it - Permission denied Permission denied 129 Checking Permissions • To check the permissions of an existing file or an existing directory, use the command: ls –l • Example: ux% ls –l unix total 387 drwxr--r-- 1 z036473 student 862 Feb 7 19:22 unixgrades -rw-r--r-- 1 z036473 student 0 Jun 24 2003 uv.nawk -rw-r--r-- 1 z036473 student 0 Jun 24 2003 wx.nawk -rw-r--r-- 1 z036473 student 0 Jun 24 2003 yz.nawk 130Copyright Department of Computer Science,Northern Illinois University, 2004 01-65
  66. 66. Introduction to UNIX Changing Permissions 131 The chmod Command 132Copyright Department of Computer Science,Northern Illinois University, 2004 01-66
  67. 67. Introduction to UNIX Changing Permissions: Symbolic Mode 133 Changing Permissions: Symbolic Mode mp% chmod who operation permissions filename u for user + for add r for read g for group - for remove w for write o for others = for assign x for execute a for all 134Copyright Department of Computer Science,Northern Illinois University, 2004 01-67

×