Classic SysAdmin: Absolute Path vs Relative Path in Linux/Unix
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:
/var/ftp/pub /etc/samba.smb.conf /boot/grub/grub.conf
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.
pwd/var/logcd kernel
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.
cd /var/log/kernel
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.
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:
.(a single dot) - this represents the current directory. ..(two dots) - this represents the parent directory.
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 :
$pwd /home/kt/abc $cd .. ***moves one level up*** $pwd /home/kt
NOTE:Now / when used with .. has a different meaning ;instead of moving down a level,it moves one level up:
$pwd /home/kt/abc ***moves two level up*** $cd ../.. $pwd /home
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 :
$pwd /home/kt $cd abc $pwd /home/kt/abc
$pwd /home/kt $cd /home/kt/abc $pwd /home/kt/abc
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.
What is file path in linux
PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (i.e., ready-to-run programs) in response to commands issued by a user. It increases both the convenience and the safety of such operating systems and is widely considered to be the single most important environmental variable.
Environmental variables are a class of variables (i.e., items whose values can be changed) that tell the shell how to behave as the user works at the command line (i.e., in a text-only mode) or with shell scripts (i.e., short programs written in a shell programming language). A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems; its primary function is to read commands that are typed in at the command line and then execute (i.e., run) them.
PATH (which is written with all upper case letters) should not be confused with the term path (lower case letters). The latter is a file’s or directory’s address on a filesystem (i.e., the hierarchy of directories and files that is used to organize information stored on a computer). A relative path is an address relative to the current directory (i.e., the directory in which a user is currently working). An absolute path (also called a full path) is an address relative to the root directory (i.e., the directory at the very top of the filesystem and which contains all other directories and files).
A user’s PATH consists of a series of colon-separated absolute paths that are stored in plain text files. Whenever a user types in a command at the command line that is not built into the shell or that does not include its absolute path and then presses the Enter key, the shell searches through those directories, which constitute the user’s search path, until it finds an executable file with that name.
The concentrating by default of most executable files in just a few directories rather than spread all over the filesystem and the use of the PATH variable to find them eliminates the need for users to remember which directories they are in and to type their absolute path names. That is, any such program can be run by merely typing its name, such as ls instead of /bin/ls and head instead of /usr/bin/head, regardless of where the user is currently working on the filesystem. This also greatly reduces the possibility of damage to data or even to the system as a whole from the accidental running of a script that has the same name as a standard command. 1
A list of all the current environmental variables and their values for the current user, including all the directories in the PATH variable, can be seen by running the env command without any options or arguments (i.e., input data), i.e.,
As there can be considerable output, it can be convenient to modify this command so that it displays just the PATH environmental variable and its value. This can be accomplished by using a pipe (represented by the vertical bar character) to transfer the output of env to the grep filter and use PATH as an argument to grep, i.e.,
Another way to view the contents of just PATH alone is by using the echo command with $PATH as an argument:
echo repeats on the display screen whatever follows it on the command line. The dollar sign immediately preceding PATH tells echo to repeat the value of the variable PATH rather than its name.
Each user on a system can have a different PATH variable. When an operating system is installed, one default PATH variable is created for the root (i.e., administrative) account and another default is created that will be applied to all ordinary user accounts as they are added to the system. The PATH variable for the root user contains more directories than for ordinary users because it includes directories, such as /sbin and /usr/sbin, that contain programs that are normally used only by that user.
PATH variables can be changed relatively easily. They can be changed just for the current login session, or they can be changed permanently (i.e., so that the changes will persist through future sessions).
It is a simple matter to add a directory to a user’s PATH variable (and thereby add it to the user’s default search path). It can be accomplished for the current session by using the following command, in which directory is the full path of the directory to be entered:
For example, to add the directory /usr/sbin, the following would be used:
An alternative is to employ the export command, which is used to change aspects of the environment. Thus, the above absolute path could be added with the following two commands in sequence
or its single-line equivalent
That the directory has been added can be easily confirmed by again using the echo command with $PATH as its argument.
An addition to a user’s PATH variable can be made permanent by adding it to that user’s .bash_profile file. .bash_profile is a hidden file in each user’s home directory that defines any specific environmental variables and startup programs for that user. A hidden file is a file whose name begins with a dot (i.e., a period) and which is normally not visible; however, it can be seen by using the ls (i.e., list) command with its -a (i.e., all) option.
Thus, for example, to add a directory named /usr/test to a user’s PATH variable, it should be appended with a text editor to the line that begins with PATH so that the line reads something like PATH=$PATH:$HOME/bin:/usr/test. It is important that each absolute path be directly (i.e., with no intervening spaces) preceded by a colon.
It is sometimes desired to run a script or program which has been installed in a user’s home directory or some other location that is not in the user’s default search path. Such script or program can, of course, be run by typing in its absolute path. But an often more convenient alternative when the script or program is in the current directory is to merely precede the command name with a dot slash (i.e., a dot followed by a forward slash and with no intervening spaces). The dot is used in paths to represent the current directory and the slash is used as a directory separator and to separate directory names from file names.
MS-DOS also uses a PATH variable. However, it differs from Unix-like operating systems in that it searches the user’s current directory before it searches in any directories in that variable.
________
1 An extreme example would be the situation in which an ordinary user created a shell script such as rm -r /, which would delete all files and directories in the system for which the user had writing permission, and named this script ls. Were the system administrator to navigate to the directory in which this script was located and attempt to run the standard ls command in order to view the contents of that directory, the shell would instead run the script with the same name and thereby remove the contents of all currently mounted partitions on the computer!
Created June 2, 2006. Updated July 8, 2007.
Copyright © 2006 — 2007 The Linux Information Project. All Rights Reserved.