Link Commands (Demo)

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello, Cybrarians.
00:00
Welcome back to the Linux+ course here at Cybrary.
00:00
I'm your instructor Rob Gels.
00:00
In today's lesson, we're going to be
00:00
talking about link commands.
00:00
Now upon completion of today's lesson,
00:00
you're going to be able to understand inodes and
00:00
symbolic and hard links and
00:00
the relationship between all of those.
00:00
We're going to use the lm command to create
00:00
symbolic and hard links and we're going to
00:00
learn how we can use the unlink command.
00:00
Every Linux file has a name,
00:00
data blocks, and an index number
00:00
which is known as an inode.
00:00
Linux uses the inode number when it's
00:00
trying to access the file and the file is
00:00
assigned a unique inode by
00:00
the file system anytime a file is created.
00:00
A hard link has just one index number,
00:00
but it has two different files
00:00
pointing to that index number to that inode.
00:00
Both file names just point
00:00
this same inode on this system.
00:00
That is essentially a look
00:00
here at what the hard link looks like.
00:00
Filesystem has one inode.
00:00
We have File 1 and File 2 pointed to it.
00:00
A symlink by comparison,
00:00
which is also known as soft link or a symbolic link,
00:00
points to another file with its own inode.
00:00
This file actually holds the data in question.
00:00
We have File 1 with inode number, whatever,
00:00
and that actually holds the data,
00:00
and then File 2 is just a symbolic link
00:00
that points to that file.
00:00
It tells you, hey, go to this file
00:00
to get actually actual data.
00:00
This is just a pointer to where that file lives.
00:00
These files both have their own inode number.
00:00
Let's do a little bit of a review here.
00:00
We're going to compare these side-by-side in hard links,
00:00
both files share the same inode number.
00:00
In soft links, that data file has
00:00
one inode number and
00:00
symlink has a different inode number.
00:00
In hard links, both links share the same data because
00:00
remember they're just pointing to the exact same inode.
00:00
In soft links the data file
00:00
and symlink do not share the data.
00:00
The symlink is just a pointer to
00:00
the actual data that lives in the data file.
00:00
In hard links, both files
00:00
have to be on the same file system,
00:00
when you're using a soft link or a symlink,
00:00
files can be on different file systems.
00:00
You can have the data file
00:00
on one file system and then you can
00:00
symlink to where that data is
00:00
located from a different file system.
00:00
In hard links, if you delete the hard link,
00:00
it deletes all of
00:00
the files and all of the data. It's gone.
00:00
In soft links, you can delete the symlink,
00:00
the link that's pointed to
00:00
the actual data file and not delete the data file,
00:00
it just removes the link.
00:00
That might just be about as clear as mud.
00:00
Why don't we take a look at this a little
00:00
closer with some demo time.
00:00
Here we are back over in our CentOS environment,
00:00
do some demo work and just
00:00
understand to do some links are
00:00
hardly as we actually use the same command.
00:00
It's just ln, It's really easy.
00:00
The ln command is used to create links.
00:00
To create a hard link where we can do,
00:00
first of all is make sure that we're in
00:00
my home directory here.
00:00
Let's mess around with the Ubuntu
00:00
file that we have right here.
00:00
If we're going to create a hard link for this,
00:00
we can do an ln Ubuntu file
00:00
and we're going to create a hard link to this.
00:00
We'll create a link called
00:00
Ubuntu file and we'll give it the number two.
00:00
Now if we do an ls, we see
00:00
Ubuntu file and Ubuntu file 2 right here.
00:00
Now, if we want to,
00:00
we can see the inode numbers for these
00:00
by using the -i option.
00:00
We're going to do a long listing so
00:00
>> ls -il to see the inode number.
00:00
>> Do ls -il and we want to do that on
00:00
all Ubuntu file star.
00:00
All of Ubuntu files.
00:00
We can see that they both share the same inode number,
00:00
that's this number here at the beginning, 137.
00:00
Now if we want to create a soft link,
00:00
we use the ln command, but
00:00
it's just a little bit different.
00:00
We have to use the -s option.
00:00
S is for soft link or symlink
00:00
or just essentially a soft link to get to this file.
00:00
If we wanted to symlink back to that same file,
00:00
we could do an ln -s on Ubuntu file.
00:00
We'll say that we want this file to be called symlink.
00:00
Now if we run an ls -il on the Ubuntu files,
00:00
we see that they have the inode number of 137,
00:00
but if we run an ls -il on the same link file,
00:00
we see that it has an unknown number of 138.
00:00
The symlink, which is this,
00:00
and the source file which is this,
00:00
have different inode numbers.
00:00
We can also see that this symlink shows an arrow
00:00
pointing to the Ubuntu file
00:00
because it's really all same like this.
00:00
It's just a pointer to where the data actually lives,
00:00
and this shows the relationship between the two files.
00:00
The symlink points to the actual data file.
00:00
Now if we wanted to remove a symlink,
00:00
we just simply delete it.
00:00
We do rm symlink and it's gone.
00:00
If you run an ls -il,
00:00
it's not there anymore because it's not in the system.
00:00
But if we wanted to remove
00:00
this association between these hardlinks that we have,
00:00
we actually have to use another command called unlink.
00:00
If we just start removing files,
00:00
things can happen very badly
00:00
because they're all pointing to the same file.
00:00
If I were to remove Ubuntu file or Ubuntu file 2,
00:00
it's removing the same inodes
00:00
>> both of them get impacted.
00:00
>> If we wanted to remove Ubuntu files 2,
00:00
that hard link without impacting Ubuntu file,
00:00
we use the command unlink,
00:00
and we just type unlink Ubuntu file 2,
00:00
>> and then hit "Enter".
00:00
>> Now when we run the ls -il command,
00:00
we only have Ubuntu file.
00:00
Ubuntu file 2 has been removed and we can
00:00
do an ls and see that that's not there.
00:00
We just have Ubuntu file no more of Ubuntu file 2.
00:00
But with that, we've reached end of this lesson and in
00:00
this lesson we covered the relationship between inodes,
00:00
hard links and symbolic links.
00:00
We also talked about linking and unlinking
00:00
files using ln and unlink respectively.
00:00
Thank you so much for being here and I look
00:00
forward to seeing you in our next lesson.
Up Next