Xuan Guo Chapter 3-2 Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003. CSc 3320 1.

Slides:



Advertisements
Similar presentations
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Advertisements

ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Jan. 27 th, 2009 Essential Unix Commands.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 12.
File security and Permissions A file is owned by the user who created it That user can then specify who can read, write and execute that file A file when.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
2000 Copyrights Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 5.
More Unix Utilities (find, diff/cmp, at/crontab, tr)
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
CS 497C – Introduction to UNIX Lecture 16: - File Attributes Chin-Chih Chang
Guide To UNIX Using Linux Third Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
Links Software Tools. Lecture 4 / Slide 2 Links l A link is a pointer to a file. l In fact, in UNIX all filenames are just links to a file. Most files.
CSCI 330 T HE UNIX S YSTEM File operations. OPERATIONS ON REGULAR FILES 2 CSCI The UNIX System Create Edit Display Contents Display Contents Print.
UNIX. find command ● The find command is used to locate files on a Unix or Linux system. find will search any set of directories you specify for files.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Chapter 3 UNIX Utilities for Power Users Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original.
Shell Script Examples.
Linux Commands LINUX COMMANDS.
Review for Exam 1 Exam 1 on June 24 CSC 3320.
BILKENT UNIVERSITY DEPARTMENT OF COMPUTER TECHNOLOGY AND INFORMATION SYSTEMS CTIS156 INFORMATION TECHNOLOGIES II CHAPTER 10: ADVANCED FILE PROCESSING.
Advanced File Processing
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Lesson 11-Locating, Printing, and Archiving User Files.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Unix programming Term: III B.Tech II semester Unit-II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
Linux Operations and Administration
Agenda Chapter 1: Linux (Unix) Features Commands (Chapters 2 & 3) Command Structure / Command line editing man, passwd, cal, date, whereis, which Working.
Jozef Goetz, expanded by Jozef Goetz, 2009 Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert.
CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Find Find basics. find ~ -name myfile –print find directory criteria This will search the home directory (~) looking for files.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved Chapter 5 File Management File Overview.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 10 Linux.
BIF713 Additional Utilities. Linux Utilities  You have learned many Linux commands. Here are some more that you can use:  Data Manipulation (Reg Exps)
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
LIN Unix Lecture 7 Hana Filip. LIN Text Processing Command Line Utility Programs (cont.) sed LAST WEEK wc sort tr uniq awk TODAY join paste.
숙명여대 창병모 2011 가을 1. 파일 조작  filtering, sorting, comparing, searching for files 명령어 스케줄링  cron, at 텍스트 처리 프로그램  AWK 파일 보관 및 압축  archiving  tar, gzip.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
Chapter 7 UNIX Utilities for Power Users Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 제 3 장 유틸리티 숙명여대 창병모 파일 조작 filtering, sorting, comparing, searching for files 명령어 스케줄링 cron, at 텍스트 처리 프로그램 AWK 파일 보관 및 압축 archiving tar,
CSCI 330 UNIX and Network Programming Unit II Basic UNIX Usage: File System.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Chapter 3 UNIX Utilities for Power Users Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
Chapter 3 UNIX Utilities for Power Users Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Original.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
Linux 201 Training Module Linux Adv File Mgmt.
Lesson 5-Exploring Utilities
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands:
INTRODUCTION TO UNIX: The Shell Command Interface
Guide To UNIX Using Linux Third Edition
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Chapter Four UNIX File Processing.
Linux Shell Script Programming
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Review.
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

Xuan Guo Chapter 3-2 Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, CSc

Xuan Guo uniq fed with a text file, output the file with adjacent identical lines collapsed to one uniq -c -number [inputfile [outputfile]] -c: causes each line to be preceded by the number of occurrences that were found -number: number fields of each line are ignored CSc

Xuan Guo Happy Birthday to You! Happy Birthday Dear Tux! Happy Birthday to You! uniq uniq.dat Happy Birthday to You! Happy Birthday Dear Tux! Happy Birthday to You! CSc

