Sunday, May 30, 2004

Hardlink and Symlinks

HARDLINKS AND SYMLINKS


Today we will test your virtual imagination capabilities !

The main difference between hardlinks and symlinks ( symbolic or softlinks ) are:
1.) You cannot make a hardlink to a directory.
2.) If you remove the original file of a hardlink the link will still show you the content of the file.
3.) A symlink can link a directory
4.) The symlink is useless as you remove the original file.

All this might seem hard to grasp, but let´s explain:


Hardlinks

A little experiment to show the case.

CODE
$ mkdir Test

( Making a new directory for our test )

CODE
$ cd Test

( Move in the directory )

CODE
$ vi fileA

( Make a file called fileA )

<>
Type in some funny lines of text
<>
<> ( save the file )

So, we made a ¨fileA¨ in a new directory called ¨Test¨ in your /home.

CODE
$ ln fileA fileB

( Making a hardlink )

CODE
$ ls -il fileA fileB

( The ¨i¨ argument will show the inode on the HD )
This is what you get:

QUOTE (Text @ Screen)
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 fileA
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 fileB


Here you can see that both fileA and fileB have the same inode number ( 1482256 ), also both files have the same file permissions and the same size, because that ´size´ is on the same inode it does not consume any extra space on your HD !

Now if we would remove the original ¨fileA¨

CODE
$ rm fileA

and have a look at the content of the ¨link¨ fileB

CODE
$ cat fileB

you will still be able to read the funny line of text you typed. ( MAGIC ! )




Symlinks

Staying in the same test directory as above we make a symlink:

CODE
$ ln -s fileB fileC
$ ls -il fileB fileC

This is what you´ll get:

QUOTE (Text @ Screen)
1482256 -rw-r--r-- 1 bruno bruno 21 May 5 15:55 fileB
1482226 lrwxrwxrwx 1 bruno bruno 5 May 5 16:22 fileC -> fileB


You´ll notice the inodes are different and the link got a ¨l¨ before the rwxrwxrwx . The link has different permissions than the original file because it is just a symbolic link, its real content is just a string pointing to the original file. The size of the symlink ( 5 ) is the size of it´s string. ( The "-> fileB" at the end shows you where the link points to )

CODE
$ cat fileB

and

CODE
$ cat fileC

Will show the same funny text.

Now if we remove the original file:

CODE
$ rm fileB

and check the Test directory

CODE
$ ls

you will see the link fileC is still there, but if we do

CODE
$ cat fileC

it will tel you that there is no such file or directory !! Though

CODE
$ ls -il fileC

will still give you:

QUOTE (Text @ Screen)
1482226 lrwxrwxrwx 1 bruno bruno 5 May 5 16:22 fileC -> fileB

But the link is obsolete ! ( hope you´re still with me )

O.K. The test is over, you can delete the Test directory

CODE
$ cd ..
$ rm -rf Test

( ¨r¨ stands for recursive ¨f¨ is force )


WARNING: "rm -rf" is very powerfull, if ever someone wants to play a trick on you and tells you to do "rm -rf /" as root, you might loose all your files and directories on your / partition !!!

Not dizzy yet ? Wait till next week when we come to the real stuff !


Sriram

Thursday, May 20, 2004

File Permissions

CODE
$ ls -l /etc/gnome/gnomerc


This is what you will get:

QUOTE (Text @ Screen)
-rwxr-xr-x 1 root root 484 Feb 25 14:08 /etc/gnome/gnomerc


This does look a bit complicated but it really isn´t. The first 10 characters are built up like this:

- | rwx | r-x | r-x

The first one tells you whether it is a file ( - ) a directory ( d ) or a link ( l )
The next three are for the "user" 'r'ead 'w'rite and e'x'ecute. The next three for the "group" and the last three for all "others"

The next 1 stands for the number of links to the file. The owner. The group. The size in bytes. The date and time of the last modification to the file. And the name of the file.