Académique Documents
Professionnel Documents
Culture Documents
https://www.cs.duke.edu/courses/cps110/fall01/
How to preserve symbolic links especially when you tar the files is explained below. We all know how to use tar command. Below option shows the
way to preserve links when you archive.
Example
# tar -cvhf archive.tar /backupfolder
Note :
What you need to remember is that when you tar archives a symbolic link, it writes a record to the archive naming the target of the link. When you use
-h with -c it causes tar to archive the files symbolic links point to, instead of the links themselves. When this option is used, when tar encounters a
symbolic link, it will archive the linked-to file, instead of simply recording the presence of a symbolic link.
Hard Link acts like a mirror copy of the original file. These links share the same inodes. Changes made to the original or hard linked file will reflect the
other. When you delete hard link nothing will happen to the other. Hard links can't cross file systems.
Soft Link is actual link to the original file. These Links will have a different Inodes value. Soft link points to the original file so If original file is deleted the
soft link fails. If you delete the soft link, nothing will happen to file. The reason for this is, the actual file or directorys inode is different from the "soft link"
created file's inode, Hard links can cross file systems.
Hard links
$ mkdir Test
$ cd Test
$ touch sample1
Now, create a hard link to sample1. Name the hard link sample2.
Unrestricted
$ ln sample1 sample2
Display inodes for both files using I argument of the ls command.
$ ls -il sample1 sample2
This is what you get:
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 sample1
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 sample2
From the output you can notice that both sample1 and sample2 have the same inode number (1482256). Also both files have the same file permissions
and the same size.
Now Remove the original sample1
$ rm sample1
After removing hard link just have a look at the content of the "link" sample2.
$ cat sample2
You will still be able to see the content of the file.
Symbolic links
Create soft link for the file sample2.
$ ln -s sample2 sample3
Display inodes for both using i argument of ls command.
$ ls -il sample2 sample3
This is what you'll get:
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
From the output you can notice that the inodes are different and the symbolic link got a "l" before the rwxrwxrwx. The permissions are different for the
link and the original file because it is just a symbolic link.
Now list the contents:
$ cat sample2
$ cat sample3
Now remove the original file:
$ rm sample2
And then check the Test directory:
$ ls
It will still display symbolic link sample3 but if you try to list the contents It will tell you that there is no such file or directory.
$ cat sample3
Now you know about some of the key differences between hard links and soft links to make it easier to access files and run programs.
Unrestricted
# rm linkname
or
# unlink linkname
r : read permission
For a file r means you will be able to read the file.
For a directory, the permission r means you will be able to list the contents of the directory.
w: write permission
For a file w means you will be able to edit the file.
For a directory, the permission w means you will be able to add, delete or rename files in that directory.
Unrestricted
x : execute permission
For a file x means you will be able to execute the program or shell script of that file.
For a directory, the permission x means you will be able to move to that directory (cd to the directory).
So, the permission drwxrwxrwx on tmp directory sets read, write and execute permissions for user, group and others. And the permission -rw-rw-r
on file1 permits the user sam to read and edit the file, all users belong to the group admin can also read and write the file and everybody else can just
read the file but not write or execute it.
Example 1:
If you want to set the permission of a file such that the user should be able to read, write and execute the file, group and others should only be read
and execute the file, the permission should be like -rwxr-xr-x.
We can now find the OCTAL-MODE need to be used for setting the permission -rwxr-xr-x.
For user part -> rwx = 4+2+1 = 7
For Group -> r-x = 4+0+1 = 5
For others -> r-x = 4+0+1 = 5
Hence, the command should be,
chmod 755 filename
Example 2:
If you want to set the permission of a file such that the user should be able to read and write the file, the group should be able to read the file and
others should not have any access to the file, permission should be like -rw-r-----.
For user -> rw- = 4+2 = 6
For group -> r-- = 4+0+0 =4
For others -> --- = 0+0+0 =0
Hence the command should be,
chmod 640 filename
Unrestricted
Example 3:
If you want to temporarily disable a file, you need to set the permission of the file such that nobody will be able to access the file. In order to set this
permission ----------, you need to execute the command,
chmod 000 filename
Example 4:
If you want to give full permissions (rwxrwxrwx) to a directory and all sub directories and files in it, you can use chmod recursively as follows.
chmod R 777 dir_name
Unrestricted