250 likes | 399 Views
Chapter 4 File Structure. Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert Koretsky, Syed A. Sarwar, 2005 Addison Wesley. expanded by Jozef Goetz, 2009. Objectives. You may ignore last slides 19 – 24
E N D
Chapter 4 File Structure Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert Koretsky, Syed A. Sarwar, 2005 Addison Wesley expanded by Jozef Goetz, 2009
Objectives You may ignore last slides 19 – 24 • To explain the UNIX file concept • To discuss various types of files supported by UNIC • To describe attributes of a file • To explain the notion of pathnames • To explain the user view of the UNIX file system • To describe the user’s interface to the UNIX file system - browsing the file system • To discuss representation of a file inside the UNIX system • To describe how a UNIX file is stored on the disk • To explain the concept of standard files in UNIX • To cover the commands and primitives ~ , . , .. , / , PATH, cd, echo, file, ls, mkdir , pwd , rmdir
7 Types of Files • Simple/ordinary file • Directory • Symbolic (soft) link • Special (device) files • block special files and character special files • Named pipe (FIFO) • Socket
1. Types of Files Simple/ Ordinary File UNIX doesn’t impose any naming conventions on files of any types File names – 14 letters in System V - 255 letters in BSD
Types of Files2. Directory • Inode number is 4 bytes and is an index value for an array on the disk. • An element of this array is an index node (inode) contains such as file size. • UNIX kernel allocates an inode whenever a new file is created • [cs253u@shell cs253u]$ ls -i 5671882 myFile • Where 5671882 is an index number
Types of Files 3. Symbolic Link • Link File • A Link File is created by the system when a symbolic link in created to an existing file. • it allows you to rename an existing file and share itwithout duplicating its contents
4. Special (Device) File • A special File is a means of accessing hardware devices, including the keyboard, hard disk, CD-ROM drive, tape drive and printer. • Character Special Files • Correspond to character-oriented devices (e.g., Keyboard) • Block Special Files • Correspond to block-oriented devices (e.g., a disk)
4. Special (Device) File • /dev directory contains at least 1 file for every device connected to the computer e.g, • fd0 – for floppy drive 0 • hda - for hard disk drive a • lp0 - for line printer 0 • tty – for terminal • applications and cmds read/write peripheral device files in the sameway that read/write an ordinary file
5.Named Pipe (FIFO) 6. Sockets • Tools that enable processes to communicate with each other • client-server paradigm called interprocess communication mechanism – IPC • A FIFO is a file that allows 2 processes to communicate with each other, if Ps are on the same computer • A socketallows 2 processes to communicate with each other, if Ps are on the different computer • On System V UNIX system, a socket file type means a UNIX domain socket
File System Structure • File System Organization – => • Home and Present Working Directories • $HOME, ~, $home - HomeWorking Directory • pwd - Present Working Directory • . – current working directory • .. – parent of the present working directory • Pathnames: • Absolute – starting from the root • Relative - starting from the presentworking dir • Multiple disk drives: • files mounted on the samefile system structure can be accessed as if they are part of the file structure one disk • Some Standard Directories and Files • Root directory (/), • /bin, /dev, /etc, /lib…
Standard Directories and Files • Some Standard Directories and Files • / - Root directory • /bin – binary files • /dev – device files • /etc – cmds and files for system administration • inetd.conf, login, printcap, rc.d, services, termcap,… • passwd –a contains info for users, each line (7 fields) is as follows: login_name:password:user_ID:user_info: home_directory:login_shell • From /etc use cmd: cat passwd or cat /etc/passwd • /lib – all essential lib • /users – hold all the users (home directories) • /usr – utilities, tools, language lib, and manual pages • /bin - binary images of utilities, tools etc. • /lib - language lib • /var – otherdirectoriesand /spool/mail that receives and holds incoming –emails • /unix or /kernel – binary image of UNIX kernel loaded into memory at bootup time
Navigating the File Structure • Determining the Absolute Pathname for Your Home Directory • echo [string] • echo $HOME • Browsing the File System • cd [directory] • ls [options] [pathname-list]
Navigating the File Structure [cs253u@shell cs253u]$ ls -ls * // display the content of each directory from the current one // -l use a long listing format // -s, --size print size of each file, in blocks – 1st number at the column // -S sort by file size 4 -rwxrw-r-- 1 cs253u cs253u 18 Apr 19 11:24 case_demo 4 -rwxrw-r-- 1 cs253u cs253u 155 Apr 26 16:23 d 4 -rwxrw-r-- 1 cs253u cs253u 280 Apr 18 22:48 ex_add_all_ch16 4 -rwxrw-r-- 1 cs253u cs253u 1174 Apr 19 17:20 ex_add_param_my 4 -rwxrwxrwx 1 cs253u cs253u 114 Mar 31 00:31 ex_input 4 -rwxrw-r-- 1 cs253u cs253u 425 Apr 19 14:21 ex_nslookup_lab10_ex6 4 -rwxr-xr-x 1 cs253u cs253u 27 Apr 12 09:48 ex_simple 4 -rwxrw-r-- 1 cs253u cs253u 466 Apr 18 23:11 ex_sum_nat_#_ch16 4 -rw-rw-r-- 1 cs253u cs253u 269 Apr 18 18:47 ex_while 4 -rwxrw-r-- 1 cs253u cs253u 269 Apr 12 01:16 ex_while1 0 -rw-rw-r-- 1 cs253u cs253u 0 Apr 12 15:24 sort mail: total 8 4 -rwx------ 1 cs253u cs253u 505 Feb 9 17:03 saved-messages 4 -rwx------ 1 cs253u cs253u 918 Feb 9 17:05 sent-mail personal: total 0 professional: total 0 temp: total 0 [cs253u@shell cs253u]$
Navigating the File Structure • Creating Files • pico, vi, emacs • Creating and Removing Directories • mkdir [options] dirnames, • rmdir [options] dirnames • Determining File Attributes ls –l, ls –i, ls –al, ls –F, ls -RC • -F, --classify = append indicator (one of */=@|) to entries; @ - symbolic link, unix* - executable • -R, --recursive = list subdirectories recursively • -C list entries by columns ls -RC .: dead.letter m mailmydir myPATH new1 ./mail: saved-messages sent-mail ./mydir:
Navigating the File Structure drwxrwxrwx 1 sarwar faculty 512 May 12 23:44 solutions
Navigating the File Structure • $ ls -i • 12329 courses 22876 memos 12487 personal • $ ls -al ~/courses/ee446/exams • drwxr-x-- 1 sarwar faculty 512 Mar 16 08:24 . • drwxr-x-- 1 sarwar faculty 512 Jan 29 13:27 .. • -rwxr--r-- 1 sarwar faculty 1863 Mar 16 11:10 mid1 • -rwxr--r-- 1 sarwar faculty 459 Apr 11 14:34 mid22 • drwxrwxrwx 1 sarwar faculty 512 May 12 23:44 solutions • $ ls -F / • bin/ dev/ etc/ install@ lost+found/ tmp/ usr/ unix* • install@ - symbolic link • unix* - executable • $ ls -l ~/courses/ee446/lab[^5]*.c • [^5] – cannot be 5 • $ ls ~/[^0-9]*.[c,C] • - not start with a digit and end with c. or C.
Determining the Type of a File’s Contents • file [options] file-list $ file * - all files in pwd $ file /* in root all.backup: POSIX tar archive bin: directory dev: directory etc: directory install: symbolic link to var/lib/LST lost+found: directory tmp: directory usr: directory unix: ELF 32-bit LSB executable $
File Representation and Storage in UNIX • attributes of a file are stored in a • data structure on the disk , called inode:
File Representation and Storage in UNIX • Link Count - # of different names the file has within the system • File Mode – what the file was open for read, write, etc • User ID – the owner of the file • Access Permissions – who can access the file • File’s Location on Disk – a direct or indirect pointers to the disk • blocks containing file data use of id # to index the inode table UNIX kernel maps the inode of the file lab1.c to its contents on disk
Standard Files and File Descriptors To open and the issue a file I/O operations: read, write, seek, etc. UNIX automatically opens 3 files for every cmd it executes: sdin, stout and sderr. File descriptor: a small integer that the UNIX kernel attaches with every open file standard input (sdin) — file descriptor 0 standard output (stout) — file descriptor1 standard error (sderr) — file descriptor2 uses to index uses to index
Standard Files and File Descriptors • The input, output and errors of a command • can be redirected to other files by using • file redirection facilitiesin UNIX • Here the sdininput – default keyboard • stout output – default display screen • sderr error – default display screen
Standard Files and File Descriptors • The input, output and errors of a command • can be redirected to other files by using • file redirection facilitiesin UNIX • Here the sdininput 0– default keyboard • stout output 1 – default display screen • sderr error 2 – was redirected to lab1.c • The other descriptors usually • range from 3 through 19 • are called user-defined file descriptors.