Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 File System of Unix 2 References “UNIX&Linux Shell 設計程式篇 ” 第一章 – 請參考 pages 18 - 32 “Practical UNIX Programming - A Guide to Concurrency, Communication,

Similar presentations


Presentation on theme: "1 File System of Unix 2 References “UNIX&Linux Shell 設計程式篇 ” 第一章 – 請參考 pages 18 - 32 “Practical UNIX Programming - A Guide to Concurrency, Communication,"— Presentation transcript:

1

2 1 File System of Unix

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

4 3 Online Introduction of File System Original File Some important Shell Commands

5 4 Introduction of File System

6 5  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 An Example of Files

7 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

8 7 The Tree Structure wrlai.cshrc.login.mailrcproject Cprogram proj1proj2 a.outfile1.cc t1.cct2.cch1* myfile abc

9 8 The directory you are working in, is called working directory or current directory. cd Cprogram When you are first log in, the working directory is your home directory. –Use 〜 to present the home directory cd 〜 Current/ Home Directory

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

11 10 Original Files

12 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

13 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

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

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

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

17 16  ls -la -rw-rw---- 1 wrlai cs 3302 Jan 14 16:32 myfile File Information 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 )

18 17 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) Access Permissions ( 1/3 )

19 18 T hree 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 Access Permissions ( 2/3 )

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

21 20 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 Change Mode ( 1/4 )

22 21 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 Change Mode ( 2/4 )

23 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 execute Value 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

24 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

25 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.

26 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 ,表示只 有超級使用者可刪除此目錄。

27 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 擁有者相同的執行權 。

28 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 用於決定某一使用者對於某一 檔案的使用權。

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

30 29 Some important Shell Commands

31 30 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 ! rm - remove ( 1/3 )

32 31 % 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 rm - remove ( 2/3 )

33 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

34 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

35 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

36 35 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 mv - move

37 36 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 cp - copy ( 1/2 )

38 37 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) cp - copy ( 2/2 )

39 38 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. mkdir - make directory

40 39 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 ? rmdir - remove directory

41 40 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. chown, chgrp

42 41 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 find ( 1/2 )

43 42 % 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 find to locate files starting from the root ( / ). find ( 2/2 )

44 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”.


Download ppt "1 File System of Unix 2 References “UNIX&Linux Shell 設計程式篇 ” 第一章 – 請參考 pages 18 - 32 “Practical UNIX Programming - A Guide to Concurrency, Communication,"

Similar presentations


Ads by Google