Friday, December 28, 2012
Wednesday, December 26, 2012
Thursday, December 6, 2012
Symbolic link 101
bash-4.1$ stat sample
File: `sample'
Size: 5 Blocks: 68 IO Block: 65536 regular file
Device: 1bh/27d Inode: 502918152 Links: 1
Access: (0600/-rw-------) Uid: (97399/bruce) Gid: ( 34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500
bash-4.1$ ln -s sample sample1
bash-4.1$ stat sample
File: `sample'
Size: 5 Blocks: 68 IO Block: 65536 regular file
Device: 1bh/27d Inode: 502918152 Links: 1
Access: (0600/-rw-------) Uid: (97399/bruce) Gid: ( 34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500
So, the link count did not increase. If you don't use the -s flag and create a hard link, the link count increases by 1 on each link operation.
Use the readlink command to read contents of a symbolic link
lrwxrwxrwx. 1 foo admin 5 Dec 6 23:21 avg_s.c -> avg.c
$ readlink avg_s.c
avg.c
So, the symbolic link contains the path to the file it is pointing to.
Soft link pros:
1) Can create link to files in other file systems or remote file systems.
2) Can create link to directories. Hard link to directories are not allowed in Unix.
3) Editing file using text editors like vim is more reliable. If we have hard links the edits to file inode are done in place make it prone to inconsistency on failures.
Cons:
1) Wastes an inode and takes space on disk.
2) Indirection. Slower.
Hard link pros:
1) Faster access as there is no indirection
2) Protection from accidental deletion.
3) Versioning/Backup software can easily make sure that file is backed up only once.
File: `sample'
Size: 5 Blocks: 68 IO Block: 65536 regular file
Device: 1bh/27d Inode: 502918152 Links: 1
Access: (0600/-rw-------) Uid: (97399/bruce) Gid: ( 34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500
bash-4.1$ ln -s sample sample1
bash-4.1$ stat sample
File: `sample'
Size: 5 Blocks: 68 IO Block: 65536 regular file
Device: 1bh/27d Inode: 502918152 Links: 1
Access: (0600/-rw-------) Uid: (97399/bruce) Gid: ( 34/ UNKNOWN)
Access: 2013-01-20 18:18:31.624217805 -0500
Modify: 2013-01-20 18:18:31.624217805 -0500
Change: 2013-01-20 18:18:55.152443117 -0500
So, the link count did not increase. If you don't use the -s flag and create a hard link, the link count increases by 1 on each link operation.
Use the readlink command to read contents of a symbolic link
lrwxrwxrwx. 1 foo admin 5 Dec 6 23:21 avg_s.c -> avg.c
$ readlink avg_s.c
avg.c
So, the symbolic link contains the path to the file it is pointing to.
Soft link pros:
1) Can create link to files in other file systems or remote file systems.
2) Can create link to directories. Hard link to directories are not allowed in Unix.
3) Editing file using text editors like vim is more reliable. If we have hard links the edits to file inode are done in place make it prone to inconsistency on failures.
Cons:
1) Wastes an inode and takes space on disk.
2) Indirection. Slower.
Hard link pros:
1) Faster access as there is no indirection
2) Protection from accidental deletion.
3) Versioning/Backup software can easily make sure that file is backed up only once.
How to get the contents of a directory?
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <assert.h>
int main(int argc, char *argv[])
{
DIR * dp = opendir(".");
assert(dp != NULL);
struct dirent *d;
while ((d = readdir(dp)) != NULL)
{
printf("%d %hd %ld %s\n", (int)d->d_type, d->d_reclen, (long) d->d_ino, d->d_name);
}
closedir(dp);
return 0;
}
Subscribe to:
Posts (Atom)