UNIX Tools and the UNIX File System CMSC

  • Slides: 20
Download presentation
UNIX Tools and the UNIX File System CMSC 121 Introduction to UNIX The material

UNIX Tools and the UNIX File System CMSC 121 Introduction to UNIX The material on submitexec was taken from Dan Hood’s CMSC 121 Lecture Notes.

UNIX File System: Links n Every file in the UNIX file system has an

UNIX File System: Links n Every file in the UNIX file system has an associated block of information called an i-node. n n The i-node contains such all information about a file, except its name. The i-node’s information includes: date created, user id, group id, file size, timestamps, the number of links, etc. It also contains the physical location of the file’s blocks on the disk. The i-node table maps the filename to the specific inode. ls –i will display the i-node for a file. As each file has its own i-node, there is no reason that more than one name can’t point to the same i-node in the table. This is the idea behind links.

UNIX File System: Links n n n The ln command creates two names to

UNIX File System: Links n n n The ln command creates two names to the same file. linux 2 [4]# touch temp linux 2 [5]# ln temp_link These are called “hard links” because they are of equal status. Type ls –i and compare the files’ inodes. A file isn’t actually deleted until all of the hard links to it have been removed. So, rm temp will not remove the file, only this one link to it. One important thing to note is that you cannot hard link across file systems, as i-node numbers are not unique across file systems. You also cannot hard link directories.

UNIX File System: Symbolic Links n n n A symbolic link is a sort

UNIX File System: Symbolic Links n n n A symbolic link is a sort of alias to a file. Symlinks have a much lower status than hard links. The ln -s command creates a symlink to a file. linux 2 [5]# ln –s temp_symlink ls –L will dereference symlinks to the actual files, instead of giving information about where symlinks point. Symlinks can work with directories and across file systems, as this is their main purpose. Note that some programs (e. g. tar) provide options for dealing with symlinks so that they do not break. n tar’s –h ensures that symlinks don’t break by saving the referenced files instead of the symlinks.

Wildcards allow a single command to act upon multiple files with a name that

Wildcards allow a single command to act upon multiple files with a name that matches a pattern. n Primarily, wildcards reduce typing and make commands more reusable. n * – Matches any sequence of characters. n ? – Matches any single character. n

[ ] - Match Single Character n n [ ] – Matches any single

[ ] - Match Single Character n n [ ] – Matches any single character included in the list. The list may contain ranges, specified by a dash. E. g. [a-z] An exclamation point ! matches any character not in the list. E. g. [!a-z] Example: linux 3 [6]# ls file. a file. cgi file. html file. lib file. tk file. z file. c file. h file. l file. pl file. txt n To list just the *. c and *. h files: linux 3 [8]# ls *. [c, h] file. c file. h n Or you can specify ranges of values, such as. . . linux 3 [10]# ls *. [a-z] file. a file. c file. h file. l file. z

{ } - Match Multiple Characters n n n We can also denote multiple

{ } - Match Multiple Characters n n n We can also denote multiple files by using the "{" and "}" characters. This is the multiple character version of the previous example. Given the following directory listing: linux 3 [11]# ls file. a file. cgi file. html file. lib file. tk file. z file. c file. h file. l file. pl file. txt To list just the *. cgi, *. pl and *. tk files: linux 3 [12]# ls *. {cgi, pl, tk} file. cgi file. pl file. tk This could be very helpful when trying to open up an entire project that consists of multiple files in emacs. For example: emacs *. {c, h, txt} &

UNIX Tools Philosophy Each tool should do one thing very well, nothing more and

UNIX Tools Philosophy Each tool should do one thing very well, nothing more and nothing less. n These tools should support chaining using streams. The output of one program should be considered to be the input to another program. n Use existing tools in combination to do very complex tasks instead of building specialized tools. n

xargs: chaining commands n xargs takes the output of one command passes it as

xargs: chaining commands n xargs takes the output of one command passes it as command line arguments to another command. find. -name “*. txt” | xargs grep John n n Usingle quotes may overflow the command line, so you shouldn’t use this form. grep John ‘find. -name “*. txt”’ Another use of xargs: Example: take all the files whose names are in the file “names” and concatenate their contents: xargs cat < names > contents

enscript: more flexible printing n n The enscript command converts ascii files to postscript

enscript: more flexible printing n n The enscript command converts ascii files to postscript and then spools the postscript file to the printer. It is a more flexible way of printing than lpr. enscript options: -G : a “gaudy” header with a timestamp, file name, and page numbers -r : rotate the page to a landscape orientation -2 : print in two columns -a : print only the specified pages -p : print to the specified file instead of the printer n Example: linux 2 [5]# enscript -2 Gr -p myfile. ps

Working with ps and pdf files n ps 2 pdf generates a pdf file

Working with ps and pdf files n ps 2 pdf generates a pdf file (Adobe Acrobat) from a ps (Postscript) file. linux 2 [10]# ps 2 pdf input. ps output. pdf n Viewers: gv, xpdf.

Working with text files ispell interactively spell checks a file. linux 2 [9]# ispell

