Linux Relative Path and Absolute Path Use

  • 2021-07-26 09:08:04
  • OfStack

01. Overview

Absolute path and relative path are often encountered in shell environment, and each has its own use. Sometimes the relative path is more convenient, and sometimes the absolute path is more convenient.

02. Absolute path (Absolute Pathname)

Absolute path must begin with **/**

The absolute path points to the location of the file/file

At any time, we can use the absolute path to find the file we want

Illustrate with examples


 Example 1 : 
/home/deng

 Example 2 : 
/usr/share/man

03. Relative Path (Relative Pathnames)

Relative path does not start with **/**

The relative path is relative to the current position and points to the destination

Usually, the relative path is 1 point shorter than the absolute path, which can be used as a shortcut for us to find files/files quickly


 Example 1 : 
../test/itcast

 Example 2 : 
./test

04. Use of relative paths

So what's so great about relative path and absolute path? Drink! That's really amazing! Suppose you write a software, which needs three directories, namely etc, bin and man. However, because different people like to install under different directories, suppose that the directories installed by A are/usr/local/packages/etc/packages/packages/bin and/usr/local/man, but B likes to install them in/home/etc,/home/packages/bin,/home/packages/man Is it troublesome? Yes! In this way, it is difficult to correspond to the things in each directory! At this time, the writing of relative path is particularly important!

In addition, if you like to write the name of the path very long, so that you can know what the directory is doing, for example:/cluster/raid/output/taiwan2006/smoke, and the other directory is at/cluster/raid/output/taiwan2006/cctm, then I want to go from the first to the second directory, how to write more conveniently? Of course it's cd …/cctm is more convenient! Right?

05. Use of Absolute Path

But for the correctness of the document name, "the correctness of the absolute path is better ~". Generally speaking, Bird Brother would suggest that if you are writing a program (shell scripts) to manage the system, you must use the absolute path. How should I put it? Because the writing of absolute path is troublesome, but it is certain that there will be no problem in this writing. If you use relative paths in your program, you may be running in a different working environment, which may lead to some problems.

06. Divergent thinking

Question: When the relative path is given, how does the system identify its corresponding absolute path?

In fact, the relative path is based on the PATH environment variable to find the absolute path.

For example, I'm currently in the/home/itcast itcast user's home directory, and I want to look at the data in the.bashrc file, using the more directive, which is in the/bin/more. The usual command is written as follows:


[itcast@localhost ~]$ more .bashrc

And because the value of PATH variable is:


[deng@localhost ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/deng/.local/bin:/home/deng/bin
[deng@localhost ~]$ 

When executing the more command, shell looks up in order in the path of the PATH variable, and executs/bin/more. bashrc once the lookup matches (this example should match the/bin/more). This is the process of finding the absolute path from the relative path when executing the command.

Therefore, we can understand why the executable program in the current directory should be executed in the format:


[deng@localhost ~]$ ./test.sh

Instead of using test. sh directly. Because usually the PATH variable does not contain the current path. If you don't add./, the command in relative path format can't find the absolute path.


Related articles: