What is absolute path in linux

Absolute and Relative Pathnames in UNIX

A path is a unique location to a file or a folder in a file system of an OS.A path to a file is a combination of / and alpha-numeric characters.

Absolute Path-name

An absolute path is defined as the specifying the location of a file or directory from the root directory(/).
To write an absolute path-name:

  • Start at the root directory ( / ) and work down.
  • Write a slash ( / ) after every directory name (last one is optional)

For Example :

will work only if the fie “abc.sql” exists in your current directory. However, if this file is not present in your working directory and is present somewhere else say in /home/kt , then this command will work only if you will use it like shown below:

In the above example, if the first character of a pathname is /, the file’s location must be determined with respect to root. When you have more than one / in a pathname, for each such /, you have to descend one level in the file system like in the above kt is one level below home, and thus two levels below root.

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory.

Relative path

Relative path is defined as the path related to the present working directly(pwd). It starts at your current directory and never starts with a / .

To be more specific let’s take a look on the below figure in which if we are looking for photos then absolute path for it will be provided as /home/jono/photos but assuming that we are already present in jono directory then the relative path for the same can be written as simple photos.

Using . and .. in Relative Path-names

UNIX offers a shortcut in the relative pathname– that uses either the current or parent directory as reference and specifies the path relative to it. A relative path-name uses one of these cryptic symbols:

.(a single dot) - this represents the current directory. ..(two dots) - this represents the parent directory.

Now, what this actually means is that if we are currently in directory /home/kt/abc and now you can use .. as an argument to cd to move to the parent directory /home/kt as :

$pwd /home/kt/abc $cd .. ***moves one level up*** $pwd /home/kt

NOTE:Now / when used with .. has a different meaning ;instead of moving down a level,it moves one level up:

$pwd /home/kt/abc ***moves two level up*** $cd ../.. $pwd /home

Example of Absolute and Relative Path

Suppose you are currently located in home/kt and you want to change your directory to home/kt/abc. Let’s see both the absolute and relative path concepts to do this:

    Changing directory with relative path concept :

$pwd /home/kt $cd abc $pwd /home/kt/abc
$pwd /home/kt $cd /home/kt/abc $pwd /home/kt/abc

This article is contributed by Dimpy Varshni. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

Absolute vs Relative Path in Linux: What’s the Difference?

In this essential Linux learning chapter, know about the relative and absolute paths in Linux. What’s the difference between them and which one should you use.

Path is one of the most essential concepts in Linux and this is something every Linux user must know.

A path is how you refer to files and directories. It gives the location of a file or directory in the Linux directory structure. It is composed of a name and slash syntax.

As a user, you’ll have to use the path when you want to access a certain file or directory or when you have to give the location of a file or directory to a command or script.

cat /home/abhishek/scripts/my_script.sh

Remember, if the path starts with slash «/», the first slash denotes root. The rest of the slashes in the path are just separators. Beginners often get confused between the root slash and the separator slashes.

Paths in Linux

In the above diagram, you have the first path that starts with root slash. There is another path that doesn’t start with / (i.e., root).

Both are correct. The first one is absolute path and the second one is relative path. Let’s have a detailed look at them.

Absolute and Relative Path in Linux

The Absolute path always starts from the root directory (/). For example, /home/abhishek/scripts/my_scripts.sh.

A relative path starts from the current directory. For example, if you are in the /home directory and you want to access the my_scripts.sh file, you can use abhishek/scripts/my_scripts.sh.

Understanding the difference between absolute and relative paths

You know that the directory structure in Linux resembles the root of a tree. Everything starts at root and branches out from there.

Now imagine that you are in the directory abhishek and you want to access the my_scripts.sh file.

The absolute path is depicted in the green dotted line and the relative path is depicted in the yellow dotted lines.

Absolute vs relative path in Linux

Suppose you want to see the properties of the file my_script.sh using the ls command.

You may use the absolute path that starts with the root directory (/):

ls -l /home/abhishek/scripts/my_script.sh

Or, you can use the relative path (which starts from the current directory, not /):

Both commands will yield the same result (except for the path of the file).

Absolute and relative path in Linux

In Linux, file names are case sensitive and can have uppercase, lowercase, numbers, periods, dashes, underscores and most symbols except slash (/). Slash (/) is reserved for root and for separating directories in the path.

Using relative path with . and .. directories

Let me show another examples to explain the difference between absolute path and relative path. But before that, you should know about two special relative paths:

  • . ( single dot) denotes the current directory in the path.
  • .. (two dots) denotes the parent directory, i.e., one level above.

Things will be clear in a moment. Take a look at the scenario. In this one, you want to go to the directory prakash from the directory abhishek .

Absolute vs relative path example in Linux

You can use the cd command to switch directories. The absolute path is quite evident here:

To use the relative path, you’ll have to use the special relative path:

Why use . Because a relative path requires direction from the current directory and you have to tell the cd command to go up a level before going down. The .. brings you to the /home directory and from there you go to the prakash directory.

I hope things are a lot more clear now.

The absolute path always starts with root (/) and will remain the same irrespective of your current location. The relative path depends on your current location.

Which one should you use? Relative path or absolute path?

To be honest, there is no straightforward answer to this question. It really depends on the situation.

If you are deep down in directory hierarchy and you have to move a level up or down, using the relative path will be easier.

Suppose you are in /home/username/programming/project/interface/src/header directory and you have to access something in /home/username/programming/project/interface/bin directory. Using the relative path would save you from typing all those lengthy directory name and you can simply use ../../bin here.

But if you have to access something in the /usr/bin directory from /home/username/programming/project/interface/src/header directory, using something like ../../../../../../usr/bin will not be a wise thing to do. Using the absolute path is the sensible thing to do in this case.

Another case is using the paths in scripts or programs. When you are sure of the location, use the absolute path. If your project has several folders and you are required switching between directories, you may use the relative path here because you do not where the end user will be copying all the project files, in the home directory or in some dev directory.

In other words, you should know both methods and use the one that is going to be easier based on your scenario.

I hope you have a better understanding of path and navigation in Linux now. If you have questions or suggestions, please leave a comment.

Источник

Classic SysAdmin: Absolute Path vs Relative Path in Linux/Unix

To understand this we have to know what is a path in Linux.

What Is A Path?

A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

What Is An Absolute Path?

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.

Some examples of absolute path:

/var/ftp/pub /etc/samba.smb.conf /boot/grub/grub.conf 

If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.

What Is The Relative Path?

Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel

changing directory to /var/log/kernel by using relative path concept.

pwd/var/logcd kernel

Note: If you observe there is no / before kernel which indicates it’s a relative directory to present working directory.

Changing directory to /var/log/kernel using absolute path concept.

cd /var/log/kernel

Note: We can use an absolute path from any location where as if you want to use relative path we should be present in a directory where we are going to specify relative to that present working directory.

Источник

Читайте также:  Linux как удалить графическую оболочку
Оцените статью
Adblock
detector