490 likes | 642 Views
File Management. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology email:cheng@cse.ttit.edu.tw http://www.cse.ttit.edu/~fccheng. Outline. Unix File System Basic File Manipulation Utilities
E N D
File Management Instructors: Fu-Chiung Cheng (鄭福炯) Associate Professor Computer Science & Engineering Tatung Institute of Technology email:cheng@cse.ttit.edu.tw http://www.cse.ttit.edu/~fccheng
Outline • Unix File System • Basic File Manipulation Utilities • Advanced File Manipulation Utilities
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
File System layout Block 0: bootstrap Block 1: superblock Block 2 Block 2 - n:i-nodes ... Block n Block n+1 Block n+1 - last:Files ... The last Block
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.
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 system • size: the number of logic blocks • date: the last modification date of super block. • B. information of i-nodes • the number of i-nodes • the number of free i-nodes • C. information of data block: free data blocks. • The information of a superblock is loaded into memory.
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
mode I-node structure owner timestamp Data block Data block Size Data block Data block Reference count ... ... Block count Direct blocks 0-9 Data block Data block Indirect block ... Single indirect Indirect block Double indirect Indirect block Indirect block Triple indirect
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)
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
home john alex jenny Report.txt bin notes grep find i-# Next 10 Report.txt pad i-# Next 3 bin pad i-# Next 5 notes pad 0 Next
Current Working Directory • pwd: print current working directory home login: kc password:****** Welcome to UNIX ! $ pwd /home/kc $ cd /usr/kc/source $ pwd /usr/kc/source kc alex Report.txt source notes grep find
Changing Working Directory • The cd utility makes another directory the working • directory. $ cd /home/alex/literature $ pwd /home/alex/literature $ cd $ pwd /home/kc
List Files • list the contents of directories • Example: list the current working direcory $ ls Report.txt source notes
Boot Block SuperBlock i-node ... Current directory i-node ... Per-process data region I-nodes i-node ... ... i-node ... notes ... source ... Data Blocks Report.txt ... Current Dir
List Files • list with options • -l: long format $ ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes permissions owner group user Size File or directory name Last access time number of links code
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.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $rm * $ls -l total 0 $
Move Files • mv file or files to someOtherDir $ ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $mv * ../notes $ls -l total 0 $ls -l ../notes -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes
Copy Files • Copy file will duplicate files $ ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $ls -l ../notes total 0 $cp * ../notes $ls -l ../notes -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes
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)
Hard Links(I) $ ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-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.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-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.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $more hardLink.txt the content is shown here
Hard Links (II) i-# Next 10 Report.txt pad i-# Next 12 hardLink.txt pad File Reference count=2 i-node Description of file
Remove Hard Links (III) i-# Next 12 hardLink.txt pad File Reference count=1 i-node Description of file
Soft Links (I) $ ls -l -rw-r--r-- 1 kc pubs 3355 May 2 10:52 Report.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-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.txt lrwxrwxrwx 1 kc pubs 3355 May 9 7:22 softLink.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $rm Report.txt $ls -l lrwxrwxrwx1 kc pubs 3355 May 9 7:22 softLink.txt drwxrwxr-x 2 kc pubs 512 May 5 14:03 source drwxr-xr-x 1 jo staff 512 Jun 3 1997 notes $more softLink.txt softLink.txt: No such file or directory $
Soft Links (Symbolic) II i-# Next 10 Report.txt pad i-# Next 12 softLink.txt pad i-node /home/kc /Report.txt Reference count=1 i-node Reference count=1 File Description of file i-node
Soft Links (Symbolic) III 0 Next 10 Report.txt pad i-# Next 12 softLink.txt pad i-node /home/kc /Report.txt Reference count=1
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
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
Create & Remove Directories • mkdir: create a new directory • rmdir: remove a directory $ mkdir /home/kc/notes/UNIX $ ls /home/kc/notes UNIX $rmdir /home/kc/notes/UNIX … $rm -r /home/kc/source home kc alex Report.txt source notes grep find UNIX
Search Files/directories • find: locate the misplaced files (or directories) $find /home -name demo -print /home/alex/demo /home/john/demo / home alex john temp literature demo demo promo
Search Files/directories • locate files/directories that start with a v and end • with a digit $ find /usr -name ‘v*[0-9]’ -print /usr/share/lib/terminfo/v/vt100 /usr/share/lib/terminfo/v/vt52 ….
Search Files/directories • locate files whose size have more than 1000 blocks $ find /usr -size +1000 -print /usr/lib/libc.so /usr/lib/libnsl.so ….
Search Files/directories • locate files that have been modified within the last • day (24 hrs) $ find . -mtime -1 -print ./notes/softLink.txt ./notes/hardLink.txt ….
Search Files/directories • locate files and then remove them $ 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 {} \;
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% Compression UX:pack: INFO: vi: 17.9.7% Compression UX: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
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.Z vi: Compression 34.43% -- replaced with vi.Z who: 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
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
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.
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 *ltr a don.ltr 37 tape blocks a don2.ltr 292 tape blocks a 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
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
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
Examine File Type • file command determines the type information of files. • Type information: directory, binary file, ascii file $ ls Readme makefile meggaa.c zsrc $ file * Readme: ascii text makefile: ascii text meggaa.c c program text zsrc: directory
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 $ du 37 ./don.ltr 292 ./don2.ltr 129 ./ jane.ltr $ du /ect/fc … $ du -s /ect/fc 2750 /ect/fs
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% /homes swap 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
Dump Files • od (octal dump) command show a file with octal, decimal, • ASCII, hexadecimal format. $ cat spices thyme nutmeg sage cumin salt pepper $ od -c spices 000000 t h y m e \t n u t m e g \n s a g 000020 e \t c u m i n \n s a l t \t p e p 000040 p e r \n 000044
On-line Manual • Always use on-line manual (man) to find detail • syntax and options $ man cat ... $ man od ...
End of File Management Lecture