What is relative path in linux

Absolute and Relative Paths in Linux | How to Reference Them?

An absolute path is a complete path that begins from the root directory. In contrast, the relative path is a path that refers to the existing directory. Absolute paths are utilized when the exact location of a file or directory is needed or if the directory/file resides nearby locations of the root folder. The relative paths are often utilized to refer to a file or directory in the working directory or a nearby subdirectory.

This article presents absolute and relative paths and how to reference them in Linux. The supported content of this article is mentioned below:

What is Absolute Path in Linux?

The absolute path refers to the root directory of the system. The absolute path comprises all the directories/subdirectories (from the root directory) needed to reach the desired file or directory.

How to Reference an Absolute Path?

To reference an absolute path in Linux, you must provide the full path starting from the root directory, represented by a forward slash ‘/’.

Example 1: Change the Current Directory Using Absolute Path

For instance, we need to change the directory to “Downloads”, and our terminal is currently in the home directory. For this, the “cd” command is used with the complete path from the root directory:

$ cd /home/itslinuxfoss/Downloads

The output shows that the current working directory has been changed to the “Downloads” directory.

Example 2: Move a File Using its Absolute Path

To move a file using the absolute path, specify the file name with the complete directory path. For instance, specify the “file.txt” file that is located in the “/home/itslinuxfoss/” directory and move to the “/office” directory:

$ sudo mv /home/itslinuxfoss/file.txt /office

This command uses two absolute paths, one for the source file and another for the destination.

What is Relative Path in Linux?

This path contains the directories and subdirectories needed to reach the desired directory or file from the current working directory.

How to Reference Relative Paths?

To reference a relative path in Linux, you do not need to provide the full path starting required to reach the destination from the current directory.

Example 1: Change the Current Directory

Let’s navigate to the “Downloads” directory from the current directory (which is the home directory) using the relative path:

Читайте также:  How remove linux mint

This is a relative path, as it refers to the parent directory of the existing/current directory.

Example 2: Move a File With the Help of the Relative Path

To move a file using the relative path in the Linux terminal, execute the “mv” command by specifying the source and destination of the file:

It moves the “file.txt” file from the current directory to the “Folder” subdirectory.

Conclusion

In Linux, the absolute path provides the exact location of a file or directory, while the relative path refers to directories and files in the current directory. To reference an absolute path, you need to provide the full path starting from the root directory, while the relative path refers to the leading path from the current directory. This article has explained the absolute and relative paths and the reference in Linux.

Источник

Absolute Path vs Relative Path in Linux

When handling files and directories on Linux, you must know how to work with paths for quick navigation and access to files. A path defines the route to access a given folder or file. Linux’s directory structure resembles the roots of a tree, where everything starts from the root down to the branches until you reach the target file or folder.

Today’s guide focuses on understanding paths on Linux. We will dig in on absolute and relative paths in Linux to understand their differences and how to use each when locating the path to a file or folder.

Understanding Absolute and Relative Path in Linux

The Linux file system is all about files and directories— how you access these files or folders is using paths. Every path starts from the root directory, represented as a slash (/). From the root directory, you can have the separators. You can use the relative or absolute path.

1. Absolute Path

You use the absolute path when you specify the path to a file or folder, starting with the root directory. This path begins with the root (/), followed by the separators to the other directories in the middle before you reach the target file.

To understand the absolute path in Linux, let us have an example of using the pwd command.

You will note that our current path, in this case, is /home/linuxhint. The first slash (/) represents the root directory. All users created in a Linux system get stored in the home directory. In this case, our user is named linuxhint. So, this absolute path is the home directory of linuxhint and it contains all the other child directories.

Suppose we wanted to list the contents of the Downloads directory for the named user, you can do so using its absolute path, as shown.

The absolute path in Linux allows access to files and folders in other locations without navigating into their parent directory.

Читайте также:  Просмотр сетевого окружения linux

2. Relative Path

Unlike the absolute path that starts from the root directory, the relative paths start from the present working directory. Thus, the relative path changes depending on your current directory.

For instance, referencing the earlier example of accessing the contents of the /Downloads, our command would change as follows, if we wanted to use the absolute path:

The target folder is in the current directory, meaning we can directly reference it without needing to add the root directory.

The relative path allows using single dot (.) and double dot (..) to specify paths up or down the current directory. These dots are not visible unless you use them to access the target file.

The single dot specifies the current directory, while the double dot represents its parent directory.

We are in the /home/linuxhint/Downloads/names path.

Suppose we wanted to list the contents of the Downloads/ without typing its absolute path, we could use the double dots for the relative path to access the parent directory with the command below.

Using the relative path in such a case is more convenient, unlike the absolute path, which would require typing the whole path as shown.

Similarly, if we wanted to move higher in the parent directory, we could add more double dots with a separator. In the below example, we have accessed the home directory.

If we wanted to access a directory in the child directory of our current directory, we could specify its relative path using single quotes.

Absolute Path vs Relative Path

The absolute path specifies the path to a folder or file starting from the root directory followed by the user’s home directory. However, the relative path specifies the path to a file or folder starting with the current directory.

Use the relative path when accessing a child or parent directory starting from the current directory. Use the absolute path to access a file or folder down the file system or from another location without switching the current directory.

Conclusion

This guide has discussed what the absolute and relative paths mean in Linux. We have seen the examples in each case and how to choose either option depending on the location of your target file or folder. With this post, you will quickly understand how to work with paths in Linux.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

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.

Источник

Оцените статью
Adblock
detector