net prog1-file inout-5
TRANSCRIPT
Technical Foundation of Computer Science 5
Network Programming I
Maria Sawaby
Department of Communication and Operating System
May 4, 2015
Lecture-05 (Network Department) Net-Prog I May 4, 2015 1 / 22
Contents
1 File In, File Out
2 Making Files Immutable
3 Loopback Files and Mounting
4 Mounting ISO �les as loopback
5 Finding di�erence between �les, patching
6 wc
7 Printing directory tree
8 Summary
Lecture-05 (Network Department) Net-Prog I May 4, 2015 2 / 22
File In, File OutGenerating �les of any size
The easiest way to create a large sized �le with a given size is to usethe dd command dd if=/dev/zero of=new�le bs=1M count=1
Lecture-05 (Network Department) Net-Prog I May 4, 2015 3 / 22
Intersection and set di�erence on text �les
intersection and set di�erence operations are used in mathematicalclasses on set theory
we can have similar operations on text �les
comm command is used to perform comparisons between two �lesI Intersection: will print lines that are common between two �lesI Di�erence: will print lines which are not the same in all �les
Note that comm takes sorted �les as input
Lecture-05 (Network Department) Net-Prog I May 4, 2015 4 / 22
Intersection and set di�erence on text �les
$ sort �le1 -o A.txt ; sort �le2 -o B.txt
Lecture-05 (Network Department) Net-Prog I May 4, 2015 5 / 22
Intersection and set di�erence on text �les
Some options are available to format the output as per ourrequirement
I -1 removes �rst column from outputI -2 removes the second columnI -3 removes the third column
Lecture-05 (Network Department) Net-Prog I May 4, 2015 6 / 22
Intersection and set di�erence on text �les
Lecture-05 (Network Department) Net-Prog I May 4, 2015 7 / 22
Making Files Immutable
When a �le is made immutable, any user or super user cannot removethe �le until the immutable attribute is removed from the �le
chattr can be used to make �les immutable
$sudo chattr +i �le1
$rm �le1rm: remove write-protected regular �le '�le1'? yrm: cannot remove '�le1': Operation not permitted
sudo chattr -i �le1
make /etc/shadow �le immutable to make it secure from modi�cation
Lecture-05 (Network Department) Net-Prog I May 4, 2015 8 / 22
Loopback Files and Mounting
We normally create partitions on a physical hard disk
Using Loopback �lesystems we can create in �les rather than aphysical device!
$dd if=/dev/zero of=loopback�le.img bs=1GB count=1
$mkfs.ext4 loopback�le.img
$sudo �le loopback�le.img (or: �le -b lookback�le.img)
$sudo mkdir /mnt/loopback
$mount -o loop loopback�le.img /mnt/loopback
This is the shortcut method. We do not attach it to any devices. Butinternally it attaches to a device called /dev/loop1 or loop2
Lecture-05 (Network Department) Net-Prog I May 4, 2015 9 / 22
Loopback Files and Mounting
Lecture-05 (Network Department) Net-Prog I May 4, 2015 10 / 22
Loopback Files and Mounting
We can do it manually as follows:
# losetup /dev/loop1 loopback.img
# mount /dev/loop1 /mnt/loopback
# umount /mnt/loopback
Lecture-05 (Network Department) Net-Prog I May 4, 2015 11 / 22
Mounting ISO �les as loopback
An ISO �le is an archive of any optical media. We can mount ISO �lesin the same way that we mount physical discs by using loopbackmounting.
A mount point is just a directory, which is used as access path tocontents of a device through a �lesystem
# mkdir /mnt/iso
# mount -o loop /media/maria/30ECFD7EECFD3EA2/ubuntu-10.04-desktop- i386.iso /mnt/iso/
Lecture-05 (Network Department) Net-Prog I May 4, 2015 12 / 22
Mounting ISO �les as loopback
Lecture-05 (Network Department) Net-Prog I May 4, 2015 13 / 22
Finding di�erence between �les, patching
If the �les are of 1000s of lines, they are practically very di�cult andtime consuming to compare!
The di� command utility is used to generate di�erence �les
$ di� -u version1.txt version2.txt
-n option is used to show line number
Lecture-05 (Network Department) Net-Prog I May 4, 2015 14 / 22
Finding di�erence between �les, patching
A patch �le can be generated by redirecting the di� output to a �le,as follows:
$ di� -u �lev1.txt �lev2.txt > version.patch
Now using the patch command we can apply changes to any of thetwo �les
To patch a �le:
$patch -p1 �lev1.txt < version.patch
To revert the changes:
$patch -p1 �lev1.txt < version.patch
Lecture-05 (Network Department) Net-Prog I May 4, 2015 15 / 22
patching
Lecture-05 (Network Department) Net-Prog I May 4, 2015 16 / 22
Counting number of lines, words, and characters in a �le
wc is the utility used for countingcount number of lines:
$wc -l �lein order to use stdin as input:
$cat �le | wc -l
count number of words:
$ wc -w �le$cat �le | wc -w
count number of characters:
$ wc -c �le$cat �le | wc -c
count number of characters in a text:
$echo -n this is a text | wc -c
Lecture-05 (Network Department) Net-Prog I May 4, 2015 17 / 22
wc
when wc is used with no option:
$wc �le
it will print number of lines, words and characters
wc can also be used to print the length of longest line using -L option
$wc �le -L
Lecture-05 (Network Department) Net-Prog I May 4, 2015 18 / 22
Printing directory treetree command
Lecture-05 (Network Department) Net-Prog I May 4, 2015 19 / 22
Printing directory treeHTML output for tree
It is possible to generate HTML output from the tree command
$ tree . -H http://localhost -o out.html
Lecture-05 (Network Department) Net-Prog I May 4, 2015 20 / 22
Summary
Generating Files of any size by dd command
�nding di�erences and intersections in �les using comm command.
making �les immutable using chattr
mounting loopback �les using dd, mount, losetup
patching using di� and patch commands
counting number of words, lines and characters using wc command
printing directory tree by tree command
Lecture-05 (Network Department) Net-Prog I May 4, 2015 21 / 22
Lecture-05 (Network Department) Net-Prog I May 4, 2015 22 / 22