Raspberry Pi_Eng_16.1.3 Specifying Paths for Directory and File


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


16.1.3  Specifying Paths for Directory and File

 

There are two ways to specify the path to a directory or file in Linux, depending on the start position described when specifying the path. One is an absolute path method starting from "/" (root directory), and another one is a relative path method based on the current working position.

 

16.1.3.1    Absolute Path

 

Absolute path method is the one which specifies a complete path starting from the top of the file system, the "/" (root directory). This method provides complete information about the location of the directory or file by describing all the paths that reach from the topmost position to the specified position.

 

For example, if there is a file named "DebianManual.txt" in the "testdata" directory in the home directory of the user account "pi", the absolute path to this file should be in the format "/home/pi/testdata/DebianManual.txt".

 

pi@raspberrypi ~ $ cd testdata

pi@raspberrypi ~/testdata $ ls -l

total 20

-rw-rw---- 2 pi pi   80 Apr 12 15:12 DebianManual.txt

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

drwxr-xr-x 2 pi pi 4096 Apr 10 13:32 TestFolder02

-rw-rw---- 1 pi pi   81 Apr 11 09:03 user_guide01.txt

 

If you want to know the absolute path to the current location on your system, you can use the "pwd" command.

 

pi@raspberrypi ~/testdata $ pwd

/home/pi/testdata

 


 

16.1.3.2    Relative Path

 

The location that is applied by default when a particular location is not specified is called "current working location", and the method of describing the location of a specific directory or file is called the relative path method.

 

When specifying the location of the target directory or file in the system, the following base position is used as the starting point:

    ./    -- Indicates "current working position".

    ../   -- Points to the parent directory one level above the "current working location".

 

In the relative path specification method, since the position of the target file is completely different according to the current working position, it is impossible to determine a specific target by only the path itself. Therefore, it is always necessary to accurately check "current working position".

 

pi@raspberrypi ~ $ ls l testdata

total 20

-rw-rw---- 2 pi pi   80 Apr 12 15:12 DebianManual.txt

drwxr-xr-x 3 pi pi 4096 Apr 11 02:17 TestFolder01

drwxr-xr-x 2 pi pi 4096 Apr 10 13:32 TestFolder02

-rw-rw---- 1 pi pi   81 Apr 11 09:03 user_guide01.txt

pi@raspberrypi ~ $ ls l Downloads

total 1808

-rw-r--r-- 1 pi pi   17842 Apr  7 02:17 commands.md

-rw-rw---- 2 pi pi      80 Apr 12 15:12 DebianManual.txt

-rw-r--r-- 1 pi pi 1296587 Apr 11 09:19 debian-reference.en.txt

-rw-r--r-- 1 pi pi  275957 Apr  1 04:06 debian-reference.en.txt.gz

-rw-r--r-- 1 pi pi  245423 Mar 31 03:58 RaspiCam-Documentation.pdf

 

For example, if there is a file named "DebianManual.txt" in the "testdata" directory in the home directory of the user account "pi" and the current working location is "/home/pi", then the relative path to this file is specified in the format "./testdata/DebianManual.txt".

 

In the above example, there is a file named "DebianManual.txt" in both the "testdata" directory and the "Downloads" directory. If you specify "./DebianManual.txt" as the relative path, you can see that the target file can be completely different depending on whether or not the current working location is "/home/pi/testdata" or "/home/pi/Downloads"

 

Below is an example of executing the command to look up information about the "Downloads" directory above the current location by specifying the path starting from the parent directory "../" when "/home/pi/testdata" is the current location.

 

pi@raspberrypi ~ $ cd testdata

pi@raspberrypi ~/testdata $ ls ../Downloads -l

total 1808

-rw-r--r-- 1 pi pi   17842 Apr  7 02:17 commands.md

-rw-rw---- 2 pi pi      80 Apr 12 15:12 DebianManual.txt

-rw-r--r-- 1 pi pi 1296587 Apr 11 09:19 debian-reference.en.txt

-rw-r--r-- 1 pi pi  275957 Apr  1 04:06 debian-reference.en.txt.gz

-rw-r--r-- 1 pi pi  245423 Mar 31 03:58 RaspiCam-Documentation.pdf