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:

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 :

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

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:

  1. Changing directory with relative path concept :
  2. Changing directory with absolute path concept:

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?

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.

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.

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.

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 (/):

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).

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 .

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.

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.

Источник

Absolute path vs relative path in Linux/Unix

One of this blog follower asked us that what’s the difference between absolute and relative path?

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:

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.

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.

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.

Examples of relative path and absolute path for the same operation.

Example 1: Present location is /abc/xyz, I am want to remove /abc/xyz/read/hello.txt file.

Using relative path:

Using absolute path:

Example 2: My present location is /etc/samba and now I want to change directory to /etc.

Using relative path:

Using absolute path:

Example 3: My present location is /var/ftp/ and I want to change the location to /var/log

Using relative path:

Using absolute path:

Example 4: My present location is /etc/lvm and I want to change my location to /opt/oradba

Using relative path:

Using absolute path:

Example 5: My present location is /usr/local and I want to remove a abc.txt file located in this directory how can I do that?

Using relative path:

Using absolute path:

I hope this helps to understand the difference between Absolute and relative path.

Источник

Getting the Absolute (Full) and Relative Path In Linux

This article explains absolute paths and how they differ from relative paths, getting them, and how symbolic links are handled.

FileSystem Paths

A path is the location of a file in a file system. It’s the directions to the file in the folder it is located.

A path consists of a string of characters. Some represent directory names, and a separator character separates the directory names from the file name and extension.

Consider the below path:

  • Forward slashes (/) to separate directories from their subdirectories
  • Directory Names – the text between the forward slashes
  • The file name – in this case, file.txt

Relative Paths

Relative paths are paths that are defined in relation to your current position in the file system.

You can find your current position using the pwd command:

Relative paths begin without a forward slash, or a . or ...

  • Paths beginning without a / or with a . start in the current directory
  • Paths beginning with .. start in the directory above the current directory (the parent of the current directory)

Example

If you are in the directory /home/user:

…and it contains a file called test.txt, you need only type:

…to view the contents of the file, as you are in the same directory as that file, and can access it using its relative path.

In the above example, the cd command is used to change the directory, and the cat command reads the contents of the file to the screen.

Absolute Paths

Absolute paths can be used from anywhere on the filesystem. They represent the full path of the file from the root (the very top) of the file system hierarchy. They are absolute because it’s the absolute location of the file on the file system. It is not relative to where you are or where you might be; the path will work when called from any location.

Example

Consider again that we are in the directory /home/user:

…and there is a file called another.txt located at /var/temp. Attempting to open it by running:

…will fail because that file doesn’t exist at /home/user. We can, however, access it at its absolute path:

As it provides the full location of the file and does not rely on the relative path we are currently situated.

Soft links (symlinks) are just files that point to another file. The absolute path is still the path to the symlink. If you want the path to the linked file itself, you will need to use the readlink command to find the absolute path of the linked file:

Hard links are also absolute paths – the data exists at multiple absolute paths but in only one location on the physical disk.

Brad Morton

I’m Brad, and I’m nearing 20 years of experience with Linux. I’ve worked in just about every IT role there is before taking the leap into software development. Currently, I’m building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I’m up to.

Источник

Читайте также:  Divx player mac os
Оцените статью