Xuan Guo Happy Birthday to You! Happy Birthday Dear Tux! Happy Birthday to You! uniq -c uniq.dat 2 Happy Birthday to You! 1 Happy Birthday Dear Tux! 1 Happy Birthday to You! CSc

Xuan Guo Sort sorts a file in ascending or descending order based on one or more fields. Individual fields are ordered lexicographically, which means that corresponding characters are compared based on their ASCII value. CSc

Xuan Guo Sort sort -tc -r [+POS1 [-POS2]] {sortField -bfMn}* {fileName}* –-tc separator is c instead of blank –-r descending instead of ascending –-b ignore leading blanks –-f ignore case –-M month sort (3 letter month abbreviation)‏ –-n numeric sort –+POS1 [-POS2] key positions start [up to end] CSc

Xuan Guo Sort Examples CSc $ cat sort.dat John Smith Apr 1956 Tony Jones Mar 1950 John Duncan Jan 1966 Larry Jones Dec 1946 $ sort sort.dat John Duncan Jan 1966 John Smith Apr 1956 Larry Jones Dec 1946 Tony Jones Mar 1950

Xuan Guo Sort Examples CSc $ sort M sort.dat John Duncan Jan 1966 Tony Jones Mar 1950 John Smith Apr 1956 Larry Jones Dec 1946 $ sort sort.dat John Smith Apr 1956 Larry Jones Dec 1946 John Duncan Jan 1966 Tony Jones Mar 1950

Xuan Guo Sort Examples CSc $ sort -k 6,6 -k 5,5 sort.dat Larry Jones Dec 1946 Tony Jones Mar 1950 John Smith Apr 1956 John Duncan Jan 1966

Xuan Guo Archiving tar -cfrtuvx tarFileName fileList –creates a “tape archive”-format file from the fileList –Typically, use either -c or -x, but not both! -c for Create -x for Extracts –-t option generates a table of contents –-r option unconditionally appends listed files to tar formatted file CSc

Xuan Guo Archiving tar -cfrtuvx tarFileName fileList –-u option appends only files that are more recent than those already archived –-f option enables you to give a tar file name Default name is /dev/rmt0 –-v verbose If fileList contains directory, its contents are appended/extracted recursively. CSc

Xuan Guo Archive Example CSc $ tar -cvf ch6.tar ch6 ch6/ ch6/menu.csh ch6/junk/ ch6/junk/junk.csh ch6/junk.csh ch6/menu2.csh ch6/multi.csh ch6/expr1.csh ch6/expr3.csh ch6/expr4.csh ch6/if.csh ch6/menu3.csh

Xuan Guo Archive Example CSc $ ls -l ch6.tar -rw-rw-r-- 1 raj raj Jun 26 20:08 ch6.tar $ tar -tvf ch6.tar drwxr-xr-x raj/raj :57 ch6/ -rwxr-xr-x raj/raj :50 ch6/menu.csh drwxr-xr-x raj/raj :57 ch6/junk/ -rwxr-xr-x raj/raj :57 ch6/junk/junk.csh -rwxr-xr-x raj/raj :56 ch6/junk.csh -rw-r--r-- raj/raj :59 ch6/menu2.csh -rwxr-xr-x raj/raj :26 ch6/multi.csh -rwxr-xr-x raj/raj :18 ch6/expr1.csh -rwxr-xr-x raj/raj :20 ch6/expr3.csh -rwxr-xr-x raj/raj :21 ch6/expr4.csh -rwxr-xr-x raj/raj :22 ch6/if.csh -rw-r--r-- raj/raj :01 ch6/menu3.csh

