Getting older, not necessarily wiser!
Having used the terms relative and absolute on occasion when referring to file paths and locations, I thought I should take some time to explain what these terms mean. This will be specifically in regard to a Linux file system, but may be generally relevant to other Operating Systems.
When discussing a path to a file or directory (in Linux), a forward slash delineates the path through the file system. In a Linux file system the top level is represented by the forward slash or β/β. All directories reside under the forward slash. For example, the user home directories are kept in the top level home folder which would be written as β/homeβ. The home directory for user 1 wound then be represented as β/home/user1β, and user 2 would be β/home/user2β.
/ |_ home |Β |_ user1 |Β |Β |_ Documents |Β |Β |Β |_ Story.odt |Β |Β |_ Pictures |Β |_ user2 |Β |Β |_Pictures |Β |Β | |_ Flower.jpg
Note, for a generic overview of the Linux file system please refer to Linux File System Hierarchy Standard.
Note, on a Windows system a path would look more like βC:\User\User1β. Note the use of a hard drive letter, and the backward slash. In Linux hard drives are attached to a directory in the file tree, not called out separately.
An absolute path to a file is the full path, starting at the top of the file tree. In the above example, the absolute path to the Story.odt file is β/home/user1/Documents/Story.odtβ.
When performing command line functions, absolute paths will always work (assuming you have permissions for the file which is another subject for another post). The downside to absolute paths is how long they can be. For example on a standard Debian system, if you need the gvim.svg icon, the absolute path from the top of the file tree would be β/usr/share/icons/hicolor/scalable/apps/gvim.svgβ. That is a drill down though six directories to finally get to the wanted file.
A relative path is the file path between two locations in the file system. Linux uses two special directory types for this. If you do a ls -a, you will see two hidden directories that appear at the top of the file tree. These are labeled β.β and β..β. The first is representative of your current working directory, and can be used for things like running code from your current directory. The second one represents the parent directory of your current working directory.
If you are in the user1 directory from above, and you want to list the contents of the user2 directory using an absolute path, then you would enter βls /home/user1β. However since both user1 and user2 have the same parent directory, /home, you could use the relative path βls ../user2β.
Lets say you are working in the /home/user1/Documents directory on the file Story.odt, and you want to grab a copy of Flower.jpg from user2. To grab a copy using relative paths you would enter either absolute or relativeΒ paths. Note that file/directory names in Linux are case sensitive.
cp /home/user2/Pictures/Flower.jpg /home/user1/Documents/Flower.jpg
or
cp ../user2/Pictures/Flower.jpg ./
The β..β brings us up one level to the home directory, from which we enter user2 and select the directory path and file to copy. Then for the destination we use β./β which means our current working directory, and because of the way cp works, it copies the file with the same name to that directory. Note we could have provided a different name for the file if we wanted.
Both absolute and relative paths can technically be used interchangeably. The situation will determine which is easier to type (shorter). This brief outline should provide you with enough information to use both as needed. It is up to you to determine which one will be more efficient for any given situation.
Add New Comment