Вы находитесь на странице: 1из 5

Linux basics training:

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.

What are Hard Links


1. Hard Links have same inodes number.
2. ls -l command shows all the links with the link column shows number of links.
3. Links have actual file contents
4. Removing any link, just reduces the link count, but doesn't affect other links.
5. You cannot create a hard link for a directory.
6 If original file is removed then the link will still show you the content of the file.

What are Soft Links


1. Soft Links have different inodes numbers.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Soft Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but removing original file, the link becomes "dangling" link which points to nonexistent file.
5. A soft link can link to a directory.
Let us try to see some experimental differences.Make a new directory called Test and then move into it and create new file. Simply follow below steps.

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.

Delete Symbolic Link File


Below two commands are used to delete link.

Unrestricted

# rm linkname
or
# unlink linkname

Linux File Permissions


In Linux operating system, everything is organized in the form of files and directories. By setting permissions on files and directories, one can make
sure that only authorized users are allowed to access a specific data. Each file in Linux is owned by a user and group. The user is the one that creates
the file and group is the one to which the user (owner of the file) belongs to.
For example, you can list the files under the directory /home/sam as follows.
ls l /home/sam
drwxrwxrwx 3 sam admin 80 2012-08-20 21:37 tmp
-rw-rw-r-- 1 sam admin 8187 2012-08-25 13:35 file1
-rwxr-x--- 1 sam admin 10348 2012-08-21 20:31 file2
Here, the first field shows the file permissions, third column shows the owner (user) of the file and the fourth column shows the group of the file. We
can check the file permission field in detail.
To understand the file permissions easily, we can divide the permission bits into 4 parts.
[d][rwx][rwx][rwx]
The first part can have any of the following value.
d : directory
- : regular file
l : symbolic link
p : named pipe
s : Unix domain socket
c : character device file
b : block device file
The second part shows the allowed permissions for the user (owner of the file/directory). Third part shows the allowed permissions for the users that
belong to the group of the file/directory and the fourth part shows the permissions for everybody else (who doesnt belong to the user or group).
Permissions need to be set for the following modes.

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.

Changing File permissions


The linux command chmod can be used to change the permission of a file or directory. The basic syntax of chmod command is as follows.
chmod [option] OCTAL-MODE filename
The value of OCTAL-MODE is basically a 3 digit number where first digit refers to the permissions for the user, second digit refers to the permissions
for the group and third digit refers to the permissions for others (anybody other than the user and group). Each digit can be calculated using the
following table
r (Read) 4
w (write) 2
x (execute) 1
-(no permission) 0

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