• Aucun résultat trouvé

Chapter Four: UNIX/Linux File Processing

N/A
N/A
Protected

Academic year: 2022

Partager "Chapter Four: UNIX/Linux File Processing"

Copied!
26
0
0

Texte intégral

(1)

Chapter Four:

UNIX/Linux File Processing

Guide To UNIX Using Linux

(2)

Guide to UNIX Using Linux, Third Edition 2

Objectives

• Explain UNIX and Linux file processing

• Use basic file manipulation commands to create, delete, copy, and move files and directories

• Employ commands to combine, cut, paste, rearrange, and sort information in files

(3)

Objectives (continued)

• Create a script file

• Use the join command to link files using a common field

• Use the awk command to create a professional- looking report

(4)

Guide to UNIX Using Linux, Third Edition 4

UNIX and Linux File Processing

• Based on the approach that files should be treated as nothing more than character sequences

• Because you can directly access each character, you can perform a range of editing tasks – this offers flexibility in terms of file manipulation

(5)

Reviewing UNIX/Linux File Types

• Regular files, also known as ordinary files

– Create information that you maintain and manipulate, and include ASCII and binary files

• Directories

– System files for maintaining file system structure

(6)

Guide to UNIX Using Linux, Third Edition 6

Reviewing UNIX/Linux File Types (continued)

• Special files

– Character special files relate to serial I/O devices – Block special files relate to devices such as disks

(7)

Understanding File Structures

• Files can be structured in many ways depending on the kind of data they store

• UNIX/Linux store data, such as letters and product records, as flat ASCII files

• Three kinds of regular files are – Unstructured ASCII character – Unstructured ASCII records – Unstructured ASCII trees

(8)

Guide to UNIX Using Linux, Third Edition 8

Understanding File Structures

(continued)

(9)

Processing Files

• UNIX/Linux processes commands by receiving

input from a standard input device (e.g. keyboard) and sending it to a standard output device (e.g.

monitor)

• System administrators and programmers refer to standard input as stdin, standard output as stdout

• When UNIX/Linux detect errors, they send data to standard error (stderr, the monitor)

(10)

Guide to UNIX Using Linux, Third Edition 10

Using Input and Error Redirection

• You can use redirection operators to retrieve input from something other than the standard input device and send output to something other than the standard output

device

• Examples of redirection

– Redirect the ls command output to a file, instead of to the monitor (or screen)

– Redirect a program that receives input from the keyboard to receive input from a file instead

– Redirect error messages to files, instead of to the screen by default

(11)

Manipulating Files

• When you manipulate files, you work with the files themselves, as well as their contents

• Create files using output redirection

– cat command - concatenate text via output redirection – without a command - > filename

– touch command - creates empty files

(12)

Guide to UNIX Using Linux, Third Edition 12

Manipulating Files (continued)

• Delete files when no longer needed

– rm command - permanently removes a file or an empty directory

– The -r option of the rm command will remove a directory and everything it contains

• Copy files as a means of back-up or as a means to assist with new file creation

– cp command - copies the file(s) specified by the source path to the location specified by the

destination path

(13)

Manipulating Files (continued)

• Move files from directory to directory

– mv command - removes file from one directory and places it in another

• Finding a file helps you locate it in the directory structure

– find command - searches for the file that has the name you specify

(14)

Guide to UNIX Using Linux, Third Edition 14

Manipulating Files (continued)

• Combining files using output redirection – cat command - concatenate text of two

different files via output redirection

– paste command - joins text of different files in side by side fashion

(15)

Manipulating Files (continued)

The paste command joins text of different files in side by

side fashion

(16)

Guide to UNIX Using Linux, Third Edition 16

Manipulating Files (continued)

Extracting fields of a file using output redirection: the cut command

removes specific columns or fields from a file

(17)

Manipulating Files (continued)

• Sorting the contents of a file

– sort command - sorts a file’s contents alphabetically or numerically

– the sort command offers many options:

• You can sort the contents of a file and redirect the output to another file

• Utilizing a sort key provides the option of

(18)

Guide to UNIX Using Linux, Third Edition 18

Manipulating Files (continued)

(19)

Creating Script Files

• UNIX/Linux users create shell script files to contain commands that can be run sequentially as a set – this helps with the issues of command automation and re-use of command actions

• UNIX/Linux users use the vi editor to create script files, then make the script executable using the chmod command with the x argument

(20)

Guide to UNIX Using Linux, Third Edition 20

Creating Script Files (continued)

(21)

Using the join Command on Two Files

• Sometimes you want to link the information in two files

• The join command is often used in relational database processing

• The join command associates information in two different files on the basis of a common field or key in those files

(22)

Guide to UNIX Using Linux, Third Edition 22

A Brief Introduction to the Awk Program

• Awk, a pattern-scanning and processing language helps to produce professional-looking reports

• Awk provides a powerful programming environment that can perform actions on files that are difficult to duplicate with a combination of other commands

(23)

A Brief Introduction to the Awk Program (continued)

• Awk checks to see if the input records in specified files satisfy a pattern

• If so, awk executes a specified action

• If no pattern is provided, awk applies the action to every record

(24)

Guide to UNIX Using Linux, Third Edition 24

Chapter Summary

• UNIX/Linux supports regular files, directories, and character and block special files

• File structures depend on data being stored

• UNIX/Linux receives input from the standard input device (keyboard, stdin) and sends output to the standard output device (monitor, stdout)

(25)

Chapter Summary (continued)

• touch updates a file’s time and date stamps and creates empty files

• rmdir removes empty directories

• cut extracts specific columns or fields from a file

• paste combines two or more files

• sort sorts a file’s contents

(26)

Guide to UNIX Using Linux, Third Edition 26

Chapter Summary (continued)

• To automate command processing, include commands in a script file

• join extracts data from two files sharing a common field and uses this field to join the two files

• Awk is a pattern-scanning and processing

language useful for creating a formatted report with a professional look

Références

Documents relatifs

Create user file directories and subfile directories Condition: Used by operator to create UFOs DELFIL. Delete files without

cat > file – places standard input into file more file – output the contents of file head file – output the first 10 lines of file tail file – output the last 10 lines of file

cat file1 file2 > files Combine file1 and file2 into files cat file1 >> file2 Append file1 at the end of file2 chgrp [options] ..

nyy or nY Places n lines in the buffer–copies; omitting n copies only the current line to the buffer!. ycursor_cmd Copies from cursor to cursor_cmd (e.g., yG copies current line

In a multitable clustering file organization records of several different relations can be stored in the same file.  Motivation: store related records on the same block to

 Level 3 is not used anymore since bit-striping forces single block reads to access all disks, wasting disk arm movement, which block striping (level 5) avoids.  Level 6 is

Chapter 5 goes in depth into tools and utilities used to create and manage file systems and partitions on Linux systems. Chapter 6 covers file management including the various

Useful to make sure files are identical, DIR Sorted current or declared directory files with sizes and disk space used and remaining are displayed on console with this command..