49
1 File Management Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology email:[email protected] http://www.cse.ttit.edu/~fccheng

File Management

  • Upload
    brygid

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

File Management. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology email:[email protected] http://www.cse.ttit.edu/~fccheng. Outline. Unix File System Basic File Manipulation Utilities - PowerPoint PPT Presentation

Citation preview

Page 1: File Management

1

File Management

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung Institute of Technology

email:[email protected]://www.cse.ttit.edu/~fccheng

Page 2: File Management

2

Outline

• Unix File System• Basic File Manipulation Utilities• Advanced File Manipulation Utilities

Page 3: File Management

3

Unix File Systems

• A file system is a collection of files and directories on a disk or tape in standard UNIX file system format. • Low capacity disks (such as floppy disks) usually contain a single file system.• Large disks usually are divided into several regions (partitions), each containing a file system.• Each UNIX file system contains four major parts: A. boot block: loader programs for booting B. superblock: key information of the file system C. i-node table: i-nodes store information about files. D. data block: file storage

Page 4: File Management

4

File System layout

Block 0: bootstrap

Block 1: superblock

Block 2

Block n

...

Block n+1

The last Block

...

Block 2 - n:i-nodes

Block n+1 - last:Files

Page 5: File Management

5

Boot Block

• A boot block may contains several physical blocks.• Note that a physical block contains 512 bytes

(or 1K or 2KB)• A boot block contains a short loader program for

booting• It is blank on other file systems.

Page 6: File Management

6

Super Block

• Superblock contains key information about a file system• Superblock information: A. Size of a file system and status:

label: name of this file systemsize: the number of logic blocksdate: the last modification date of super block.

B. information of i-nodesthe number of i-nodesthe number of free i-nodes

C. information of data block: free data blocks.• The information of a superblock is loaded into memory.

Page 7: File Management

7

I-nodes

• i-node: index node (information node)• i-list: the list of i-nodes • i-number: the index of i-list.• The size of an i-node: 64 bytes. • i-node 0 is reserved.• i-node 1 is the root directory.• i-node structure: next page

Page 8: File Management

8

I-node structuremode

owner

timestamp

Size

Block count

Direct blocks0-9

Double indirect

Triple indirect

Single indirect

Data block

Data block

Data block

Indirect block

...

Data block

Data block

Data block

...

Indirect blockIndirect block

Indirect block

...

Reference count

Page 9: File Management

9

I-node structure

• mode: A. type: file, directory, pipe, symbolic link B. Access: read/write/execute (owner, group,)

• owner: who own this I-node (file, directory, ...)• timestamp: creation, modification, access time• size: the number of bytes• block count: the number of data blocks• direct blocks: pointers to the data• single indirect: pointer to a data block which

pointers to the data blocks (128 data blocks).• Double indirect: (128*128=16384 data blocks)• Triple indirect: (128*128*128 data blocks)

Page 10: File Management

10

Data Block

• A data block has 512 bytes. A. Some FS has 1K or 2k bytes per blocks.B. See blocks size effect (next page)

• A data block may contains data of files or data of a directory.• File: a stream of bytes. • Directory format:

i-# Next size File name pad

Page 11: File Management

11

Report.txt

home

john

bin

find

alex jenny

notes

grep

i-# Next 10 Report.txt pad i-# Next 3

bin pad i-# Next 5 notes pad 0 Next

Page 12: File Management

12

Block size

Max Accessible Capacity (bytes)Blocksize

Directblock

IndirectBlock

DoubleIndirectblock

Tripleindirectblock

512 5120 64K(128)

8M 1G

1K 10240 256K(256 )

64M 16G

2K 20480 1M(512)

512M 256G

Page 13: File Management

13

Current Working Directory

login: kcpassword:******

Welcome to UNIX !$ pwd/home/kc$ cd /usr/kc/source$ pwd/usr/kc/source

• pwd: print current working directory

Report.txt

home

kc

source

find

alex

notes

grep

Page 14: File Management

14

Changing Working Directory

• The cd utility makes another directory the working directory.

$ cd /home/alex/literature$ pwd/home/alex/literature$ cd$ pwd/home/kc

Page 15: File Management

15

List Files

• list the contents of directories• Example: list the current working direcory

$ ls Report.txt sourcenotes

Page 16: File Management

16

Currentdirectory

Per-processdata region

Boot Block

...

SuperBlock

i-node

i-node

i-node

i-node

...

...

...

Current Dir

Report.txt

source

notes...

...

...

...

I-nodes

Data Blocks

Page 17: File Management

17

List Files

• list with options• -l: long format$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes

permissions

number oflinks

owner groupuser

Size Last access timecode

File ordirectoryname

Page 18: File Management

18

File Code (types)

code meaning

- Ordinary file

d Directory file

c Character special file

b Block special file

l Symbolic link

p FIFO (pipe) file

Page 19: File Management

19

Remove Files

