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.

Slides:



Advertisements
Similar presentations
UNIX Chapter 10 Advanced File Processing Mr. Mohammad Smirat.
Advertisements

การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
LINUX System : Lecture 3 (English-Only Lecture) Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University Acknowledgement.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Guide To UNIX Using Linux Third Edition
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.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
BILKENT UNIVERSITY DEPARTMENT OF COMPUTER TECHNOLOGY AND INFORMATION SYSTEMS CTIS156 INFORMATION TECHNOLOGIES II CHAPTER 10: ADVANCED FILE PROCESSING.
Advanced File Processing
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
BIF703 Miscellaneous Commands. File related commands  grep - print lines matching a pattern  head - output the first part of files  tail - output the.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Guide To UNIX Using Linux Fourth Edition
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
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.
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 8 Shell.
Dedan Githae, BecA-ILRI Hub Introduction to Linux / UNIX OS MARI eBioKit Workshop; Nov , 2014.
Linux+ Guide to Linux Certification Chapter Four Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Third Edition
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
Chapter 10 Advanced File Processing. Regular Expressions A compact notation for representing patterns in strings Used by many common Linux utilities such.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
OPERATING SYSTEMS DESIGN UNIX BASICS & SHELL SCRIPTING.
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 Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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 11: Shell.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
BIF713 Additional Utilities. Linux Utilities  You have learned many Linux commands. Here are some more that you can use:  Data Manipulation (Reg Exps)
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
UNIX An Introduction. Brief History UNIX UNIX Created at Bell Labs, 1969 Created at Bell Labs, 1969 BSD during mid 70s BSD during mid 70s AT&T began offering.
BASH – Text Processing Utilities Erick, Joan © Sekolah Tinggi Teknik Surabaya 1.
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
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.
Linux Commands C151 Multi-User Operating Systems.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
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.
Lesson 5-Exploring Utilities
Chapter 11 Command-Line Master Class
Advanced File Processing
The UNIX Shell Learning Objectives:
Some Linux Commands.
C151 Multi-User Operating Systems
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands:
Tutorial of Unix Command & shell scriptS 5027
Basic UNIX OLC Training.
Tutorial of Unix Command & shell scriptS 5027
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Linux Shell Script Programming
Introduction to Bash Programming, part 3
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Review.
LPI Linux Certification
Presentation transcript:

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 Utilities

© Copyright IBM Corporation 2008 Unit Objectives After completing this unit, you should be able to: Use the find and locate commands to search for files Use the grep command to search text files for patterns Use the cut command to list specific columns of a file Use the sort command to sort the contents of a file Use the head and tail commands to view specific lines in a file Use the type, which and whereis commands to find commands Use the file command to find out the content of a file Use the join and paste commands to combine files Manipulate files with gzip, gunzip and zcat

© Copyright IBM Corporation 2008 The find Command Search one or more directory structures for files that meet certain specified criteria. Display the names of matching files or execute commands against those files. Syntax: $ find path expression

© Copyright IBM Corporation 2008 / home tux1 size shape color blue green orange red big giant orange small box circle orange square orange phone1 phone2 sample Sample Directory Structure

© Copyright IBM Corporation 2008 $ cd /home/tux1 $ find. -name orange./orange./color/orange./shape/orange./size/orange Using find Generally, you want to search a directory structure for files with certain names and list the names found.

© Copyright IBM Corporation 2008 $ find. -name 's*' -exec ls -i {} \; /sample /shape/square /size/small " { } " is a placeholder for each filename. The backslash escapes the following semicolon. Executing Commands with find The -exec option executes a command on each of the file names found.

© Copyright IBM Corporation 2008 $ find. -name o\* -ok rm {} \; ? y ? n ? y Interactive Command Execution The -ok option also causes command execution but on an interactive basis:

© Copyright IBM Corporation type f d ordinary file directory -size+n -n nc larger than n blocks smaller than n blocks equal to n characters -mtime+x -x x modified more than x days ago modified less than x days ago modified x days ago -perm onum mode access permissions match onum access permissions match mode -user user finds files owned by user -newer ref.file file was modified more recently than ref.file -o -a Logical OR Logical AND Additional find Options

© Copyright IBM Corporation 2008 $ find. -perm 777./size/giant File matches expr 1 and expr 2: $ find. -name 's*' -type f -a -size +2\ >-exec ls -i {} \; /sample /shape/square /size/small File matches expr 1 or expr 2: $ find. -name circle -o -name 'b*'./color/blue./size/big./shape/box./shape/circle find Examples

© Copyright IBM Corporation 2008 locate allows you to quickly find a file on the system, based on simple criteria. Requires that the superuser runs updatedb regularly. –Most distributions run updatedb automatically. –SuSE does not install locate/updatedb by default. $ locate passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz /etc/passwd /usr/bin/passwd locate Command

© Copyright IBM Corporation 2008 The grep Command Searches one or more files or standard input for lines matching pattern Simple match or Regular Expression Syntax grep [options] pattern [file1...]

© Copyright IBM Corporation 2008 Phone 1: Phone 2: Chris10300internal Jan 20500internal Lee 30500external Pat 40599external Robin 50599external Terry 60300internal Chris1342internal Jan2083internal Lee3139external Pat4200internal Robin5200internal Terry6342external grep Sample Data Files

© Copyright IBM Corporation 2008 $ grep 20 phone1 Jan20500internal $ grep 20 phone* phone1:Jan20500internal phone2:Jan2083internal phone2:Pat4200internal phone2:Robin5200internal $ grep -v Jan phone2 Chris1342internal Lee3139external Pat4200internal Robin5200internal Terry 6342external Basic grep

