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

Summary of common MySQL table design errors

Table of contents Mistake 1: Too many columns of ...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

Analysis of the use and principle of Docker Swarm cluster management

Swarm Cluster Management Introduction Docker Swar...

Analysis of the principles and usage of Linux hard links and soft links

In the Linux system, there is a kind of file call...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

Front-end state management (Part 1)

Table of contents 1. What is front-end state mana...

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

How to use vue3+TypeScript+vue-router

Table of contents Easy to use Create a project vu...

...

Tomcat uses Log4j to output catalina.out log

Tomcat's default log uses java.util.logging, ...

JavaScript implements mouse drag to adjust div size

This article shares the specific code of JavaScri...