In Linux, the absolute path refers to the file path that always starts from the root directory "/", and the relative path refers to the file path that starts from the current working directory. The absolute path is relative to the root path "/". As long as the file does not move, its absolute path is constant; while the relative path is relative to the current directory. As the program executes, the current location Directories may change, so relative paths to files are not fixed.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
What is a relative path in Linux
Path is one of the most basic concepts in Linux, which every Linux user must know of.
In Linux, a path refers to how files and directories are referenced; it gives the location of a file or directory in the Linux directory structure and consists of a name followed by a slash. Simply understanding the path of a file refers to the location where the file is stored. For example, /home/cat
represents the location where the cat file is stored. As long as we tell the Linux system the exact location where a file is stored, it can find the file.
As a system user, you use a path when you want to access a file or directory, or when you must specify the location of a file or directory for a command or script.
cat /home/abhishek/scripts/my_script.sh
Remember that if the path starts with a slash "/", the first slash represents the root and the remaining slashes in the path are just separators. Beginners often confuse the root slash and the delimiter slash.
In the above figure, the first path starts with the root path (/), and the second path does not start with /. Both of these ways of writing are correct, the first is an absolute path and the second is a relative path.
What are absolute paths and relative paths?
There are two ways to specify the location where a file is stored, namely using absolute paths and relative paths. path.
We know that all files (directories) in the Linux system are organized into an inverted tree structure starting from the root directory "/", as shown in Figure 1.
Figure 1 Linux system file organization structure diagram
Absolute path
The absolute path must start from the root directory / For example:
/home/abhishek/scripts/my_scripts.sh
Use the absolute path to indicate the location of the bin file. The path should be written as /usr/bin. The test code is as follows:
[root@localhost ~]# bin bash: bin: command not found <-- 没有找到 [root@localhost ~]# /usr/bin bash: /usr/bin: is a directory <-- 是一个文件
You can see So, if you pass only a file name to the Linux system, it cannot find the specified file; but when you pass the absolute path of the bin file to the Linux system, it can find it successfully.
Relative path
Different from the absolute path, the relative path does not start from the root directory /, but starts from the current working directory. When using a relative path to indicate the storage location of a file, the two special directories mentioned earlier are often used, namely the current directory (represented by .) and the parent directory (represented by ..).
For example, when we log in to the Linux system as root, the current working directory defaults to /root. If we need to adjust the current working directory to the root subdirectory Desktop, of course we can use the absolute path. , the sample code is as follows:
[root@localhost ~]# pwd <-- 显示当前所在的工作路径 /root [root@localhost ~]# cd /root/Desktop [root@localhost Desktop]# pwd /root/Desktop
As you can see, by using the absolute path, we successfully changed the current working path. But other than that, it's easier to use relative paths. Because it is currently at /root and Desktop is located in the current directory, so:
[root@localhost ~]# pwd <-- 显示当前所在的工作路径 /root [root@localhost ~]# cd ./Desktop [root@localhost Desktop]# pwd /root/Desktop
In this code, ./Desktop represents the path of the Destop file relative to /root.
To give another example, if you log in to the Linux system as root and convert the current working directory from /root to the /usr directory, there are two ways:
#使用绝对路径 [root@localhost ~]# pwd <-- 显示当前所在的工作路径 /root [root@localhost ~]# cd /usr [root@localhost ~]# pwd /usr #使用相对路径 [root@localhost ~]# pwd <-- 显示当前所在的工作路径 /root [root@localhost ~]# cd ../usr <-- 相对 root,usr 位于其父目录 /,因此这里要用到 .. [root@localhost ~]# pwd /usr
In short, absolutely The path is relative to the root path /. As long as the file does not move, its absolute path is constant; while the relative path is relative to the current directory. As the program executes, the current directory may change. Change, so the relative path of the file is not fixed.
The difference between absolute path and relative path
We all know that the directory structure in Linux is a tree, starting from the root (/) , and then generate branches.
Suppose we are now in the directory abhishek and want to access the file my_scripts.sh, as shown in the figure below: The two paths are represented:
Suppose we use ls CommandTo view the file information of my_script.sh, use the absolute path:
ls -l /home/abhishek/scripts/my_script.sh
然后,相对路径:
ls -l scripts/my_script.sh
那么,以上两种方式,除了文件路径不一样以外,其他的完全一样:
注:在Linux中,文件名区分大小写。可以有大写、小写、数字、点、中划线、下划线以及除了斜杠(/)之外的大多数字符。斜杠(/)作为保留字符,用于根目录和用于分割路径中的目录。
使用带有 . 和 .. 目录的相对路径
有两种特殊的相对路径:
如下图,我们要从 abhishek 目录中,到 prakash 目录中去。
切换目录,使用 cd 命令。如果使用绝对路径,可以这样:
cd /home/prakash
使用相对路径的话 ,需要用到特殊的相对路径 .. :
cd ../prakash
为什么要使用 .. 呢?因为相对路径要从当前目录开始,我们需要告诉 cd 命令向上一级,将我们带到 /home 目录,然后从那里在进入到 prakash 目录。
绝对路径始终以 / 开始,并且与当前位置无关;相对路径取决于当前的位置。
绝对路径和相对路径,应该使用哪个呢?
老实说,这个问题没有直截了当的答案。这要视情况而定。
如果你当前所在的目录层次结构比较深,并且需要向上或者向下一级移动,那么使用相对路径会更简单。
假设你位于
/home/username/programming/project/interface/src/header 目录中,并且需要访问 /home/username/programming/project/interface/bin 目录中的某些内容。使用相对路径可以避免键入所有冗长的目录名,只需在此处使用.././bin即可。
但是,如果你要从 /home/username/programming/project/interface/src/header目录访问 /usr/bin 目录中的某些内容,那么使用 ../../../../../../usr/bin 会比较麻烦,这个时候使用绝对路径更方便。
另一种情况,是在脚本或者程序中使用路径。如果位置是确定的,那么就使用绝对路径;如果项目中有多个文件夹,并且需要在其中之间切换,那么此处需要使用相对路径,因为你不确定最终用户会将程序放在什么地方。
推荐学习:Linux视频教程
The above is the detailed content of What are Linux absolute paths and relative paths?. For more information, please follow other related articles on the PHP Chinese website!