CSCI 330 UNIX and Network Programming Unit II

  • Slides: 31
Download presentation
CSCI 330 UNIX and Network Programming Unit II Basic UNIX Usage: File System

CSCI 330 UNIX and Network Programming Unit II Basic UNIX Usage: File System

CSCI 330 - UNIX and Network Programming UNIX Shell: traditional user interface • “in

CSCI 330 - UNIX and Network Programming UNIX Shell: traditional user interface • “in the terminal at the command line” • via “Terminal Emulator” • via “putty” Features: • interprets and executes commands • remembers command history and allows editing • allows command submission as background jobs 2

CSCI 330 - UNIX and Network Programming 3 Command Line Structure % command Command

CSCI 330 - UNIX and Network Programming 3 Command Line Structure % command Command prompt • • Command name [-options] [arguments] Command modifier: usually one character preceded by + or - sign Arguments can be: 1. names of files 2. more information UNIX is case sensitive Must be a space between command, options and arguments No space between the plus or minus sign and option letter Fields enclosed in [ ] are optional

CSCI 330 - UNIX and Network Programming Command Line Example % sort list %

CSCI 330 - UNIX and Network Programming Command Line Example % sort list % sort -r list % sort -o sorted list Command name Command option Option argument Command argument 4

CSCI 330 - UNIX and Network Programming Some Basic Commands passwd ls more logout

CSCI 330 - UNIX and Network Programming Some Basic Commands passwd ls more logout - change password - list files - show content of file, page by page - logout from system date who clear - display date and time - display who is on the system - clear terminal screen man - find and display system manual pages 5

CSCI 330 - UNIX and Network Programming 6 RTFM: The man Command • show

CSCI 330 - UNIX and Network Programming 6 RTFM: The man Command • show pages from system manual Syntax: man [options] [-S section] command-name % % man man date -k date crontab -S 5 crontab Caveats: Some commands are aliases Some commands are part of shell Section Description 1 User commands 2 System calls 3 C library functions 4 Special system files 5 File formats 6 Games 7 Miscellaneous features 8 System administration utilities

CSCI 330 - UNIX and Network Programming The Unix file system • hierarchical organization

CSCI 330 - UNIX and Network Programming The Unix file system • hierarchical organization of files • contains directories and files • always single tree • basic commands to list and manipulate files • independent of physical file system organization • typical Unix file system types • ext 4 (formerly ext 2, ext 3) • Reiser 4, UFS (… HFS+), JFS, … • also: vfat, ntfs 7

CSCI 330 - UNIX and Network Programming Directory terminology • Root Directory: / •

CSCI 330 - UNIX and Network Programming Directory terminology • Root Directory: / • top-most directory in any UNIX file structure • Home Directory: ~ • directory owned by a user • default location when user logs in • Current Directory: . • default location for working with files • Parent Directory: . . • directory immediately above the current directory 8

CSCI 330 - UNIX and Network Programming Path • path: list of names separated

CSCI 330 - UNIX and Network Programming Path • path: list of names separated by “/” • Absolute Path • Traces a path from root to a file or a directory • Always begins with the root (/) directory Example: /home/student/Desktop/assign 1. txt • Relative Path • Traces a path from the current directory • No initial forward slash (/) • dot (. ) refers to current directory • two dots (. . ) refers to one level up in directory hierarchy Example: Desktop/assign 1. txt 9

CSCI 330 - UNIX and Network Programming 10 Path to file 3 Absolute Path:

CSCI 330 - UNIX and Network Programming 10 Path to file 3 Absolute Path: /usr/staff/joan/file 3

CSCI 330 - UNIX and Network Programming Directory commands • cd to show path

CSCI 330 - UNIX and Network Programming Directory commands • cd to show path of current working directory to change the current working directory • mkdir to create a new directory • pwd • rmdir to delete an empty directory • use “rm -r” to remove non-empty directory 11

