การใช้ระบบปฏิบัติการ UNIX พื้นฐาน ...
description
Transcript of การใช้ระบบปฏิบัติการ UNIX พื้นฐาน ...
![Page 1: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/1.jpg)
การใช้�ระบบปฏิ�บ�ติ�การ UNIX พื้��นฐาน
บทท�� 4 File Manipulation วิ�บ�ลย์� วิราสิ�ทธิ�ช้�ย์
น�กวิ�ช้าการคอมพื้�วิเติอร� ศู�นย์�คอมพื้�วิเติอร� ม.สิงขลานคร�นทร�
เวิอร�ช้��น 1 วิ�นท�� 21 ม�.ย์ . 46
![Page 2: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/2.jpg)
OOOOOOOOOO This lecture covers:
• Ways to examine the contents of files.• How to find files when you don't know how thei
r exact location.• Ways of searching files for text patterns.• How to sort files.• Tools for compressing files and making backu
ps.• Accessing floppy disks and other removable m
edia.
![Page 3: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/3.jpg)
OOOOOOOOOO OOOO OOOOOOO Besides cat there are several other useful utilities
for investigating the contents of files:
• file filename(s) file analyzes a file's contents for you and reports
- a high level description of what type of file it app ears to be:
prompt> file myprog.c letter.txt webpage.html myprog.c: C program text
letter.txt: English text webpage.html: HTML document text
![Page 4: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/4.jpg)
Continued
• head, tail filename head and tail display the first and last f
ew lines in a file respectively. You can specify the number of lines as an optio
n, e.g. prompt> head -5 /etc/passwd
prompt> tail -20 /etc/passwd
![Page 5: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/5.jpg)
Continued
• More examples, head sample.f
- display first 10 lines (default)head -5 sample.f - display first 5 lineshead -25 sample.f - display first 25 lines
tail sample.f
- display last 10 lines (default)tail -5 sample.f - display last 5 linestail -5c sample.f - display last 5 characterstail -25 sample.f - display last 25 lines
![Page 6: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/6.jpg)
Continued
• tail includes a useful -f option that can be used to continuously monitor the last few lines of a (possibly changing) file. This ca
n be used to monitor log files, for exampl e:
prompt> tail -f /var/log/messages
continuously outputs the latest additions to the system log file.
![Page 7: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/7.jpg)
Finding Files
• find If you have a rough idea of the directory tree t
he file might be in (or even if you don't and yo u're prepared to wait a while) you can use fin
d:
find directory -name targetfile -print
find will look for a file called targetfile in any p art of the directory tree rooted at directory. ta
rgetfile can include wildcard characters.
![Page 8: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/8.jpg)
Continued
• For example, prompt> find /home -name "*.txt" -print
will search all user directories for any file end ing in ".txt" and output any matching files.
• find can in fact do a lot more than just find fil es by name. It can find files by type (e.g. -typ
e f for files, - type d for directories), by permis sions (e.g. - perm o=r for all files and directori
es that can be read by others), by size (-size ) etc.
![Page 9: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/9.jpg)
Continued
• You can also execute commands on the fil es you find. For example,
prompt> find . -name "*.txt" -exec wc -l '{}' '
;'
counts the number of lines in every text fil e in and below the current directory. The '
{} ' is replaced by the name of each file fo und and the ';' ends the -exec clause.
![Page 10: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/10.jpg)
Continued
• which command If you can execute an application progr
am or system utility by typing its name at the shell prompt, you can use which to find out where it is stored on disk. F or example:
prompt> which ls /bin/ls
![Page 11: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/11.jpg)
Finding Text in Files
• grep (General Regular Expression Print)grep options pattern files grep searches the named files (or standard inp
ut if no files are named) for lines that match a given pattern. The default behaviour of grep is
to print out the matching lines. For example:
prompt> grep hello *.txt
searches all text files in the current directory f or lines containing "hello".
![Page 12: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/12.jpg)
Continued
• Some of the more useful options that grep provides are:-c (print a count of the number of lines that match),-i (ignore case),-v (print out the lines that don't match the pattern) and-n (printout the line number before printing the matchin
g line).• So
prompt> grep -vi hello *.txt
searches all text files in the current directory for lines th at do not contain any form of the word hello (e.g. Hello,
HELLO, or hELlO).
![Page 13: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/13.jpg)
Continued
• If you want to search all files in an entire dire ctory tree for a particular pattern, you can co
mbine grep with find using backward single q uotes to pass the output from find into grep.
• So prompt> grep hello `find . -name "*.txt" -print`
will search all text files in the directory tree r ooted at the current directory for lines contai
ning the word "hello".
![Page 14: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/14.jpg)
Continued
• The patterns that grep uses are actually a speci al type of pattern known as regular expressions
.• Most characters, including all letters and digits,
are regular expressions.• Any other character with special meaning may
be quoted by preceding it with a backslash (\ ).• A list of characters enclosed by '[ ' and '] ' match
es any single character in that list; if the first ch aracter of the list is the caret `^ ', then it match
es any character not in the list.
![Page 15: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/15.jpg)
Continued
• A range of characters can be specified using a dash (- ) between the first and l
ast items in the list.• - - So [0 9] matches any digit and [^0 9]
matches any character that is not a di git.
![Page 16: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/16.jpg)
Continued
• The caret `^ ' and the dollar sign `$ ' are sp ecial characters that match the beginning
and end of a line respectively. The dot '. ' matches any character.
• So prompt> grep ^..[l-z]$ hello.txt
matches any line in hello.txt that contains a three character sequence that ends with a lowercase letter from l to z.
![Page 17: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/17.jpg)
Continued
• Some complex examples,
prompt> cat hello.txt | grep "dog" | grep -v "cat"
finds all lines in hello.txt that contain t he string "dog" but do not contain the
string "cat".
![Page 18: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/18.jpg)
OOOOOOO OOOOO There are two facilities that are useful for sorting files in
UNIX:• sort filenames
sort sorts lines contained in a group of files alphabetic - ally or (if the n option is specified) numerically. The s
orted output is displayed on the screen, and may be s tored in another file by redirecting the output. So
prompt> sort input1.txt input2.txt > output.txt
outputs the sorted concentenation of files input1.txt a nd input2.txt to the file output.txt.
![Page 19: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/19.jpg)
Continued
• uniq filename uniq removes duplicate adjacent lines from a file. This facility is most useful when combined with sort:
prompt> sort input.txt | uniq > output.txt
![Page 20: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/20.jpg)
OOOO OOOOOOOOOOO OOO OOOOOO
UNIXsystemsusual l y suppor t a number of ut i l i t i es f or ba cking up and compressing files. The most useful
are:• tar (tape archiver)
tar backs up entire directories and files onto a t ape device or (more commonly) into a single dis
k file known as an archive. An archive is a file that contains other files plus i
nformation about them, such as their filename, owner, timestamps, and access permissions. tar
does not perform any compression by default.
![Page 21: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/21.jpg)
Continued
• To create a disk file tar archive, usetar -cvf archivename filenames
where archivename will usually have a .tar extension. Here the c option mean
s create, v means verbose (output file names as they are archived), and f me
ans file. For example,prompt> tar -cvf play.tar play
![Page 22: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/22.jpg)
Continued
• To list the contents of a tar archive, us e
tar -tvf archivename
For example,prompt> tar -tvf play.tar
• To restore files from a tar archive, usetar -xvf archivename
For example,prompt> tar -xvf play.tar
![Page 23: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/23.jpg)
Continued
• compress, gzip compress and gzip are utilities for compressi
ng and decompressing individual files (which may be or may not be archive files). To compr
ess files, use: compress filename
orgzip filename
In each case, filename will be deleted and rep laced by a compressed file called filename.Z
or filename.gz .
![Page 24: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/24.jpg)
Continued
• For example,prompt> compress play.tar
has a result of play.tar.Zor
prompt> gzip play.tar
has a result of play.tar.gz
![Page 25: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/25.jpg)
Continued
• To reverse the compression process, u se:compress -d filename
orgzip -d filename
• For example,prompt> compress -d play.tar.Z
orprompt> gzip -d play.tar.gz
![Page 26: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/26.jpg)
OOOOOOOO OOOOOOOOO OOOOO UNIX supports tools for accessing removable
media such as CDROMs and floppy disks.
• mount, umount The mount command serves to attach the f
ilesystem found on some device to the files ystem tree. Conversely, the umount comm
and will detach it again (it is very important to remember to do this when removing the
floppy or CDROM).
![Page 27: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/27.jpg)
Continued
• To access a floppy we can use: prompt> mount /mnt/floppy prompt> cd /mnt/floppy prompt> ls
• To force all changed data to be written back to the floppy and to detach the fl oppy disk from the filesystem, we use:
prompt> umount /mnt/floppy
![Page 28: การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation](https://reader035.fdocument.pub/reader035/viewer/2022062323/568158be550346895dc608d4/html5/thumbnails/28.jpg)
ฝึ(กปฏิ�บ�ติ�คร��งท��5
ใช้�เวิลา 1 ช้��วิโมง