file-ops

Embed Size (px)

Citation preview

  • 8/10/2019 file-ops

    1/24

    CSCI 330

    THEUNIX SYSTEM

    File operations

  • 8/10/2019 file-ops

    2/24

    OPERATIONS ON REGULAR FILES

    2

    CSCI330-

    TheUNIXSystem

    Create EditDisplay

    ContentsPrint Others

  • 8/10/2019 file-ops

    3/24

    CREATING NEW FILES

    3

    CSCI330-

    TheUNIXSystem

    Create Regular

    Files

    vim,

    emacs

    nano,

    etc.cat

    Redirect

    Command

    Output

    See Text Editors

    Section

    See shell

    Section

  • 8/10/2019 file-ops

    4/24

    CREATING A FILE WITH CAT

    4

    CSCI330-

    TheUNIXSystem

    Example:

    % cat > myfile

    This is line 1 of input

    Line 2 of input^d

    %

  • 8/10/2019 file-ops

    5/24

    DISPLAY CONTENTS OF TEXT FILES

    5

    CSCI330-

    TheUNIXSystem

    Display Text

    File contents

    cat more less pg head tail

  • 8/10/2019 file-ops

    6/24

    VIEWING CONTENTS OF TEXT FILES

    command cat can be used to

    display/concatenate one or more files,

    displaying the output all at once

    Example: Display the contents of file assign1.txt

    % cat assign1.txt

    6

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    7/24

    VIEWING CONTENTS OF TEXT FILES

    more, less or "pg"

    display the contents of one or more files

    one page at a time

    Space barto advance to next page bto go back a page

    Enter Keyto advance to next line

    Example: Display the contents of file assign1.txtone page at a time

    % less assign1.txt 7

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    8/24

    VIEWING CONTENTS OF TEXT FILES

    head

    displays the beginning portion of indicated file(s);

    the default head size is 10 lines.

    Example: Display first 20 lines of file assign1.txt

    % head -20 assign1.txt

    8

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    9/24

    VIEWING CONTENTS OF TEXT FILES

    tail

    displays the ending portion of indicated file(s);

    the default tail size is 10 lines.

    Example: Display last 10 lines of file assign1.txt

    % tail assign1.txt

    % tail -10 assign1.txt

    9

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    10/24

    PRINTING FILES

    lpr

    send a file to the default printer

    printers available:csl or lpcsl (default)

    frl or lpfrl

    ucl or lpucl

    Example:

    % lpr -P frl assign1.txt

    10

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    11/24

    PRETTY-PRINTING FILES

    enscript

    converts text file to PostScript, rtf or html

    default: PostScript

    sends output to printer

    Example:

    % enscript assign1.txt

    Options:

    -P to specify printer

    -w to select output language

    -o to specify output file11

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    12/24

    CHECKING PRINTING STATUS

    Syntax: lpq [options]

    Commonly used options:

    -P printer shows print jobs on specific printer-U user-id shows print jobs for specific user

    -l long format of listing

    -a shows print jobs on all printers

    Also: lprm to remove unwanted print job

    12

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    13/24

    OPERATIONS ON TEXT & OTHER FILES

    13

    CSCI330-

    TheUNIXSystem

    Other File

    Operations

    Combine

    contents

    Extract

    contents

    Compare

    files

    Count

    words

    Compress

    contentsSort

    Uniquelines Encrypt/decrypt

  • 8/10/2019 file-ops

    14/24

    COMBINING CONTENTS OF FILES

    Method 1: To vertically concatenate the contents of

    two or more files, use catwith 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

    14

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    15/24

    COMBINING CONTENTS OF FILES

    Method 2: To horizontally concatenate contents

    (columns/fields) of two or more files, usepaste

    Syntax: paste file-1 file-2 > all-file

    all-file will contain the combined contents of file-1

    and file-2 in side-by-side (horizontal) fashion

    15

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    16/24

    EXTRACTING CONTENTS OF FILES

    To extract one or more fields from a file, use cut

    fields are delimited by special character

    default: TAB, change viad option common: :

    must specify list of fields to be extracted

    option -f

    Example:

    % cut -d: -f 5 /etc/passwd

    16

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    17/24

    COMPARING FILES: COMM

    The command named comm can be used to

    compare lines that are common in two sortedfiles

    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

    17

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    18/24

    COMPARING FILES: DIFF

    The command diffcompares 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 thefirst file to the second file

    (via the patch command)

    18

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    19/24

    DETERMINING FILE SIZE

    Recall: The ls command with the option -l gives

    the file size in bytes

    Use 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

    19

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    20/24

    FILE COMPRESSION

    utilities to compress and uncompress files

    common on Linux:

    gzip, gunzip

    file extension: .gz

    Example:

    % gzip assign1.txt

    % gunzip assign1.txt.gz

    Gzip and gunzip delete their inputs

    So make a copy if you want one

    20

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    21/24

    COMPRESS FILE CONTENTS

    Bzip2

    New, better compression

    Old

    compress/uncompress (.Z)

    Windows-compatible

    zip/unzip (.zip)

    Do not delete their input!

    21

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    22/24

    SORTING FILES

    To sort a text file in ascending or descending order,

    use sort

    Syntax: sort [options] file-name

    Commonly used options:

    -r sort in reverse order

    -n numeric sort-t field delimiter

    -k field1[,field2]

    -f ignore case 22

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    23/24

    REMOVING REPEATED LINES

    uniq

    removes repeated 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 eachoutput line

    -d display the repeated lines

    -u display the lines that are not repeated23

    CSCI330-

    TheUNIXSystem

  • 8/10/2019 file-ops

    24/24

    USERS DISK QUOTA

    quota is upper limit of

    amount disk space

    number of files

    for each user account

    The command: quota -v

    displays the users disk usage and limits

    2 kinds of limits:

    Soft limit: ex. 10MBMay be exceeded for one week

    System will remind you when you log on Hard limit: ex. 12MB

    Cannot be exceeded

    24

    CSCI330-

    TheUNIXSystem