CSCI 330 - UNIX and Network Programming List directory content • most frequently used

CSCI 330 - UNIX and Network Programming List directory content • most frequently used file system command: Syntax: ls [options] [path] • common options: -a -l -t -S -r show all files show long version of listing show files sorted by time stamp show files sorted by file size show files in reverse sorted order 12

CSCI 330 - UNIX and Network Programming Long List Option List contents of the

CSCI 330 - UNIX and Network Programming Long List Option List contents of the current directory in long format % ls -al total 126. is current directory drwxr-xr-x 13 ege csci 1024 Apr 26 15: 49. . . is parent directory drwxr-xr-x 15 root 512 Apr 24 15: 18. . -rwxr--r-- 1 ege csci 885 Dec 2 13: 07. login dot (. ) names are -rwx------ 1 ege csci 436 Apr 12 11: 59. profile hidden files drwx------ 7 ege csci 512 May 17 14: 11 330 drwx------ 3 ege csci 512 Mar 19 13: 31 467 directories drwx------ 2 ege csci 512 Mar 31 10: 16 Data -rw-r--r-- 1 ege csci 80 Feb 27 12: 23 quiz. txt plain file 13

CSCI 330 - UNIX and Network Programming File System Commands • Copying files or

CSCI 330 - UNIX and Network Programming File System Commands • Copying files or directories Syntax: cp source(s) target • source(s) is one or more items to copy • target is either name of copied item, or directory • commonly used options: -i -r -p if “target” exists, then prompts for confirmation before overwriting recursively copy entire directories preserve access times and permission modes • Moving files or directories Syntax: mv source(s) target 14

CSCI 330 - UNIX and Network Programming Renaming files or directories • use “mv”

CSCI 330 - UNIX and Network Programming Renaming files or directories • use “mv” Example: rename file “unix” to “csci 330” % mv unix csci 330 Caveat: what happens if “csci 330” exists and is a directory ? 15

CSCI 330 - UNIX and Network Programming Deleting files or directories Syntax: rm path-list

CSCI 330 - UNIX and Network Programming Deleting files or directories Syntax: rm path-list • Commonly used options: -f force remove regardless of permissions -i prompt for confirmation before removing -r removes everything under the indicated directory Example: remove file “old-assign” % rm unix/assign/old-assign 16

CSCI 330 - UNIX and Network Programming Linking Files • Allows one file to

CSCI 330 - UNIX and Network Programming Linking Files • Allows one file to be known by different names • Link is a reference to a file stored elsewhere • 2 types: • Hard link (default) • Symbolic link (a. k. a. “soft link”) Syntax: ln [-s] target local 17

CSCI 330 - UNIX and Network Programming 18 Link illustration home • create entry

CSCI 330 - UNIX and Network Programming 18 Link illustration home • create entry “bb” in “dir 3” as link to student dir 1 dir 2 aa dir 3 bb file “aa” in “dir 1” % cd /home/student/dir 2/dir 3 % ln /home/student/dir 1/aa bb

CSCI 330 - UNIX and Network Programming File System Layout • file consists of:

CSCI 330 - UNIX and Network Programming File System Layout • file consists of: • data blocks: identified by block numbers • file meta information: inode • contains which blocks make up file, owner, permissions, etc. • stored in inode table • index into table is inode number • directory is table of: • file name -> inode number 19

CSCI 330 - UNIX and Network Programming Hard Link example: ln Contents of dir

CSCI 330 - UNIX and Network Programming Hard Link example: ln Contents of dir 1 home student dir 1 dir 2 aa dir 3 bb . . . 1076 aa 2407 2083 Contents of dir 3 . . . 1070 bb 2407 2050 inode. table 2406 2407 2408 . disk. blocks 20

CSCI 330 - UNIX and Network Programming Symbolic Link example: ln -s Contents of

