1 / 43

File System of Unix

File System of Unix. References. “ UNIX&Linux Shell 設計程式篇” 第一章 請參考 pages 18 - 32 “ Practical UNIX Programming - A Guide to Concurrency, Communication, and Multithreading” : Chapter 3 Files. Online. Introduction of File System Original File Some important Shell Commands.

chesmu
Download Presentation

File System of Unix

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File SystemofUnix

  2. References • “UNIX&Linux Shell 設計程式篇” 第一章 • 請參考 pages 18 - 32 • “Practical UNIX Programming - A Guide to Concurrency, Communication, and Multithreading”:Chapter 3 Files

  3. Online • Introduction of File System • Original File • Some important Shell Commands

  4. Introduction of File System

  5. An Example of Files • ls -la -rw-r----- 1 wrlai 980 Mar 23 12:00 .cshrc -rw------ 1 wrlai 2302 Sep 10 16:32 .login -rw----r-- 1 wrlai 2354 Feb 22 10:35 .mailrc --x--x--- 1 wrlai 3302 Jan 14 16:32 makefile -rw-rw--- 1 wrlai 3302 Jan 14 16:32 myfile -rw-r--r-- 1 wrlai 3880 Feb 12 10:00 abc drwxr-xr-x 1 wrlai 880 Feb 12 10:00 test drwxr-xr-x 1 wrlai 880 Feb 12 10:00 project

  6. File System • Files • Ordinary files (-) • Directory files(d) • Special Files:socket(s)、names pipe(p)、symbolic link(l)、character device(terminal)(c)、block device(disk)(b) • Hierarchical File System • Tree-like structure • Each node presents a file

  7. The Tree Structure wrlai abc myfile .cshrc .mailrc project Cprogram proj1 proj2 .login t1.cc t2.cc h1* a.out file1.cc

  8. Current/ Home Directory The directory you are working in, is called workingdirectory or currentdirectory. </home1/staff/wrlai>cd Cprogram </home1/staff/wrlai/Cprogram> When you are first log in, the working directory is your home directory. Use 〜 to present the home directory </usr/bin>cd 〜 </home1/staff/wrlai>

  9. cd • cd 用於改變 shell 的 working directory • cd 是 shell 的內建命令(不會執行fork 和 exec) • Child shell 用 cd 改變目錄不會影響 Parent shell 的目錄 • Example: </usr/home/wrlai>cd / (改變目錄到 /) </>pwd (顯示目前所在的目錄位置) / </>

  10. Original Files

  11. Files • Every file has a filename. • No two files in the same directory can have the same name. • A full (absolute) pathname is its filename and the complete path. Ex: /usr/prof/alex/project/assign1.c • A relative file name is its filename and path from current directory. Ex: alex/project/assign1.c in </usr/prof>

  12. Unix File Representation • A Unix file has a description which is stored in a structure called an inode. • Each file is segmented and located in a tree-structured representation. • Features of inode: • fairly efficient for small files • flexible if the size of the file changes

  13. inode file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification ... file block direct pointers to beginning file blocks ... ... ... single indirect pointers ... ... ... double indirect pointers ... ... ... triple indirect pointers ... ... ... ... ... ... point block

  14. inode – Small File file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification 1 direct pointers 1, 2, 3 2 3 single indirect pointers double indirect pointers triple indirect pointers

  15. inode – Larger File file information: size (in bytes) owner UID and GID relevant times (3) link and block counts permissions the time of creation the time of last access the time of last modification 1 file block ... 13 direct pointers 1, 2, …, 12 12 ... ... ... single indirect pointers ... ... point block double indirect pointers triple indirect pointers

  16. File Information • ls -la -rw-rw---- 1 wrlai cs 3302 Jan 14 16:32 myfile • The name of file • The time and date the file was • created or last modified • The size of the file in bytes • The name of group • The name of the owner • The number of hard links • Access Permissions ( 9 bits )

  17. Access Permissions(1/3) • Type of file (one bit) : d : directory - : ordinary file s : socket p : names pipe (FIFO) l : symbolic link c : character device (terminal) b : block device (disk)

  18. Access Permissions(2/3) • Three types of users can access a file(Owner/Group/Other) • A user can attempt to access a file in three ways(Read/Write/Execute) • Ordinary file • Read : you can read from file • Write : you can write to the file • Execute : you can execute the file • Directory • Read : you can read the directory • Write : you can create, move, copy or remove entries • Execute : you can search (i.e., enter) the directory

  19. Access Permissions(3/3) - r w - r w - r - - Other’s privileges (r-- means readable but not writable or executable) Group’s privileges (rw- means readable and writable but not executable) Owner’s privileges (rw- means readable and writeable but not executable) File mode (- indicates an ordinary file)

  20. Change Mode(1/4) • You can use chmod to change the access permissions of a file • Example : % ls -l myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod g+x myfile -rw-rwxrw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod o-w myfile -rw-rwxr-- 1 wrlai cs 3302 May 20 12:20 myfile

  21. Change Mode(2/4) WHO u User g Group o Other a All (ugo) Operator - Remove permission + Add permission = Set permission Permissions r Read w Write x Execute l Set locking privilege s Set user or group ID mode t Set save text (sticky bit) mode u User’s current permissions g Group’s current permission o Other’s current permissions

  22. Change Mode(3/4) -r--r--r-- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 666 myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile read write executeValue read write execute - - - 0 0 + 0 + 0 - - Yes 1 0 + 0 + 1 - Yes - 2 0 + 2 + 0 - Yes Yes 3 0 + 2 + 1 Yes - - 4 4 + 0 + 0 Yes - Yes 5 4 + 0 + 1 Yes Yes - 6 4 + 2 + 0 Yes Yes Yes 7 4 + 2 + 1

  23. Change Mode(4/4) % chmod 666 myfile -rw-rw-rw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 676 myfile -rw-rwxrw- 1 wrlai cs 3302 May 20 12:20 myfile % chmod 674 myfile -rw-rwxr-- 1 wrlai cs 3302 May 20 12:20 myfile

  24. 16 bits Attribute(1/3) • Every file has 16 attribute bits. • File Type(12-15 bits) • 12:S_IFIC(Named pipe) • 13:S_IFCHR/S_IFBLK(Character or Block) • 14:S_IFDIR(Directory) • 15:S_IFREG(Original file) • Only one bit can be set in bits 12-15.

  25. 16 bits Attribute(2/3) • Access permissions(0-8 bits)rwxrwxrwx • Sticky bit 黏著位元(bit 9):S_ISVTX • 當此 bit=1,file 會一直留在記憶體中而不會被swap out。 • 對於 Original files,使用者執行位元為 t(而非x),表示此 bit=1。例如:ls、sh、vi。 • 對於 Directory,使用者執行位元為 t,表示只有超級使用者可刪除此目錄。

  26. 16 bits Attribute(3/3) • Set Group ID(bit 10):S_ISGID • 當此 bit=1,表示對此file,每個人均有和file 擁有者相同的執行權(bit 6 = ‘s’)。 • 執行此file 的使用者,EUID 改成和 file 擁有者相同的 UID 。 • Set User ID(bit 11):S_ISUID • 當此 bit=1,表示對此file,Group 內的人均有和file 擁有者相同的執行權。

  27. UID、GID、EUID、EGID • In /etc/passwd, every user has his • UID:real User ID • GID:Group ID(可以有很多個) • Login shell 時,EUID=UID、EGID=GID。 • EUID:Effective User ID • EGID:Effective Group ID • 這四個 ID 用於決定某一使用者對於某一檔案的使用權。

  28. Example of UID & EUID • 某一使用者的 UID = ncliu,但可以利用 passwd 修改 /etc/passwd 的內容,如同擁有和root 相同的權限(EUID = root)。 被執行的指令 UID EUID SUID /bin/passwd ncliu root on

  29. Some important Shell Commands

  30. rm - remove(1/3) • The rm command allows you to remove files. • You need to have write permission. • Is it possible to restore a file that has been removed ? NO !

  31. rm - remove(2/3) % ls -l myfile.* • -rw-rw-rw- 1 wrlai cs 1898 Jan 5 ... myfile.1 • -r--r--r--1 wrlai cs 1898 jan 7 ... myfile.2 % rm myfile.* • rm : override protection 444 for myfile.2 ? y % rm -i testfile.* • i(interactive): ask user before remove • rm : remove testfile.1 ? y • rm : remove testfile.2 ? y

  32. rm - remove(3/3) % rm -f myfile.* • f(force): regardless the file permissions % ls -l drw-rw-rw- 1 wrlai cs 1898 Jan 5 ... dummy % rm -r dummy • r(recursive): remove all files and subdirectories within dummy How about these commands list below : % rm -r *% rm -ir dummy % rm -fr dummy

  33. Wildchar • ? 代表單一 character. • * 代表任何的 string.萬用字元 • `v*[0-9]`是 regular expression, v 開頭, 最後是數字的檔案. % ls sample1.c sample111.c % ls sample?.c sample1.c % ls sample*.c sample1.c sample111.c

  34. alias • You can add the following command in your .bachrc alias rm=“rm –i” • Then you open this account next time, you type rm abc It will ask user before remove

  35. mv - move • To move a file to a different directory % mv [ -if ] file ... directory • To rename a file or directory % mv [ -if ] oldname newname • If the target to which you move a file already exists, mv will replace the file. • If you want to be caution about losing data, use -i % mv -i original destination

  36. cp - copy(1/2) • Make a copy of a file % cp [-ip ] file1 file2 • i (interactive):ask the permission before replacing a file that already exists • p(preserve):the destination file have the same modification times and permissions as the source file • Copy files to a different directory %cp [-ip ] file ... directory

  37. cp - copy(2/2) • Copy a directory to another directory % cp -r [-ip ] directory1 directory2 % cp -r essays backups • r(recurive):will copy essays and all its files and subdirectories to backups (directory)

  38. mkdir - make directory • To make a directory % mkdir [-mp] directory ... • m : Specify the mode, in octal, for the new directory • p : Create both the directory, and any parent directories that don’t already exist.

  39. rmdir - remove directory • To remove a directory % rmdir [-ps] directory ... • p : Remove the directory and any empty parent directories • s : Suppress warning messages • Is it possible to remove your working directory ?

  40. chown, chgrp • To change the owner and group associated with a file. % chown % chgrp [-Rh] groupID files • R : Recursive operation • h : Change the ownership or group of the symbolic link file itself, not the file to which it refers • Note: In some system, these commands are not permit to anyone but the superuser.

  41. find (1/2) • To locate misplaced files % find . -name sample1.c -print >./C-language/sample1.c • to search through the current subtree(.)for a file named “sample1.c” % find /usr -name `v*[0-9]` -print • to search the file with its name start with v and end with a digit in the /usr subtree

  42. find (2/2) % find /etc -size +1000 -print • to search files with size larger than 1000 blocks (512 bytes) in the /etc subtree • ‘+’ : larger than; ‘=‘ : equal to (exactly); ‘-’ : less than % find / -name core -exec rm { } \ ; • to search the entire file system for the files named core and remove them) • Note : Be careful to use findto locate files starting from the root(/).

  43. Simple Test • Try to change one of your executable programs • Only you can read/write/execution. The other can’t do anything. • All of people can read it, but only you can write and execute it. • Use the command “man” to find the detail about “ls”.

More Related