Xuan Guo Archive Example CSc $ rm -fr ch6 $ tar -rvf ch6.tar date.txt date.txt $ tar -tvf ch6.tar drwxr-xr-x raj/raj :57 ch6/ -rwxr-xr-x raj/raj :50 ch6/menu.csh drwxr-xr-x raj/raj :57 ch6/junk/ -rwxr-xr-x raj/raj :57 ch6/junk/junk.csh -rwxr-xr-x raj/raj :56 ch6/junk.csh -rw-r--r-- raj/raj :59 ch6/menu2.csh -rwxr-xr-x raj/raj :26 ch6/multi.csh -rwxr-xr-x raj/raj :18 ch6/expr1.csh -rwxr-xr-x raj/raj :20 ch6/expr3.csh -rwxr-xr-x raj/raj :21 ch6/expr4.csh -rwxr-xr-x raj/raj :22 ch6/if.csh -rw-r--r-- raj/raj :01 ch6/menu3.csh -rw-rw-r-- raj/raj :06 date.txt

Xuan Guo Archive Example CSc $ tar -xvf ch6.tar ch6/ ch6/menu.csh ch6/junk/ ch6/junk/junk.csh ch6/junk.csh ch6/menu2.csh ch6/multi.csh ch6/expr1.csh ch6/expr3.csh ch6/expr4.csh ch6/if.csh ch6/menu3.csh date.txt

Xuan Guo Find Utility find pathList expression finds files starting at pathList finds files descending from there Allows you to perform certain actions –e.g. deleting the files CSc

Xuan Guo Find Utility -name pattern true if the file name matches pattern -perm oct true if the octal description of file's permission equals oct -type ch true if the type of the file is ch (b=block, c=char..)‏ -user userId true if the owner of the file is userId -group groupId true if the group of the file is groupId CSc

Xuan Guo Find Utility -atime count true if the file has been accessed within the count-th day -ctime count true if the contents of the file have been modified within the count-th day or any of its file attributes have been modified -exec command true if the exit code = 0 from executing the command. command must be terminated by \; If {} is specified as a command line argument it is replaced by the file name currently matched CSc

Xuan Guo Find Utility -print prints out the name of the current file and returns true -ls displays the current file's attributes and returns true !expression negation of expression expr1 [-a] expr2 short circuit and expr1 -o expr2 short circuit or CSc

Xuan Guo Find Examples $ find / -name x.c –searches for file x.c in the entire file system $ find. -mtime 14 -ls –lists files modified in the 14th day of the past $ find. -name '*.bak' -ls -exec rm {} \; –ls and then remove all files that end with.bak CSc

Xuan Guo Find Examples CSc $ find ch6 -name ‘j*’ ch6/junk ch6/junk/junk.csh ch6/junk.csh

Xuan Guo Scheduling Command (cron)‏ $ crontab cronTabName $ crontab -ler [useName] –Allows you to schedule a series of jobs –Executed on a periodic basis –Uses a file with the following line format: CSc minute hour day month weekday command (0-59) (0-23) (1-31) (1-12) (1-7; 1=Monday) (any command)‏

Xuan Guo Scheduling Example CSc $ cat crontab.cron 0 8 * * 1 echo Happy Monday Morning * * * * * echo One Minute passed > /dev/pts/0 minute hour day month weekday command (0-59) (0-23) (1-31) (1-12) (1-7; 1=Monday) (any command)‏

Xuan Guo Scheduling Example CSc $ crontab crontab.cron $ crontab -l # DO NOT EDIT THIS FILE - edit the master and reinstall. # (crontab.cron installed on Sat Jun 26 23:33: )‏ # (Cron version -- $Id: crontab.c,v /01/17 03:20:37 vixie Exp $)‏ 0 8 * * 1 echo Happy Monday Morning * * * * * echo One Minute passed > /dev/pts/0 $ crontab -r unregister crontab file

Xuan Guo Scheduling Command (at)‏ $ at command –allows you to schedule one-time commands/scripts $ at -csm time [date [, year]] [+increment] [-f script] –-c option : use C-shell –-s option : use Bourne shell –-m option : send mail CSc

Xuan Guo Scheduling Command (at)‏ $ at -l –-l option list all entries in at queue $ atrm [jobId]+ –remove entries in at queue CSc