Working with text files ispell interactively spell checks a file. linux 2 [9]# ispell filename. txt n cut extracts specific columns of data from input. n sed is an advanced tool for editing a stream. n awk is a powerful data manipulation tool. n

Working with graphics gimp is a powerful graphics software package. n convert changes images

Working with graphics gimp is a powerful graphics software package. n convert changes images from one format to another. n xv is an interactive image viewer. n

submitexec n n n Many people wish to do things like compile and run

submitexec n n n Many people wish to do things like compile and run projects once they are submitted. There have been many instances where a student has forgotten to submit a small part of a project, such as a header file, and loses a bunch of points for it not compiling. Dan Hood has written a script that allows you to execute arbitrary commands from within your submit directory. Some of courses provide such program, but many of them are written for a specific course and are compiled for a particular architecture (such as Linux and not IRIX or Solaris). So, Dan Hood has written a cross platform (works on any of the UNIX systems that we have here) script that you can use for any course. It is in his public directory: /afs/umbc. edu/users/d/h/dhood 2/pub/submit_utilities/ and the name of the script is called submitexec

submitexec n n n The usage for this script is similar to all of

submitexec n n n The usage for this script is similar to all of the other submit utilities. The first command line argument is the course, the second is the project, and then any remaining arguments are the command(s) that you want to execute. This is a very versatile script. You can do anything from an ls command to compiling and running your program where the grader will do so. I suggest making a copy of this script and placing it in your own bin directory. Note: some of you may have used submitmake or heard of it. This script is also capable of doing that as well. If you rename the script (or create a symbolic link) with the name submitmake it will act like the submitmake provided in other courses. Again, it is no longer limited to a particular course or a particular architecture.

submitexec linux 2 [3]# submit cs 331 proj 2 hello. c Submitting hello. c.

submitexec linux 2 [3]# submit cs 331 proj 2 hello. c Submitting hello. c. . . OK linux 2 [4]# submitexec cs 331 proj 2 ls Executing the command: ls hello. c Execution complete: exit status = 0 linux 2 [5]# submitexec cs 331 proj 2 ls –l Executing the command: ls –l total 1 -rw-r--r-- 1 dhood 2 general 78 Apr 20 10: 42 hello. c Execution complete: exit status = 0 linux 2 [6]# submitexec cs 331 proj 2 gcc -Wall -ansi hello. c Executing the command: gcc -Wall -ansi hello. c Execution complete: exit status = 0 linux 2 [7]# submitexec cs 331 proj 2 a. out Executing the command: a. out Hello World! Execution complete: exit status = 256

Common Programming Tools n n n gcc – the GNU compiler for C, C++,

Common Programming Tools n n n gcc – the GNU compiler for C, C++, and Objective-C. gdb – the GNU debugger. make – automates compilation and execution java/javac – the Java virtual machine and compiler An excellent guide to UNIX tools for the edit-compile -link-debug programming cycle: http: //cslibrary. stanford. edu/107/Unix. Programming. Tools. pdf.

indent: proper whitespace n indent formats C/C++ files by adjusting the whitespace to make

indent: proper whitespace n indent formats C/C++ files by adjusting the whitespace to make it easier to read. It is also useful for enforcing consistent formatting for large projects. linux 2 [8]# indent file. c –o file. out linux 2 [9]# indent –st file. c > file. out

make: automating compilation and execution n n make runs using the build instructions in

make: automating compilation and execution n n make runs using the build instructions in a file called “Makefile” or “makefile” in the working directory. The makefile includes variable definitions, and build rules for each component. Variable definitions facilitate future makefile modifications: CC = gcc CFLAGS = -g -I~/classes/cmsc 121/include A build rule consists of one line specifying dependency information and a second line specifying the command to build that component. proj 1. o : proj 1. c proj 1. h aux. h TAB$(CC) $(CFLAGS) –c proj 1. c

# Makefile PROJ = Proj 1 CC = CC CCFLAGS = -g SUBMITCLASS =

# Makefile PROJ = Proj 1 CC = CC CCFLAGS = -g SUBMITCLASS = cs 121_fall 05 PRINTPGM = /usr/lib/print/lptops PRINTFLAGS = -G -U -H -M 2 -O 0. 4 pt PRINTFILE = $(PROJ). ps clean: - rm -f *~ cleaner: - make clean; rm -f *. o cleanest: - make cleaner; rm -f $(PROJ) SOURCES = $(PROJ). C Aux 1. H Aux 1. C Aux 2. H Aux 2. C OBJECTS = Aux 1. o Aux 2. o $(PROJ): $(PROJ). C $(OBJECTS) $(CCFLAGS) -o $(PROJ). C $(OBJECTS) Aux 1. o: Aux 1. C Aux 1. H $(CC) $(CCFLAGS) -c Aux 1. C Aux 2. o: Aux 2. C Aux 2. H $(CC) $(CCFLAGS) -c Aux 2. C print: $(SOURCES) - $(PRINTPGM) $(PRINTFLAGS) $(SOURCES) Makefile > $(PRINTFILE) submit: submit $(SUBMITCLASS) $(PROJ) $(SOURCES) Makefile