HardLinks and Softlinks

How do i make a hardlink readable,writable, and executable by me?

I am kinda hoping for a command i can use

Also, i was wondering what file type are hardlinks and softlinks?Are they directories?

Hardlink

ln -f <src> <dst>
chmod 744 <yourfile>

--ahamed

1 Like

thanks for the response but what is the <src> and <dst> mean. ln -f didnt work

Lets say you want to create a hardlink from a source file located at /tmp/file to a destination of /var/file

#Create the hard link
ln -f /tmp/file /var/file

#Give the permission
chmod 755 /var/file

--ahamed

FYI:-

A hard link can point only to a file and not to a directory, and only to a file existing on the same file system where the link itself is. 
A symbolic link, in contrast, can point to a file or a directory and can span across file systems. 
Furthermore, you cannot create a hard link for a non-existent file, whereas you can create a symbolic link for it. 
Links can be removed with the same rm command that is used to remove files.

to know how to use "ln" command do the below in your system:

man ln

BR

1 Like

ok its starting to make sense......can i not just say the file name instead of the directory? and what permissions will it give me?

you can control the permission by using "chmod" command , to create a soft link use "ln -s" command

---------- Post updated at 21:17 ---------- Previous update was at 21:15 ----------

Creating Hard and Symbolic Links
To create a symbolic link or a hard link, you use the same command name, In, which has the following syntax:

In [-fns] <source> [<target>]


Hard link is the default output of the ln command-that is, if you issue the ln command without the - s option, a hard link will be created.
 

In this command, <source> is the original file and <target> is the link that will be created and linked to the <source>. The options for the command are listed here:

-f (force). Link the file without questioning the user.

-n. If the <target> is an existing file, do not overwrite the content of the file. The -f option overrides this option.

-s. Create a symbolic link. The default is a hard link.

The hard links and symbolic links are compared in Table 4-2 

Table 4-2: Comparison of hard and soft links  Characteristic
 Hard Link
 Soft Link
 
Existence of original file
 You cannot create a hard link to a file that does not exist.
 You can create a symbolic link to a file that does not exist.
 
File systems
 A hard link cannot span file systems-that is, the link and the file it points to have to be in the same file system.
 A soft link can span file systems.
 
Kind of original file
 A hard link can only point to a file that is not a directory.
 A soft link can point to a file or a directory.
 
I-node
 A hard link has the same inode number as the file it points to.
 A soft link has a different inode from the file it points to.
 

A soft link is an directory entry that points, by name, to another directory entry, which may or may not exist. Since it's pointing by name the target can even reside on a different mount point.

Hard links, on the other hand, are directory entries that point to an inode used by another directory entry, and is still valid if the original entry is removed. Since all access information but the name is saved with the inode, both entries will always have the same access rights, timestamps, and user/group information. Also, due to sharing the same inodes, a hard link can't cross the boundary between mount points.

I'll initially point you to the ln man page for all of the good details

Man Page for ln (FreeBSD Section 1) - The UNIX and Linux Forums

The -f option (force) has a different description depending on your flavour of *NIX. In general it causes the link to be created regardless. At least one of the man pages indicates that it "removes existing destination files", so see the version of the man page for your system to know exactly what the option should do.

I've always thought of the link command in this manner:

 ln <existing-file> <new-link-to-existing> 

rather than source and destination. So, if you have a file foo, and want to create a link l_foo, the command would be:

ln foo l_foo

Whether or not you need the -f option depends on your system, whether or not you have write access to the existing file and maybe other factors.

A hard link is another directory reference to the exising file. It can be in the same directory as the first/other reference(s) or it can be in a different directory as long as that directory exists on the same physical filesystem. After you create the hard link to foo, executing ls -al foo will show that the link count (usually the value in column two of the output) has increased (probably 2).

Hope this makes things a bit clearer.

kindly refer to my reply in my second post to see the comparison between soft and hard links

Ok great thanks for the advice