@laks wrote:
Today Day-3, we will walk through file related stuffs.
- More File operations
- Taking file backup with cp
- Verifying file integrity
- How to move file from one location to another?
- Protecting files with chmod
More File operations:
Now lets start manipulating files itself,rather than its data.
Taking file backup with cp
How to create copy of the file?,Say,we need to take a backup of .bash_history file from your home directory to
another directory dir1.
[efg@fedori ~]$cp .bash_history dir1
now verify the dir1 contents -
[efg@fedori ~]$ls -la dir1
You should find the file now it two places one under your home directory and another under "dir1".Verifying file integrity
Seems to be there but how can we 100% sure ,that file integrity is not compromised?
you can use 'head' and 'tail' to verify few starting and ending lines,if they are fine - we are okay!.
what if something in middle got messed up? but wait they universally (yeah,even martians use this trick) acce
pted
method. Use md5sum.
`
[efg@fedori ~]$md5sum .bash_history
8599e55388ecef39d8905b1c67b044ef .bash_history[efg@fedori ~]$md5sum dir1/.bash_history
8599e55388ecef39d8905b1c67b044ef dir1/.bash_history
`
As you can see both numbers/checksum are same.Thus we can be sure both files are same.How to move file from one location to another?
you can simply move a file to another directory
[efg@fedori ~]$mv giis.txt dir1
now verify the dir1 contents -
[efg@fedori ~]$ls -la dir1
Since we moved (not copied) this file will be available only one location which is "dir1".Here is a question for you,If you copying 10MB file into a directory and also moving (mv) 15GB file into a dir
ectory "dir1".
Which operation will complete first ? is it 10MB copy? or 15GB move?Protecting files with chmod:
As we seen above,someone can use cp and mv command to copy/move your files to another location.How to prevent
,someone from accessing your files?
Remember 'ls -l' which we used long long ago? yeah,use it again.
`
[efg@fedori ~]$touch secretfile[efg@fedori ~]$ls -l secretfile
-rw-rw-r--. 1 efg efg 0 2011-05-04 22:40 secretfile
`
As you can see owner and group is given read/write permission and 'others' given read-only permission.
Clearly other can't write something into your 'secretfile' since 'w' is missing for others.
but they can copy your file to some other location. In order to avoid that ,we can use 'chmod'.
[efg@fedori ~]$ chmod o-r secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-rw----. 1 efg efg 0 2011-05-04 22:40 secretfile
o stands for others and '-r' means remove read permission.
If you want to remove rw from groups too,then use 'g' followed '-rw'
[efg@fedori ~]$ chmod g-rw secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile
If you are someone like "Leonard" (hero from memento) ,and you want to extra careful ,even you do not to mess
up with it ,
then you yourself can remove its write permission like -
[efg@fedori ~]$ chmod u-w secretfile
[efg@fedori ~]$ ls -l secretfile
-r--------. 1 efg efg 0 2011-05-04 22:40 secretfile
After some time (15 minutes later :D) , suddenly realize you got to modify the this file,so need to add 'w' to
yourself.
When removing permission we used '-' ,now we want to add them so whatelse the symbol would be other '+'?so do it like -
[efg@fedori ~]$ chmod u+w secretfile
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile
Links
=====
For example ,you created a new directory named "dir2"
[efg@fedori ~]$ mkdir dir2
and want to make sure that 'secretfile' is accessible from both dir1 and dir2.
(though secretfile has 0 size ,just assume it has around 5GB data thus size=5GB)If you cp the 5GB secretfile to dir2 and dir1.On the whole it consume 15GB (5 each for secretfile at home,dir1
and dir2)
Since file contents are same, its not a good idea to let them occupy 15GB.
Here is where links will be useful.How about creating links from dir1 and dir2 instead of cp them?
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 1 efg efg 0 2011-05-04 22:40 secretfile
links are created by ln command like -
[efg@fedori ~]$ ln secretfile dir1/secretfile
check the dir1 contents -
[efg@fedori ~]$ ls -lt dir1/secretfile
-rw-------. 2 efg efg 0 2011-05-04 22:40 dir1/secretfile
yeah,we have it.
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 2 efg efg 0 2011-05-04 22:40 secretfile
now wait a second , rewind 10 lines - check the first ls
it shows "1" before owner "efg" - right? But now check the last ls output
it shows "2". Why?
That's because secretfile is being linked from two location now after ln command.
One from original home and another from dir1.So now if we create another link to dir2 - like
[efg@fedori ~]$ ln secretfile dir2/secretfile
Now the link count increased by 1,its new count 2+1 which is ..hmmmm...where the heck is my calculator ?!
yeah..got it..its 3.So the ls should shows as 3.
[efg@fedori ~]$ ls -lt dir2/secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 dir2/secretfile
[efg@fedori ~]$ ls -lt dir1/secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 dir1/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 3 efg efg 0 2011-05-04 22:40 secretfile
and it does Image may be NSFW.
Clik here to view.![]()
Now If you change the secretfile at home ,this will be reflected at dir1 and dir2.
For example , I'll append some data
[efg@fedori ~]$ echo "This is some random data for the file" >> secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 3 efg efg 38 2011-05-04 22:42 secretfile
Okay,now go and check dir1 and dir2 ,
`
[efg@fedori ~]$ cat dir2/secretfile
This is some random data for the file[efg@fedori ~]$ cat dir1/secretfile
This is some random data for the file
`
Since all the links has single copy of the file,whatever you do via one link will be reflect via remaining lin
ks too.
Similarly , if you change from dir2 it gets reflected at dir1 and home and so on.Say if you delete the file from dir1 ,the count will decrease by 1 and count will be 2.
[efg@fedori ~]$ rm -rf dir1/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 2 efg efg 38 2011-05-04 22:42 secretfile
true.
and If you delete it from dir2 too,then count will become 1.
[efg@fedori ~]$ rm -rf dir2/secretfile
[efg@fedori ~]$ ls -lt secretfile
-rw-------. 1 efg efg 38 2011-05-04 22:42 secretfile
true again
If you delete it from home ,then file will be gone forever.Above linking is known as "hard link". There another linking method known as "soft link".
The major different between the two is , hard link is twice as heavy as soft link Image may be NSFW.
Clik here to view...hehe just kidding.
Lets begin with an example for soft link,
[efg@fedori ~]$ ln -s /home/efg/secretfile dir1/sym_file
we used the same ln command but added an option "-s" to create symbolic link or soft link.Here is the dir1 contents , notice the first letter 'l' before permissions ,which refers to the file named "sy
m_file" is soft link.
[efg@fedori ~]$ ls -l dir1/
lrwxrwxrwx. 1 efg efg 21 2011-05-07 22:52 sym_file -> /home/efg/secretfile
And also note that secretfile's link count didn't increase to "2".
[efg@fedori ~]$ ls -l secretfile
-rw-------. 1 efg efg 38 2011-05-06 09:27 secretfile
Though, you can access the file ,modify it via "sym_file", removing "sym_file" won't remove the original file.
What i mean,is
[efg@fedori ~]$rm -rf dir1/sym_file1
won't affect secretfile by any means.But if you delete the original file first ,then "sym_file" is known as "broken link" since it points to a file
which does not exist.
Posts: 1
Participants: 1