- Absolute and Relative Paths in Linux & How to Reference Them
- Absolute Path
- Relative Path
- Conclusion
- About the author
- Aqsa Yasin
- Absolute path vs relative path in Linux/Unix
- What is a path?
- What is an absolute path?
- Some examples of absolute path:
- What is the relative path?
- Examples of relative path and absolute path for the same operation.
- Absolute and Relative Pathnames in UNIX
- Relative path and absolute path
- file path
- Relative path and absolute path
- Example
- Display user current work directory
- LS: List the directory
- Simple information from files and subdirectorys in / root directory
- Details of the file and subdirectories in / root directory
- Detailed information on all files and subdirectories in / root directory, including hidden files
- CD: Switch Directory
- Switch to / root directory
Absolute and Relative Paths in Linux & How to Reference Them
Absolute Path
An absolute address refers to a document or folder’s address independent of the current working directory; in actuality, it is related to a root folder. Its name comes from the fact that it includes the full address of a document or folder. Also, it’s known as a complete pathname and an absolute pathname since it always begins at the very same location, which would be the root folder. Absolute routes include most of the details needed to locate the assets referenced via an absolute URL. When referring to websites hosted on a domain else than your own, the absolute path should be cast off. To write the absolute path, you have to use a slash “/” to start it, representing the root directory.
Relative Path
A relative path specifies where a document or directory is located concerning the current working directory. It is indeed best cast-off to link to sites on the very same domain, particularly on certain portions of websites where the documents’ connections to one another never alter. Despite absolute routes, relative routes only carry content useful to the present content on the very same site, obviating any need for a complete absolute path. In basic terms, a relative path is a route that is relative to the current web page’s position.
Example 01
Let’s have some clear examples to elaborate the concept of Absolute and relative path. Hence, log in from the Ubuntu system first and then open the terminal. The terminal application can be opened using “Ctrl+Alt+T” or indirectly from the applications. When you open the terminal, you must be located at the root directory of your Ubuntu 20.04 system. Hence, to check your current location while working, we will be using the “pwd” command in the shell as below. You can see, we are currently at the location of the root directory, which is specified by the user name “aqsayasin” followed by the “home” directory.
Let suppose you have a file “test.txt” currently residing in your home or root directory where you are currently residing. This means, when you want to check the contents of the file “test.txt” from the current location, it should show its contents. Hence, we have to check if it will be opened using a cat query in the shell or not. So, we tried the command below and got the contents of a file successfully.
Suppose you move the file to the Documents folder and check if the cat query will show its contents. You will get the below error saying, “No such file or directory”. This is because we haven’t used the absolute path.
But you can also show the file contents using slash within the path to the folder where the file resides, e.g., Documents. Hence, the below form of the path will work perfectly with a cat. You have to understand that the file’s location is resolute about root because of the first slash sign “/”. Every slash sign indicates that we are dropping one level throughout the file system to every such /, as below, where “aqsayasin” is one level beneath “home” and hence two levels beneath “root”.
Example 02
The relative pathname is a Linux shorthand that takes the current or parental folder as a base and provides the route. A few of these enigmatic characters are used in a relative pathname:
- Single Dot: The current folder is represented by a single dot.
- Double Dot: The parental folder is represented by two dots.
That implies that we’re presently in the folder /home/aqsayasin; we can even use the “..” option within the “cd” query to go to the parental location /home. So let’s do this by checking the current directory using “pwd”, and you can have a look that we are currently at /home/aqsayasin.
Let’s use the double dots “..” in the “cd” command to move towards the parent directory as:
This will move to the /home directory as below. You can also confirm it using the “pwd” command.
Example 03
Let’s have the same concept in another example. First, move to the Documents folder using the “cd” query in your terminal shell below.
Now check your current location with “pwd”, and you can see we are at Documents folder two-level below from the home directory and three-level below from the root (as three slash signs are used). Now you can open any file that has been residing in this folder.
Let suppose you want to go two levels up in the path. For this purpose, we have to use double dots two times in the “cd” query with the slash sign between them. The first double dots represents the parent of the “Documents” folder, which is “aqsayasin”. And, double dots after the slash sign represents the parent of folder “aqsayasin”, which is “home”. Hence, we should be moved to a home directory using this query.
The blue highlighted part of the image shows the “home” directory, which is our current location. On the other hand, you can check it via “pwd” as below.
Example 04
Let’s have another example. Let suppose you are at the root directory of your system right now. You can confirm it via “pwd” instruction as well.
Let’s move to another folder. Let’s move to the “Pictures” folder using the same “cd” instruction in the shell. Now you are in the Pictures folder. You can also check it using “pwd” again. The blue highlighted part also shows that you are in the Pictures folder.
From the above image, you can see that the parent of a “Pictures” folder is “aqsayasin”. Here is the twist in this example. Let suppose you want to move towards the Document folder directly from the Pictures folder. For this reason, we should use double dots in our cd command along with a slash sign to get to the parent of a folder “Pictures”, which is “aqsayasin”. On the other hand, we have mentioned a folder name, e.g., “Documents”, after the slash sign, as we want to go towards it from the Pictures folder. Remember that you cannot directly move towards the Document folder until you get to its parent folder, “aqsayasin”. Now you are at the Documents folder, as shown from the “pwd” command output.
Conclusion
We have done with both the paths, e.g., absolute and relative path, in this guide. We have covered enough examples to elaborate and understand the concepts of referencing them with each other.
About the author
Aqsa Yasin
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
Источник
Absolute path vs relative path in Linux/Unix
One of this blog follower asked us that what’s the difference between absolute and relative path?
To understand this we have to know what is a path in Linux.
What is a path?
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.
What is an absolute path?
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
Some examples of absolute path:
If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.
What is the relative path?
Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel
changing directory to /var/log/kernel by using relative path concept.
Note: If you observe there is no / before kernel which indicates it’s a relative directory to present working directory.
Changing directory to /var/log/kernel using absolute path concept.
Note: We can use an absolute path from any location where as if you want to use relative path we should be present in a directory where we are going to specify relative to that present working directory.
Examples of relative path and absolute path for the same operation.
Example 1: Present location is /abc/xyz, I am want to remove /abc/xyz/read/hello.txt file.
Using relative path:
Using absolute path:
Example 2: My present location is /etc/samba and now I want to change directory to /etc.
Using relative path:
Using absolute path:
Example 3: My present location is /var/ftp/ and I want to change the location to /var/log
Using relative path:
Using absolute path:
Example 4: My present location is /etc/lvm and I want to change my location to /opt/oradba
Using relative path:
Using absolute path:
Example 5: My present location is /usr/local and I want to remove a abc.txt file located in this directory how can I do that?
Using relative path:
Using absolute path:
I hope this helps to understand the difference between Absolute and relative path.
Источник
Absolute and Relative Pathnames in UNIX
A path is a unique location to a file or a folder in a file system of an OS.A path to a file is a combination of / and alpha-numeric characters.
Absolute Path-name
An absolute path is defined as the specifying the location of a file or directory from the root directory(/).
To write an absolute path-name:
- Start at the root directory ( / ) and work down.
- Write a slash ( / ) after every directory name (last one is optional)
For Example :
will work only if the fie “abc.sql” exists in your current directory. However, if this file is not present in your working directory and is present somewhere else say in /home/kt , then this command will work only if you will use it like shown below:
In the above example, if the first character of a pathname is /, the file’s location must be determined with respect to root. When you have more than one / in a pathname, for each such /, you have to descend one level in the file system like in the above kt is one level below home, and thus two levels below root.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory.
Relative path
Relative path is defined as the path related to the present working directly(pwd). It starts at your current directory and never starts with a / .
To be more specific let’s take a look on the below figure in which if we are looking for photos then absolute path for it will be provided as /home/jono/photos but assuming that we are already present in jono directory then the relative path for the same can be written as simple photos.
Using . and .. in Relative Path-names
UNIX offers a shortcut in the relative pathname– that uses either the current or parent directory as reference and specifies the path relative to it. A relative path-name uses one of these cryptic symbols:
Now, what this actually means is that if we are currently in directory /home/kt/abc and now you can use .. as an argument to cd to move to the parent directory /home/kt as :
NOTE:Now / when used with .. has a different meaning ;instead of moving down a level,it moves one level up:
Example of Absolute and Relative Path
Suppose you are currently located in home/kt and you want to change your directory to home/kt/abc. Let’s see both the absolute and relative path concepts to do this:
- Changing directory with relative path concept :
- Changing directory with absolute path concept:
This article is contributed by Dimpy Varshni. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Источник
Relative path and absolute path
file path
File path: The path to the file is the location of the description file in the system
Linux uses a positive slant line ( / ) Rather than anti-laminated lines ( \ ) To divide the directory in the file path
Relative path and absolute path
Absolute path:
The writing of the absolute path must be written by the root directory ‘/’
relative path:
The write method of relative path is not written by the root directory ‘/’
Single symbol (.): Represents the current directory
Double point (. ): means the parent directory of the current directory
- / Representative root directory
- .. Represents the previous directory of the current directory
- . Representing the current directory
On behalf of the user’s home directory, this symbol is usually located below the ESC key
## PWD: Display Work Directory Path
Using the PWD command can display the absolute path of the working directory where the current user is located. PWD is the abbreviation of Print Working Directory, that is, the command to display the current directory.
syntax:
Options and parameters:
- -L : When the directory connection, the output link path
- -P : Output physical path
Example
Display user current work directory
LS: List the directory
The LS command is the abbreviation of List
syntax ls [OPTION]. [FILE]
- -a or —all Display all files and directories (LS is within the beginning of the file name or directory name «.» Will not be listed.
- -A or —almost—all Display all files and directories, but do not display the current directory ( . ) And parent directory ( .. )
- -l Use a list of detailed format: In addition to the file name, the file type, permission, owner, file size, modification time, etc. are also listed in detail.
- -r Display the file in the opposite order (original dependent English alphabetical order)
- -S Sort by file and directory size
- -t List the files in the first order of the establishment time
- -R or —recursive Recursive processing, list all files and subdirectories and subdirectories in the specified directory.
### file type color
Different types of files have different colors in the Linux system, and the color is shown in the following table by default.
colour | file type |
---|---|
white | Ordinary document |
Navy blue | table of Contents |
Grass green | executable file |
red | Compressed file |
light blue | Link file |
Red flashing | Problem link file |
yellow | Device file, including block device file or character device file |
gray | Some other files |
ls -l Commands Almost all information on any file or directory on the system
Test.txt as an example
Output information | Information description |
---|---|
— | Document type, test.txt is a normal file |
rw-r—r— | file permission |
1 | File connection |
root | Document user owner |
root | File user base owner |
0 | The length of the file (that is, the file size, not the disk’s disk occupancy) |
0 November 22 14:55 | Document change time (mtime), or final access time of files (ATIME) |
test.txt | file name |
The file type is shown in the following table
symbol | Types of |
---|---|
— | Ordinary document |
d | Catalog file |
l | Symbolic link file |
p | Pipeline file |
s | Socket file |
b | Block device file |
c | Character device file |
Simple information from files and subdirectorys in / root directory
Details of the file and subdirectories in / root directory
Detailed information on all files and subdirectories in / root directory, including hidden files
#### Display the file type in / etc / directory
mark | file type |
---|---|
Not marked | Ordinary document |
/ | table of Contents |
* | executable file |
@ | Symbolic link file |
ι | Pipeline file |
= | Socket file |
#### Sort by file and directory under the / root in the last access time (recently accessed)
#### Sort by file and subdirectories in / root directory in the last access time (recently access time)
#### Sort by file and subdirectories in / root directory in the size of the file
#### View files inode
#### View file disk occupancy
CD: Switch Directory
The CD is the abbreviation of Change Directory, which is a basic tool that moves between the different directories of the Linux file system, which is the command used to transform the work directory.
syntax:
CD [relative path or absolute path]
CD command follows a path name as a parameter, indicating that entering another directory
Switch to / root directory
### Switch to the previous directory
### Switch to Home Catalog
For root users, home directory is / root directory
Источник