CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on...

29
CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables

Transcript of CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on...

Page 1: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

CS240 Computer Science II

Introduction the Unix File System and

File Related Utilities

Based on “UNIX for Programmers and Users” by G.Class and K. Ables

Page 2: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Obtaining an account

Obtaining username (login name) and password from the instructor

• Username: the department usually your last name and the first character of your first name as your login name. For example, doej is the username for Doe, John

• Password: your social security number (9 digits without blanks or hyphens. The password can be changed with the passwd command.

Page 3: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Logging in• telnet cs.wpunj.edu• At the prompts, enter

– Enter username– Enter password

• If your are accessing the system from your home (assume you have internet access)– Connect to the internet– Click Start button on the task bar (Window)– Select and click “Run” command– Type telnet cs.wpunj.edu– Enter username and password

Page 4: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Unix shells and shell commands

• Shell is an interface between the user and the kernel of the Unix OS

• The shell usually displays a prompt character $ or % after you logged in.

• There are three popular shells: – The Bourne shell– The Korn shell (a superset of the Bourne shell)– And the C shell

• All shells share a same set of core functionality together with some specialized properties.

Page 5: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Programmability with shells

• Each shell has its own programming languages which are tailored to manipulating files and processes in the Unix system.

• Shell programming are covered in later chapters of the book.

Page 6: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Running a utility program

• Recalling from previous lecture, standard Unix comes with at least 200 utility programs including, compilers, text processing tools, spreadsheets, desk-top publishing, sorting utility, some shells programs, graphical user interface (GUI), etc.

• To run a utility program, simply enter the name of the program and press enter key, e.g.,

$ date

displays date and time. Notice that the names are case-sensitive!

Page 7: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Some frequently used utilities and their basic functions

• man - displays manual pages

• stty - displays terminal characteristics

• passwd – changing password

• pwd – displays current directory

• vi, pico, emacs – starts the vi or emacs editor

• cat, more, page, head, or tail – displays the contents of a file

• ls – lists all of the files in the current working direct Rory in alphabetical order

• mv – renaming or moving a file

• mail and xmail – allows one to send and read e-mails

Page 8: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Some frequently used utilities and their basic functions

• mkdir – creates a new directory• rmdir – removes an existing directory• cd – changes to a different directory• cp – copies a file• rm – deletes a file• lp, lpstat, cancel – relates to file printing• wc – counts # lines, words, and/or characters in a file• file – attempts to describe the contents of the file• chmod – allows the user to control the accessibility of a file• group – displays a list of groups that you are a member of

Page 9: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Unix file structure: directory, subdirectory, relative path name, absolute path name

• Hierarchical (inverted tree) structure• Root directory: /• Working directory: your default login

directory (pwd displays it)• Absolute pathname that specifies the

location of a file: traces all the way from the root directory to where the file is located.

• Relative pathname: pathname relative to the current working directory.

Page 10: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

A brief summary of file manipulating utilities

Page 11: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

A brief summary of file manipulating utilities

Page 12: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

The ls utility and file attributes

Page 13: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

The chmod and the file permissions

• P42/43/46/47

Page 14: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Some important key strokes

• ^ means holding down the Ctrl key

• ^S – freezes the scrolling of screen display

• ^Q – resumes the scrolling of screen display

• ^D – signifies to the Unix the end of input (e.g., after you finished typing your e-mail)

• ^D – if entered at a shell prompt such as $, it requests the Unix the end the session.

Page 15: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

The vi (visual) Full-Page Editor

• Two mode– The command mode: allow command to be

entered– Text-entry or input mode: enter text (e.g., C++

program instructions)

Page 16: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Common vi editing features

• Cursor movement

• Deleting text

• Replacing text

• Pasting text

• Searching/replacing text

• Saving/loading files

• miscellaneous

Page 17: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Cursor movement commands

• p58

Page 18: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

vi text deleting commands

• p58

Page 19: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

From command mode to text-entry mode

• When vi is started, it enters into the command mode.

• To switch to the text-entry mode enter any of the following keys (p55):

Page 20: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

From text-entry mode to command mode

• Simply press the esc key

Page 21: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Modes in vi: a diagram

Page 22: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

C/C++ Programming Tools

Common tools• C/C++ compiler(s)• The make utility• Source code management systems

– sccs (source code control system)– rcs (revision control system)

• The debugging utilities– lint– Adb, sdb, and debug

Page 23: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

How to create, compile, and execute a C/C++ program

• Use an editor such as vi to create and save a program

• Use a compiler to compile a program$ cc example1.c or $ g++ example2.cpp

• After successful compilation, the linker creates an executable file a.out

• To run the executable file, simply type a.out at the command line

Page 24: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Compilation options• -l (lower-case L) to specify the libraries

$ cc calc.c –lm (uses abbr for library name; m stands for libm.a or math library)

• -O causes the compiler to use the optimizer• -c suppresses the link-edit process (useful to test an individual

module)• -o gives the executable (by default a.out) the name of your

choice$ cc –o example calc.c (example replaces a.out as the name of the executable file. Note that without using –o option, you can always use mv to rename a.out as in $ mv a.out example)

Page 25: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Multiple file compilation

• If there is only one object file, cc removes it after it successfully creates the a.out. But if there are multiple object files (with .o extensions), all object files are retained.$ cc ledger.c acctspay accstrec.c (3 source files)$ lsa.out acctspay.o acctsrec.o ledger.oacctspay.c acctsrec.c ledger.c

Page 26: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

More on header files and the #include preprocessor directive

• When several symbolic constants, macros, and class definitions are used in different modules of a program, they are typically collected together in a single file called header file (or include file). The angle bracket (< and >) instructs the preprocessor to look for the header file in a standard directory (/usr/include on most systems). If you want to include header file in another directory, use double quotes and specify the appropriate pathname of the file such as #include “/home/alex/cs240/myheader.h”

Page 27: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Using lint to find errors

• lint is one of the most useful debugging tools. It checks programs for potential bugs and portability problems. It it much more strict than C/C++ compiler. The following line shows the syntax of how it is used.$ lint example.c The above line will print warning and errors in the program named example.c.

Page 28: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

The standard input/output devices

• Standard input: keyboard• Standard output: monitor• Device drivers (files) are stored in /dev directory , which

allow devices to be treated as files.• By default, keyboard and monitor are assumed as the

standard I/O devices, e.g., $ a.outruns the executable file a.out. If a.out requires input, the input will come from the keyboard; if a.out produces output, the output will be displayed on the screen.

Page 29: CS240 Computer Science II Introduction the Unix File System and File Related Utilities Based on “UNIX for Programmers and Users” by G.Class and K. Ables.

Input and output redirection

• > means redirecting the output to• < means obtaining the input fromFor example:$ a.out < indata (reads data from indata file and displays

results on the screen)$ a.out > outresult (reads data from the keyboard and sends

results to outresult file)$ a.out < indata > outresult (reads data from indata and

displays results on the screen)• Note that a.out can be replaced by any executable

command or utility program.