Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Linux+ Guide to Linux Certification, Second Edition
CSE 303 Lecture 2 Introduction to bash shell
Guide To UNIX Using Linux Third Edition
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
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.
Introduction to UNIX GPS Processing and Analysis with GAMIT/GLOBK/TRACK T. Herring, R. King. M. Floyd – MIT UNAVCO, Boulder - July 8-12, 2013 Directory.
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 Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
Chapter 4: UNIX File Processing Input and Output.
Advanced File Processing
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
A 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.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Dedan Githae, BecA-ILRI Hub Introduction to Linux / UNIX OS MARI eBioKit Workshop; Nov , 2014.
Linux+ Guide to Linux Certification, Third Edition
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
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.
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.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Introduction to Unix (CA263) File Processing (continued) By Tariq Ibn Aziz.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
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.
40 Years and Still Rocking the Terminal!
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
Learning Unix/Linux Based on slides from: Eric Bishop.
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.
SIMPLE FILTERS. CONTENTS Filters – definition To format text – pr Pick lines from the beginning – head Pick lines from the end – tail Extract characters.
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
The UNIX Shell Learning Objectives:
Some Linux Commands.
Chapter 6 Filters.
Linux command line basics III: piping commands for text processing
Exploring Shell Commands, Streams, and Redirection
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 303 Concepts and Tools for Software Development
Exploring Shell Commands, Streams, and Redirection
A shell is a user interface.
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and IO redirection Commands used with redirection Some useful filters: awk, grep, sed etc.

The file system and pathnames The filesystem is a set of conventions and the specific data design for organising files and folders, or directories on a hard disk, USB drive, CD or other media. E.G. NTFS, FAT (Windows), EXT3 (Linux)‏ Files on a filesystem include plain files, directory files (or folders), links (Unix/Linux only) and device files. All modern OSs map files on a filesystem as an inverted tree.

Inverted tree with root dir as / / (root directory)‏ | | | | | | dir1 file1 file2 dir2 home | | | | | | | | | file3 dir3 file4 file5 fred bill | | | | | | | file6 file7 file8 cupboard wardrobe | | | | socks vests shirts Absolute pathnames start with a / and relative pathnames don't.

Some pathname examples

Differences between Windows and Unix Pathname delimiters: Unix forward / and Windows back \ Windows has drive letters e.g. a: Unix trees mount filesystems onto empty directories. So, a:\reports\report.txt on Windows becomes /mnt/floppy/reports/report.txt on Unix

Links Multiple paths to the same file saves space and keeps objects updated Hard link: ln /sales/personnel/simon /bonuses/recipients/simon Soft link: ln -s /usr/wizard/wands/magic/software/binaries /wiz Hard links only possible within same physical device, soft links needed to link between mounted devices/partitions.

Shell Wildcards Known as "regular expressions", REs or regexes. Used in shell commands to match or "glob" multiple files. Similar language used in Grep, Awk, Perl, Java, Python etc. to find/replace expressions in strings, e.g. tags in HTML

Shell Wildcards

Shell Wildcards can be combined, giving greater flexibility

REs and special characters. at the beginning must match explicitly, and / must always match explicitly. Avoid putting *,?$^()[]\/&<>|"'! and spaces in filenames ? If you can't the backslash special character \ will usually escape the special character following it rm \$fred deletes the file called $fred Other special characters: *. at start of name hides a system file * ~ at start of path means home directory

Pipes and redirection

Redirections can be combined e.g: grep xxx f2 \ | tee f3 | wc -l > f4 \ is used to escape the newline so a single command line can be typed using more than one line on the terminal.

Commands often used with redirection

Some useful filters A filter is a program which reads from stdin and writes to stdout. Unix has many useful programs which filter data based on contents of text lines, or row positions or filtering on columns. We can also sort a file, obtain unique keys or chop the top or tail of it. These filters will make our pipelines into a general purpose and easy to use text-processing toolkit, once a few examples have been mastered.

awk Replaced as a scripting language by Perl, but still useful as a simple column filter. Example 1: cat file1 | awk '{ print $5, $2 }' outputs columns 5 then 2 of file1 which has to have spaces or tabs between columns. Use -F to delimit input using e.g. -F: for colon seperated columns. You might prefer cut for this job.

awk example 2 Put an awk program of any complexity into a separate file. Use the -f flag to give filename on shell command. This awkprog file has 3 lines: BEGIN { total=0 } {if ( $3 == "usr19999" ) total=total+$4 } END { print total/1024 } Run awk program with command: ls -l /scratch | awk -f awkprog Counts the number of 1K blocks used by files belonging to usr19999 in the /scratch directory.

cut For awk's usual purpose as a column filter, you might prefer to learn cut(1) as an alternative. The following command extracts columns 1, 3, 6 and 7 from /etc/passwd, with colon (:) as input delimiter, and space as output delimiter. cat /etc/passwd | cut -d: --output-delimiter=" " -f1,3,6,7

Input and output of cut command input sample from /etc/passwd bert:x:1003:1003:Bert Trusted User,,,:/home/bert:/bin/bash avahi-autoipd:x:111:111:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false avahi:x:116:121:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false snort:x:117:125:Snort IDS:/var/log/snort:/bin/false pulse:x:119:126:PulseAudio daemon,,,:/var/run/pulse:/bin/false Output sample bert 1003 /home/bert /bin/bash avahi-autoipd 111 /var/lib/avahi-autoipd /bin/false avahi 116 /var/run/avahi-daemon /bin/false snort 117 /var/log/snort /bin/false pulse 119 /var/run/pulse /bin/false

grep grep extracts lines containing a particular pattern from a file, using regexes. Similar but not identical to shell REs. fgrep is faster and does not interpret REs egrep interprets more REs than grep

grep examples

Sed is used for automated "stream" editing. Another language best used for one liners.

Another sed example cat f1 | sed '1,$s/(//g' | sed '1,$s/)//g' > f2 copies all lines from f1 to f2, removing all ( and ) brackets. sed is used twice in the same pipeline and "/" is the search-replace delimiter.

sort Has many options and parameters for ascending or descending, string or numeric, field and column sort key selection etc. See sort(1) for further details. Example: cat /etc/passwd | sort -t: +0n -1 | more Gives paged output of passwd database, sorted by userid.

uniq Having sorted our input data we may find repeated lines. The following example only outputs unique lines once: cat file.txt | sort | uniq The next example precedes all unique output lines with a count showing how many times each line occurred: cat file.txt | sort | uniq -c

head and tail Head copies the first part and tail copies the last part of stdin file, see head(1) and tail(1) manual pages. Example: head -5 index displays first 5 lines of index file

tr and colrm filters tr – is used to translate characters e.g. convert a text from lower to upper case: cat keller_security | tr a-z A-Z "SECURITY IS MOSTLY A SUPERSTITION. IT DOES NOT EXIST IN NATURE... LIFE IS EITHER A DARING ADVENTURE OR NOTHING." -- HELEN KELLER colrm – similar to cut but filters based only on character positions, and doesn't understand field delimiters. cat keller_security | colrm 36 "Security is mostly a superstition. either a daring adventure or nothin -- Helen Keller

Conclusions Redirections of input, output and error files combined with the use of some filter programs allows for us to manipulate the text data used for system configurations. We will be able to extract needles of security information from the system log data haystacks. Combining a few powerful one liners into short scripts will allow for fully customised administration and security monitoring of open-source servers, routers and firewalls.