- In this video, we are going to talk about path names.
So what is there to know about path names?
Well, you can specify your paths in two ways.
As an absolute path.
An absolute path contains the full name
from the root directory to a file.
An example is /var/log/messages.
The nice thing about an absolute path
is that it is very clear what you are referring to.
Can't be any doubt about it.
But sometimes it's too much typing.
And for that reason, you might be better off
using a relative path.
Relative path is related to the current directory,
and it contains the rest that is needed to get to a file,
like log/messages, which will work
if the current directory is set to /var.
Now in relative paths, you can use dot dot
for one directory up.
If the current directory is /var/cache, for instance,
../log/messages points to /var/log/messages file.
But if I may give you a tip, use absolute paths
to avoid confusion.
Now before I'm going to demonstrate,
let me make a quick drawing
so that I can visualize what exactly it is
that we are going to do.
In this video, I would like to visualize these relative
and absolute path names.
So let's draw a small directory structure.
Start with the root directory.
In the root directory, we have etc,
we have tmp,
and in tmp we have data.
Okay, so let's imagine that in etc, we have this file,
and the name of this file is passwd.
And in tmp, we have this file,
and the name of this file is hosts
because we copied it over here.
Now just imagine that we are currently
in the data directory.
The data directory, represented by a dot.
The dot is the current directory.
And I want to copy this file up to this location.
And I'm right here.
Then what can we do?
Well, we can use these relative file names
and use a command like cp dot dot.
If you use dot dot, then it brings us up
from the data directory to the tmp directory
as our current directory.
And if I want to use relative file names to copy it,
I need to go up one more level.
So another dot dot, then I get into the root directory,
and then I can specify /etc/passwd to dot.
But hey, is that the most accurate way of doing it?
Well, I don't think so, because in this way
you probably would be better off using absolute file names.
I mean, what's the sense if you go all the way down
to the root directory anyway?
It would make sense if you want to copy over the file hosts
to the current directory.
In that case, you can use cp.. to go up one level,
that brings you here.
Then you can refer to the file hosts,
and then you can copy it to the dot directory.
And that's a little demo of how you can work
with these absolute and relative file names.
Let me show you what this looks like on a computer.
All right, I am going to create the directory data.
Notice that this is a relative path.
I'm in the tmp directory, and I'm creating data.
That will create a subdirectory.
And I am going to copy /etc/hosts to dot
so that in the data directory we have /etc/hosts.
And I'm going to copy /etc/passwd to /data.
Is that what I want?
No, because I want to create it to the subdirectory
in this tmp directory.
So I need a relative path, and this is the relative path.
Now I'm going in the data directory,
and in the data directory I see my passwd.
Pwd might be convenient as well.
That is where you can see what exactly is going on.
So if I use cp passwd to dot dot,
can you guess what is going to happen?
I'm going to copy passwd to one level up.
And if I want to verify, I'm using ls..,
and ls.. is showing a lot of stuff,
including this data directory right here,
and this passwd directory right here.
And that is how you use absolute and relative path names.