© Copyright IBM Corporation 2008 grep with Regular Expressions Patterns with metacharacters should be in single quotation marks (' ') so that the shell will leave it alone. Valid metacharacters with grep: $. * ^ [ - ]. Any single character * Zero or more occurrences of the preceding character [a-f] Any ONE of the characters in the range a through f ^a Any line that starts with a z$ Any line that ends with z

© Copyright IBM Corporation 2008 Regular Expression 1.Display all the processes that belong to tux1. ____________________________________________ Using grep, write commands to select the following lines from the phone1 file: 2.Select all the lines of the file (blank and non-blank). grep ___________ phone1 3.Select all the lines that contain an e and end in a 0. grep ___________ phone 1

© Copyright IBM Corporation v Print lines that do not match -c Print only a count of matching lines -l Print only the names of the files with matching lines -n Number the matching lines -i Ignore the case of letters when making comparisons -w Do a whole word search -f Read expressions from file instead of command line grep Options

© Copyright IBM Corporation 2008 fgrep allows only fixed strings (no regular expressions). egrep allows for multiple (alternate) patterns. What does the following command do? $ egrep '20500|40599|50599' phone1 Jan 20500intern al Pat 40599extern al Robin 50599extern al $ grep 30 phone1 | grep intern ???????? Other greps

© Copyright IBM Corporation 2008 The cut Command Pull selected columns or fields from one or more files. Syntax: cut -f(ields) -d(elimiter) file(s) cut -c(haracters) file(s)

© Copyright IBM Corporation 2008 $ cat /etc/passwd root:x:0:0:Big Brother:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown tux1:x:500:500:Tux the Penguin(1):/home/tux1:/bin/bash tux2:x:501:501:Tux the Penguin(2):/home/tux2:/bin/bash $ cut -f1,3,6,7 -d: /etc/passwd root:0:/root:/bin/bash shutdown:6:/sbin:/sbin/shutdown tux1:500:/home/tux1:/bin/bash tux2:501:/home/tux2:/bin/bash cut Example (1 of 2)

© Copyright IBM Corporation 2008 $ ps PID TTY STAT TIME COMMAND 374 p0 S 0:00 -bash 460 p0 R 0:00 ps $ ps | cut -c-5,20- PID COMMAND 374 -bash 471 ps cut Example (2 of 2)

© Copyright IBM Corporation 2008 $ cat animals dog.2 cat.4 penguin.10 $ sort animals cat.4 dog.2 penguin.10 The sort Command The sort command sorts the lines in the file specified and writes the result to standard output. sort -t(delimiter) +field -options file

© Copyright IBM Corporation 2008 $ sort +0.1 animals cat.4 penguin.10 dog.2 $ sort -t. +1 animals penguin.10 dog.2 cat.4 $ sort -t. -n +1 animals dog.2 cat.4 penguin.10 Options: -d Sorts in dictionary order. Only letters, digits and spaces are considered in comparisons. -r Reverses the order of the specified sort. -n Sorts numeric fields in arithmetic value. sort Examples

© Copyright IBM Corporation 2008 The head command can be used to view the first few lines of a file or files. The command syntax is: $ head [-lines] file(s) The tail command displays the last few lines of a file or files. The command syntax is: $ tail [{-lines|+lines|-f}] file(s) $ head -5 myfile $ ls -l | head -12 $ tail -20 file $ tail +20 file $ tail -f file The head and tail Commands

© Copyright IBM Corporation 2008 To find out what the path to a command is, use type. To find out where the binary is located, use which. To locate the binary, source, and manual page files of a command, use whereis. $ type find echo find is /usr/bin/find echo is a shell builtin $ whereis find echo find: /usr/bin/find /usr/man/man1/find.1 echo: /bin/echo /usr/man/man1/echo.1 $ which find echo /usr/bin/find /bin/echo The type, which, and whereis Commands

© Copyright IBM Corporation 2008 $ file /etc/passwd /bin/ls /home/tux1 /tmp/fake.jpg /etc/passwd: ASCII text /bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked, stripped /home/tux1: directory /tmp/fake.jpg: PDF document, version 1.3 The file Command With the file command, you can find out what the type of data in the file is.

© Copyright IBM Corporation 2008 $ cat one a apple another b bee beast $ cat two a ape b broken $ join one two a apple another ape b bee beast broken $ paste one two a apple another a ape b bee beast b broken The join and paste Commands join and paste combine files.

© Copyright IBM Corporation 2008 $ ls -l file1 -rw-rw-r-- 1 team01 team Jan 1 23:40 file1 $ gzip -v file1 file1:89.9% -- replaced with file1.gz $ ls -l file1.gz -rw-rw-r-- 1 team01 team Jan 1 23:42 file1.gz $ zcat file1 (output is the same as the output of the cat command with the uncompressed file) $ gunzip file1 $ ls -l file1 -rw-rw-r-- 1 team01 team Jan 1 23:40 file1 The gzip, gunzip, and zcat Commands To compress or uncompress files, use gzip, gunzip, or zcat.

© Copyright IBM Corporation 2008 Checkpoint 1.True or False? The command ps -aux | grep tux | grep netscape lists all Netscape processes of a user named tux. 2.Which command would best be used to locate all files in your system that begin with the string "team"? a.find / -name "^team" b.find / -name "team*" c.find / -name "*team*" d.find / -type f -name "team" 3.Translate the following command into your native language: ls -lR | egrep "txt$|tab$"| sort -rn +4 | tail +4 | head -5

© Copyright IBM Corporation 2008 Unit Summary The following commands were considered: The find command is used to recursively search directories for files with particular characteristics. The grep command is used to select entire lines containing a particular pattern. The head and tail commands are used to view specific lines in a file. The sort command sorts the contents of a file by the options specified. Find out where you can find commands with type, where, and whereis. The gzip, zcat, and gunzip commands can be used to create and work with compressed files.