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

Example code for making the pre tag automatically wrap

The pre element defines preformatted text. Text en...

Detailed process of changing apt source to Alibaba Cloud source in Ubuntu 18.04

Table of contents Preface: Ubuntu 18.04 changes a...

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing can be ...

How to make your browser talk with JavaScript

Table of contents 1. The simplest example 2. Cust...

How to enable remote access in Docker

Docker daemon socket The Docker daemon can listen...

Should nullable fields in MySQL be set to NULL or NOT NULL?

People who often use MySQL may encounter the foll...

Let's talk about the LIMIT statement in MySQL in detail

Table of contents question Server layer and stora...

Implementation of Docker cross-host network (manual)

1. Introduction to Macvlan Before the emergence o...

Solve the installation problem of mysql8.0.19 winx64 version

MySQL is an open source, small relational databas...

Design Reference Beautiful and Original Blog Design

All blogs listed below are original and uniquely ...

Chinese website user experience rankings

<br />User experience is increasingly valued...