Xuan Guo Scheduling Command (at)‏ $ at -csm time [date [, year]] [+increment] [script] –time is specified as HH or HHMM followed by an optional AM/PM –date is spelled out using first 3 letters of day and/or month –keyword now can be used in place of time sequence; today and tomorrow can be used in place of date –stated time may be augmented by an increment (number followed by minutes/hours/days/weeks/months/years)‏ CSc

Xuan Guo Scheduling Example CSc $ cat at.csh #!/bin/csh echo at done > /dev/tty1 $ at now + 2 minutes -f at.csh $ at -l lists the job here You may program the script to reschedule itself as follows: #!/bin/csh date > /dev/tty1 at now + 2 minutes -f at.csh

Xuan Guo Hard Links $ ln original newLink –Creates a hard link to original file called newLink –Both labels will refer to the same file –File will be deleted only when both labels are removed –If newLink is a directory then links are made within the directory CSc

Xuan Guo Hard Link Example CSc $ cat > one This is an example file. $ ln one two $ cat two This is an example file. $ vi two # changed “an” to “a great” $ cat two This is a great example file. $ cat one This is a great example file. $ ls -l two -rw-r--r-- 2 mweeks mweeks 31 Sep 14 03:26 two

Xuan Guo Soft (Symbolic) Links $ ln -s original newLink –Symbolic/soft links can be created from one file system into another file system –Hard links are restricted to one file system Use ls -lL to view link details –which file it refers to ls -l displays the contents of the symbolic link CSc $ ls -l lrwxrwxrwx 1 root root :26 javac -> j2sdk1.4.2_05/bin/javac $ ls -lL -rwxrwxr-x users :32 javac $ ls -l j2sdk1.4.2_05/bin/javac -rwxrwxr-x users :32 j2sdk1.4.2_05/bin/javac

Xuan Guo In Depth Unix files consist of two parts: the data part and the filename part. The data part is associated with something called an 'inode'. The inode carries the map of where the data is, the file permissions, etc. for the data. CSc

Xuan Guo In Depth The filename part carries a name and an associated inode number. More than one filename can reference the same inode number; these files are said to be 'hard linked' together. CSc

Xuan Guo In Depth a 'soft link' or a 'symbolic link' is a special file type whose data part carries a path to another file. CSc

Xuan Guo Substituting User $ su [-] userName –If userName is not specified, root is assumed –Need access privileges for this –Requires password $ sudo command –User can execute command as superuser –Requires password CSc

Xuan Guo Compress/Uncompress gzip –$ gzip fileName (.gz)‏ –$ gunzip filename tar –$ tar –zcvf filename {filename}* CSc

Xuan Guo Translate Utility (tr)‏ $ tr set1 set2 maps all characters in std. input from character set set1 to the corresponding character in set2 If length of set2 is less, the last character is repeated. CSc

Xuan Guo Translate Utility (tr)‏ $ tr -cds set1 set2 –-c option causes string1 to be complemented (every character not in string1 now is in string1!)‏ –-d option causes every character in string1 to be deleted from std. input –-s option causes every repeated character in string1 to be condensed to one occurrence CSc

Xuan Guo Examples of tr $ tr a-z A-Z file2 –causes lower-case to upper-case conversion $ tr -c a X file2 –causes every non-a character to be replaced by X (including newline)‏ $ tr -c a-z '\012' file2 –causes all non alphabetic characters to be replaced by ASCII 12 (newline)‏ $ tr -d a-c file2 –causes all a, b, c to be deleted from file1 CSc

Xuan Guo Comparing Files What if you have two files with the same name, but different sizes? $ cmp file1 file2 offset1 offset2 –compares two files for equality –reports the first byte where there is a mismatch –if one file is a prefix of the other, EOF message is displayed CSc

Xuan Guo Comparing Files cmp -ls file1 file2 offset1 offset2 Optional values offset1 and offset2 are the offsets into the files where comparison begins. –-l option displays the line number and byte offset of all mismatched bytes –-s option suppresses all output, return exit status only (0, 1, >1) CSc

Xuan Guo Comparing Files $ diff -i file1 file2 –compares two files –outputs a description of their differences –-i flag ignores case –(a for add, c for change, or d for delete) CSc