Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of Linux relative and absolute paths

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

01. Overview

Absolute path and relative path are often encountered in shell environment and have their own usefulness. Sometimes the relative path is more convenient, sometimes the absolute path is more convenient.

02. Absolute path (Absolute Pathname)

The 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

Give examples to illustrate

Example 1: / home/deng example 2: / usr/share/man

03. Relative path (Relative Pathnames)

The relative path does not begin with * / *

The relative path is relative to the current location, pointing to the destination

Usually the relative path is a little 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. The use of relative paths

So what's the big deal about relative paths and absolute paths? Drink! That's really something! Suppose you write a software that requires a total of three directories, namely, etc, bin and man. However, since different people like to install them in different directories, suppose the directories for installation An are / usr/local/packages/etc, / usr/local/packages/bin and / usr/local/packages/man. But B likes to install it in / home/packages/etc, / home/packages/bin, / home/packages/man. Is it troublesome if you need to use an absolute path? Right! In this way, it is difficult to correspond to the things in each directory! At this time, the writing of the relative path is very important!

In addition, if you, like Brother Bird, like to write the name of the path very long, so that you know what that directory is doing, for example: / cluster/raid/output/taiwan2006/smoke this directory, and the other directory is / cluster/raid/output/taiwan2006/cctm, then how can I write it from the first to the second directory? Cd, of course. / cctm is more convenient! Right?!

05. The purpose of the absolute path

But for the correctness of the document name, "the accuracy of the absolute path is better." Generally speaking, Bird will advise you to use absolute path if you are writing a program (shell scripts) to manage the system. It's hard to say? Because although it is troublesome to write the absolute path, it is certain that there will be no problem with it. If you use relative paths in the program, some problems may occur due to the different working environment in which you are running.

06. Thinking divergence

Question: when giving a relative path, how does the system identify the corresponding absolute path?

In fact, relative paths find absolute paths based on the PATH environment variable.

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

[itcast@localhost ~] $more .bashrc

Because the value of the 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 the more command is executed, shell looks in the order in the path of the PATH variable, and executes / bin/more .bashrc as soon as the search matches (in this case it should be / bin/more). This is the process of finding an absolute path by a relative path when a command is executed.

So we can understand why executable programs in the current directory should be executed in the following format:

[deng@localhost] $. / test.sh

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

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report