• rm fileName: remove fileName in current directory• rm * : remove all files in current directory• rm -rf dirName: remove all the files in dirName• rm -i fileName: interactive option

$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$rm *$ls -ltotal 0$

Page 20: File Management

20

Move Files

• mv file or files to someOtherDir

$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$mv * ../notes$ls -ltotal 0$ls -l ../notes-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes

Page 21: File Management

21

Copy Files

• Copy file will duplicate files

$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$ls -l ../notestotal 0$cp * ../notes$ls -l ../notes-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes

Page 22: File Management

22

Links

• link: a reference to a file or directory• two kinds of link: hard links and soft link• hard link: ln file1 file2

(create a hard link file2 to file1)• soft link: ln -s file1 file2

(create a soft link file2 to file1)

Page 23: File Management

23

Hard Links(I)$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$ln Report.txt hardLink.txt$ls -l -rw-r--r-- 2 kc pubs 3355 May 2 10:52 Report.txt-rw-r--r-- 2 kc pubs 3355 May 9 7:22 hardLink.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$rm Report.txt$ls -l -rw-r--r-- 1 kc pubs 3355 May 9 7:22 hardLink.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$more hardLink.txtthe content is shown here

Page 24: File Management

24

Hard Links (II)

i-# Next 10 Report.txt pad

i-# Next 12 hardLink.txt pad

Reference count=2

Description of filei-node File

Page 25: File Management

25

Remove Hard Links (III)

i-# Next 12 hardLink.txt pad

Reference count=1

Description of filei-node File

Page 26: File Management

26

Soft Links (I)$ ls -l-rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$ln -s Report.txt softLink.txt$ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txtlrwxrwxrwx 1 kc pubs 3355 May 9 7:22 softLink.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$rm Report.txt$ls -l lrwxrwxrwx 1 kc pubs 3355 May 9 7:22 softLink.txtdrwxrwxr-x 2 kc pubs 512 May 5 14:03 sourcedrwxr-xr-x 1 jo staff 512 Jun 3 1997 notes$more softLink.txtsoftLink.txt: No such file or directory$

Page 27: File Management

27

Soft Links (Symbolic) II

i-# Next 10 Report.txt pad

i-# Next 12 softLink.txt pad

Reference count=1

i-nodeFile

Reference count=1

Description of file

i-node

i-node/home/kc

/Report.txt

Page 28: File Management

28

Soft Links (Symbolic) III

0 Next 10 Report.txt pad

i-# Next 12 softLink.txt pad

Reference count=1i-node

/home/kc/Report.txt

Page 29: File Management

29

Change File modes

• Permission type: r: read w:write x:execute• Permission object: user(u), Group(g), others(o), all(ugo)• chmod: change permission

+ add permission = set permission- remove permission

• Example:

• chmod -R 755 SomeDir

$ chmod a+rw Report.txt$ ls -l Report.txt-rw-rw-rw- 1 kc pubs 3355 May 2 10:52 Report.txt

Page 30: File Management

30

Change Owner and Group

• change owner: you need to have the right (such as superuer) to do so

$ chown john Report.txt$ ls -l Report.txt-rw-rw-rw- 1 john pubs 3355 May 2 10:52 Report.txt$ chgrp staff Report.txt$ ls -l Report.txt-rw-rw-rw- 1 john staff 3355 May 3 11:45 Report.txt

Page 31: File Management

31

Create & Remove Directories

• mkdir: create a new directory• rmdir: remove a directory

Report.txt

home

kc

source

find

alex

notes

grep

$ mkdir /home/kc/notes/UNIX$ ls /home/kc/notesUNIX$rmdir /home/kc/notes/UNIX…$rm -r /home/kc/source

UNIX

Page 32: File Management

32

Search Files/directories

/

home

john

temp literature

promo

demo

$find /home -name demo -print/home/alex/demo/home/john/demo

alex

demo

• find: locate the misplaced files (or directories)

Page 33: File Management

33

Search Files/directories

$ find /usr -name ‘v*[0-9]’ -print/usr/share/lib/terminfo/v/vt100/usr/share/lib/terminfo/v/vt52….

• locate files/directories that start with a v and end with a digit

Page 34: File Management

34

Search Files/directories

$ find /usr -size +1000 -print/usr/lib/libc.so/usr/lib/libnsl.so….

• locate files whose size have more than 1000 blocks

Page 35: File Management

35

Search Files/directories

$ find . -mtime -1 -print./notes/softLink.txt./notes/hardLink.txt….

• locate files that have been modified within the last day (24 hrs)

Page 36: File Management

36

Search Files/directories

$ find / -name core -exec rm {} \;$ find / -name core -ok rm {} \;

Lab: $ mkdir d1 d2 d3$ cp anyFile d1/xxx$ cp anyFile d2/xxx$ cp anyFile d3/xxx$ find . -name xxx -ok rm {} \;

• locate files and then remove them

Page 37: File Management

37

Compression

• Two lossless compression: compress and pack• compressed files: *.z

$ ls -l-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 ls-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 vi-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 who$ pack *UX:pack: INFO: ls: 24.7% CompressionUX:pack: INFO: vi: 17.9.7% CompressionUX:pack: INFO: who: 19.6% Compression$ ls -l-r-xr-xr-x 1 kc other 13775 Feb 4 10:52 ls.z-r-xr-xr-x 1 kc other 132403 Feb 4 10:52 vi.z-r-xr-xr-x 1 kc other 51581 Feb 4 10:53 who.z

Page 38: File Management

38

Compression

• Two lossless compression: compress and pack• compressed files: *.Z

$ ls -l-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 ls-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 vi-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 who$ compress -v *ls: Compression 34.22% -- replaced with ls.Zvi: Compression 34.43% -- replaced with vi.Zwho: Compression 30.90% -- replaced with who.Z$ ls -l-r-xr-xr-x 1 kc other 12032 Feb 4 10:54 ls.Z-r-xr-xr-x 1 kc other 105729 Feb 4 10:54 vi.Z-r-xr-xr-x 1 kc other 44323 Feb 4 10:55 who.Z

Page 39: File Management

39

Compression

• Restore compressed files(*.z): unpack• Restore compressed files(*.Z): uncompress

$ unpack *$ ls -l-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 ls-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 vi-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 who$ uncompress *$ ls -l-r-xr-xr-x 1 kc other 18292 Feb 4 10:54 ls-r-xr-xr-x 1 kc other 161260 Feb 4 10:54 vi-r-xr-xr-x 1 kc other 64148 Feb 4 10:55 who

Page 40: File Management

40

Collect Files

• tar(tape archive): collect files into a single file• Space are allocated in clusters of two, four or even eight blocks at a time. (block size = 512 bytes) • Large disks usually have large cluster size.• If you create a file containing just a single character, Unix system will typically allocate 2 or 4 512-byte blocks• Thus compressing a 500-byte file into 256 bytes won’t save any space.• Collecting (tar) small files into a single file will save space.

Page 41: File Management

41

Collect Files

• create a tar file: -c

$ ls -l *-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr$ tar -cvf aTarFile.tar *ltra don.ltr 37 tape blocksa don2.ltr 292 tape blocksa jane.ltr 129 tape blocks$ ls -l *-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr-r-xr-xr-x 1 kc other 243700 Feb 4 10:55 aTarFile.tar

Page 42: File Management

42

Collect Files

• list the content of a tar file: -t

$ ls -l *-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr-r-xr-xr-x 1 kc other 243700 Feb 4 10:55 aTarFile.tar$ tar -tvf aTarFile.tar-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr$ rm *.ltr

Page 43: File Management

43

Collect Files

• extract a tar file: -x

$ tar -xvf aTarFile.tar $ ls -l *-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr-r-xr-xr-x 1 kc other 243700 Feb 4 10:55 aTarFile.tar

Page 44: File Management

44

Examine File Type

• file command determines the type information of files.• Type information: directory, binary file, ascii file

$ lsReadme makefile meggaa.c zsrc$ file *Readme: ascii textmakefile: ascii textmeggaa.c c program textzsrc: directory

Page 45: File Management

45

Examine Disk Usage

• du command show how much disk storage (blocks) of your files.

$ ls -l *-r-xr-xr-x 1 kc other 18292 Feb 4 10:52 don.ltr-r-xr-xr-x 1 kc other 161260 Feb 4 10:52 don2.ltr-r-xr-xr-x 1 kc other 64148 Feb 4 10:53 jane.ltr$ du37 ./don.ltr 292 ./don2.ltr129 ./ jane.ltr$ du /ect/fc…$ du -s /ect/fc2750 /ect/fs

Page 46: File Management

46

Examine Free Disk Space

• df command show how much free disk storage (blocks) • some system you may use -k option to show K bytes.

$ df -k Filesystem kbytes used avail cap mount/dev/dsk/c0t3d03s0 118679 83517 23302 79% //dev/dsk/c0t3d03s2 1970068 1556936 216132 88% /homesswap 26728 164 26564 1% /tmp$ df -k .Filesystem kbytes used avail cap/dev/dsk/c0t3d03s2 1970068 1556936 216132 88% /home2$ df . /home2 (/dev/dsk/c0t3d03s2 ): 826248 blocks 954962 files

Page 47: File Management

47

Dump Files

• od (octal dump) command show a file with octal, decimal, ASCII, hexadecimal format.

$ cat spicesthyme nutmegsage cuminsalt pepper$ od -c spices000000 t h y m e \t n u t m e g \n s a g000020 e \t c u m i n \n s a l t \t p e p000040 p e r \n000044

Page 48: File Management

48

On-line Manual

• Always use on-line manual (man) to find detail syntax and options

$ man cat ...$ man od ...

Page 49: File Management

49

End of File Management Lecture