Use of Linux relative and absolute paths

Use of Linux relative and absolute paths

01. Overview

Absolute paths and relative paths are often encountered in shell environments, and each has its own uses. Sometimes relative paths are more convenient, and sometimes absolute paths are more convenient.

02. Absolute Pathname

An absolute path must start 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

Example

Example 1:
/home/deng

Example 2:
/usr/share/man

03. Relative Pathnames

Relative paths do not start with **/**

The relative path is relative to the current location and points to the destination.

Usually a relative path is shorter than an absolute path and can be used as a shortcut to quickly find files/files.

Example 1:
../test/itcast

Example 2:
./test

04. Purpose of relative paths

So what's the big deal about relative paths and absolute paths? drink! That's really amazing! Suppose you have written a software that requires three directories, namely etc, bin, and man. However, different people like to install the software in different directories. Suppose Person A installs the software in /usr/local/packages/etc, /usr/local/packages/bin, and /usr/local/packages/man, but Person B likes to install the software in /home/packages/etc, /home/packages/bin, and /home/packages/man. If absolute paths are required, will it be troublesome? Yes! In this way, it is difficult to match the contents in each directory! At this time, the writing of relative paths is particularly important!

In addition, if you are like me, and like to write long path names so that you know what the directory is for, for example: the directory /cluster/raid/output/taiwan2006/smoke, and another directory is /cluster/raid/output/taiwan2006/cctm, then if I want to go from the first to the second directory, how should I write it more conveniently? Of course cd.../cctm is more convenient! Right!

05. Purpose of absolute path

However, regarding the correctness of the file name, "the accuracy of the absolute path is better~". Generally speaking, I would recommend that you use absolute paths when writing shell scripts to manage your system. How to say it? Although it is more troublesome to write an absolute path, it is certain that there will be no problem with this writing method. If you use relative paths in your program, some problems may occur due to the different working environments you are running in.

06. Divergent thinking

Question: When a 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 am currently in /home/itcast, the home directory of the itcast user. I want to view the data of the .bashrc file in it, using the more command, which is in /bin/more. The usual command writing is:

[itcast@localhost ~]$ more .bashrc

And since 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 executing the more command, the shell will search sequentially according to the path of the PATH variable. Once a match is found (in this case, the match should be /bin/more), /bin/more .bashrc will be executed. This is the process of searching for the absolute path from the relative path when executing a command.

So we can understand why the format for executing the executable program in the current directory should be:

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

Instead of using test.sh directly. Because usually the PATH variable does not contain the current path. If ./ is not added, the command in relative path format will not be able to find the absolute path.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to get the absolute path of a file through a file descriptor in Linux
  • Linux C++ Example code for getting the absolute path of a file
  • How to view the path of the MySQL configuration file on the Linux server
  • linux c get the process name and execution path of the current process (example)
  • How to query the full path of the running file in Linux
  • Problems with dynamic link library loading path and search path under Linux
  • Linux quick location web path method
  • How to get the real path of the current script in Linux
  • An example of getting the current working path under Linux

<<:  An experienced person will show you how to develop a professional and standardized MySQL startup script

>>:  How to use anti-shake and throttling in Vue

Recommend

Canvas draws scratch card effect

This article shares the specific code for drawing...

Vue implements dynamic circular percentage progress bar

Recently, when developing a small program, I enco...

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

JavaScript canvas realizes the effect of nine-square grid cutting

This article shares the specific code of canvas t...

Cross-origin image resource permissions (CORS enabled image)

The HTML specification document introduces the cr...

How to disable web page styles using Firefox's web developer

Prerequisite: The web developer plugin has been in...

Analysis of the principle of using PDO to prevent SQL injection

Preface This article uses pdo's preprocessing...

Implementing Binary Search Tree in JavaScript

The search binary tree implementation in JavaScri...

How to use filters to implement monitoring in Zabbix

Recently, when I was working on monitoring equipm...

Solution to blank page after Vue packaging

1. Solution to the problem that the page is blank...

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

Five solutions to cross-browser problems (summary)

Brief review: Browser compatibility issues are of...

Detailed analysis of the usage and application scenarios of slots in Vue

What are slots? We know that in Vue, nothing can ...

Deployment and configuration of Apache service under Linux

Table of contents 1 The role of Apache 2 Apache I...