CSCI 330 - UNIX and Network Programming Symbolic Link example: ln -s Contents of dir 1 home student dir 1 dir 2 aa dir 3 bb . . . 1076 aa 2407 inode. table 2598 2599 2600 2083 Contents of dir 3 . . . 1070 bb 2599 2050 . disk. blocks / / nt e ud t s e/ 1/aa m r ho di 21

CSCI 330 - UNIX and Network Programming Link type comparison Hard Link Symbolic Link

CSCI 330 - UNIX and Network Programming Link type comparison Hard Link Symbolic Link • Checks for the existence of the original file • The original file continues to exist as long as at least one directory contains it • Created without checking the existence of the original file • Cannot access the original file if its path has restricted permissions • Can cross physical file systems • Cannot link to a file in a different file system • Can be circular linked to another symbolic linked file 22

CSCI 330 - UNIX and Network Programming Locating Files: find Syntax: find path-list expression(s)

CSCI 330 - UNIX and Network Programming Locating Files: find Syntax: find path-list expression(s) • “find” recursively descends through directories in path-list and applies expression to every file Examples: • find. -name “*. txt” • find /tmp -empty -delete 23

CSCI 330 - UNIX and Network Programming Many more commands … • touch updates

CSCI 330 - UNIX and Network Programming Many more commands … • touch updates time stamp on file or directory can be used to create new, empty file • cat displays content of file(s) • more, less or pg display the contents of file(s) one page at a time • Space bar – to advance to next page • b – to go back a page • Enter Key – to advance to next line 24

CSCI 330 - UNIX and Network Programming … more commands … • head displays

CSCI 330 - UNIX and Network Programming … more commands … • head displays the beginning portion of indicated file(s); the default head size is 10 lines. • tail displays the ending portion of indicated file(s) • wc display the size of files common options: -l display the number of lines -w display the number of words -c display the number of characters 25

CSCI 330 - UNIX and Network Programming 26 Comparing Files: diff • compare two

CSCI 330 - UNIX and Network Programming 26 Comparing Files: diff • compare two files line by line Syntax: diff [options] file-1 file-2 • reports a series of commands that can be used to convert the first file to the second file via the “patch” command

CSCI 330 - UNIX and Network Programming Compress File Contents • utilities to compress

CSCI 330 - UNIX and Network Programming Compress File Contents • utilities to compress and uncompress files • common on Linux: • gzip, gunzip, zcat • file extension: . gz • Example: % gzip assign 1. txt % zcat assign 1. txt. gz % gunzip assign 1. txt. gz • Also: • bzip 2 better compression • Windows compatible: zip/unzip 27

CSCI 330 - UNIX and Network Programming Sorting Files Syntax: sort [options] • Commonly

CSCI 330 - UNIX and Network Programming Sorting Files Syntax: sort [options] • Commonly used options: -r sort in reverse order -n numeric sort -t field delimiter (default: blank) -k field 1[, field 2] -f ignore case file-name 28

CSCI 330 - UNIX and Network Programming User’s Disk Quota • quota is upper

CSCI 330 - UNIX and Network Programming User’s Disk Quota • quota is upper limit of • amount disk space • number of files for each user account • command: quota -v • displays the user’s disk usage and limits • 2 kinds of limits: • Soft limit: ex. 100 MB • Maybe exceeded for one week • System will nag • Hard limit: ex. 120 MB • Cannot be exceeded 29

CSCI 330 - UNIX and Network Programming Sending Files to the Printer Syntax: lpr

CSCI 330 - UNIX and Network Programming Sending Files to the Printer Syntax: lpr • option [options] file –P to specify printer: lpcsl (others: lpfrl) Example: % lpr –P lpcsl assign 1. cc • other commands: • lpq show print job queue • lprm remove job from queue 30

CSCI 330 - UNIX and Network Programming Summary • shell is traditional command line

CSCI 330 - UNIX and Network Programming Summary • shell is traditional command line user interface • hierarchical organization of files • basic commands to list and manipulate files 31