my understanding of hard links and soft links

Discuss the security implications of the various flavors of linux and unix

my understanding of hard links and soft links

Post by ghostheadx2 on Thu Feb 18, 2016 12:53 am
([msg=91642]see my understanding of hard links and soft links[/msg])

I'm reading a linux book right now, it's called "The Linux Command Line." I ran into the concepts of hard links and soft links. It says that when we create a hard link, we create an "additional directory entry for the file." So then, does that mean that it makes a new entry in the folder for the link that links the two files? I know hard links between two files, the two files both have to be on the same disk partition. From my understanding, hard linked files can only link to other files.

Soft links: creates a separate file so that when you write to one file, yes it writes to the other because the link file is a separate program that tells it to do so, but it is a lot like a windows shortcut where you click on a shortcut to a file and it opens that other file. Thus, if a soft link is deleted, its like deleting a windows short cut in that it won't delete the file. Hard links will delete the file with the deletion of the shortcut.

Am I right?
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: my understanding of hard links and soft links

Post by boriz666 on Thu Feb 18, 2016 4:04 am
([msg=91645]see Re: my understanding of hard links and soft links[/msg])

Greetings,

Hard links are pointing to the actual inode of the file, so if the file is renamed or moved
a hard link will still point to it. You can only have hard links within the same filesystem
due to the fact that they refer to the inode of the file and not the name/location of the
file.

Soft links are based on the path of the file and can then point to files on other drives,
if you delete the soft link, you wont delete the files, you will delete the files if you
delete a hard link as it points to the physical file and not the path of a file.

So yeah it seems you got it right!

A fun thing to note is that some programs are made so their function is dependant on
the name which they are executed as.

say you have a program called: supercommands

and you make a link to it (soft in this example)
ln -s /usr/bin/supercommands /usr/bin/displayfiles

then when you call displayfiles, the supercommand ofc gets called, but it can see
which filename it was called with and will issue the correcponding functionality.

This "trick" is used in some command shells in linux, in order to make the whole
thing more compact and then you only have the commands (as links) you need
on your system so its not so cluttered.
You normally use hardlinks for this due to the fact that they are faster to resolve
to the actual file and take up less space.

Also if you want to see the inode of a file you can use the ls command like this:
ls -ali

Then you get the inode and also a count of how many links are to the given files
in the listing.
boriz666
Experienced User
Experienced User
 
Posts: 99
Joined: Tue Mar 24, 2015 11:53 am
Blog: View Blog (0)


Re: my understanding of hard links and soft links

Post by ghostheadx2 on Fri Feb 19, 2016 1:44 am
([msg=91649]see Re: my understanding of hard links and soft links[/msg])

Just curious, here's the definition of "inode":

An inode is a data structure on a filesystem on Linux and other Unix-like operating systems that stores all the information about a file except its name and its actual data. A data structure is a way of storing data so that it can be used efficiently.


But your saying the inode is the file. Or is the inode the METADATA of the file, like the information stored besides the file name and contents, its everything ABOUT the file literally meaning metadata.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: my understanding of hard links and soft links

Post by Ayr3s on Fri Feb 19, 2016 6:56 am
([msg=91652]see Re: my understanding of hard links and soft links[/msg])

Yes, in a way. Inode data is data about how and where the filesystem stores the file, not really about the data itself. Metadata for the file itself, if present, is usually in the header of the file, not in the inode. Two identical files will have the same header and will contain the same data, but they will have different inodes. Hard links to a file will have the same inode.
Here, two identical files, same hashes, different inodes:

Code: Select all
09:40 [ayr3s@arch] ~
$>> echo "1234" | tee file{1,2}.txt
1234
09:40 [ayr3s@arch] ~
$>> cat file{1,2}.txt
1234
1234
09:40 [ayr3s@arch] ~
$>> md5sum file{1,2}.txt
e7df7cd2ca07f4f1ab415d457a6e1c13  file1.txt
e7df7cd2ca07f4f1ab415d457a6e1c13  file2.txt
09:40 [ayr3s@arch] ~
$>> stat file{1,2}.txt
  File: 'file1.txt'
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 14290256    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ ayr3s)   Gid: ( 1000/ ayr3s)
Access: 2016-02-19 09:40:31.223326079 -0200
Modify: 2016-02-19 09:40:24.666578291 -0200
Change: 2016-02-19 09:40:24.666578291 -0200
Birth: -
  File: 'file2.txt'
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 14290744    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/ ayr3s)   Gid: ( 1000/ ayr3s)
Access: 2016-02-19 09:40:31.223326079 -0200
Modify: 2016-02-19 09:40:24.666578291 -0200
Change: 2016-02-19 09:40:24.666578291 -0200
Birth: -


I like to think of soft links as 'teleporting machines', they sit in one place and throw you out to somewhere else, and hard links as worm holes, with the file inside, it is in more than one place at the same time.
User avatar
Ayr3s
New User
New User
 
Posts: 29
Joined: Mon Feb 02, 2015 2:34 pm
Blog: View Blog (0)


Re: my understanding of hard links and soft links

Post by ghostheadx2 on Mon Feb 29, 2016 1:18 am
([msg=91719]see Re: my understanding of hard links and soft links[/msg])

Ayr3s wrote:Yes, in a way. Inode data is data about how and where the filesystem stores the file, not really about the data itself. Metadata for the file itself, if present, is usually in the header of the file, not in the inode. Two identical files will have the same header and will contain the same data, but they will have different inodes. Hard links to a file will have the same inode.
Here, two identical files, same hashes, different inodes:



So your saying the different names mean different references in memory, thus they are different inodes.
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)



Return to *nix

Who is online

Users browsing this forum: No registered users and 0 guests