Newby Coder header banner

Relative Path

What is Relative Path in Programming

The relative path of a file or directory is the path or location from a specific directory

For example, if a file wallpaper.jpg is downloaded to Downloads folder

Then its full path in Windows is something like

C:\Users\John\Downloads\wallpaper.jpg

and in Linux its something like

/home/john/Downloads/wallpaper.jpg

And, its relative path from the Downloads directory is the name of the file itself

wallpaper.jpg

Similarly, its relative path from home directory /home/izaku is

Downloads/wallpaper.jpg 

Such relative paths can be used as alternative of full path

cd /home/john/
mv Downloads/wallpaper.jpg ~/Pictures/

Here, it moves wallpaper.jpg using its relative path Downloads/wallpaper.jpg

Usage of relative paths in a program may allow the program to be installed or run in any location

What is an Relative Path of URL

A relative URL is relative to the domain of an Url

Domain of an Url https://youtube.com/videos/235235 is https://youtube.com

and the relative path of the url is /videos/235235

A relative url can only be used if the file or url exists on the same server as the HTML file(or current url)

So the domain name for both the Urls should be the same

In contrast, absolute URL specifies the domain address in an Url

Absolute URLs must be used to link to other websites that are not located on the same domain such as an url of an Youtube page from a Facebook page

Consider the absolute URL

[ncw-wd]/sample/file/a_picture.jpg"

It's relative path is the path after its domain name

/sample/file/a_picture.jpg

In such cases, a web browser adds the relative path to the current url to get its absolute path and forward the user

But if such a link is referred by a page whose url contains a path after the domain name

Then the relative path has to navigate to the parent directory (using .. two dots)

For example the relative url of an image [[ncw-wd]]/icon.jpg from url [[ncw-wd]]/sample/redirect/sample_redirect

is
../../icon.jpg