The Concept of Path in Linux

Before you start

Objectives: Learn the difference between an absolute path and the relative path, and useful commands, and common shortcuts for typing in paths.

Prerequisites: you should know what is Shell in Linux.

Key terms: path, directory, paths, command, linux, absolute, relative, current, run, system, file, shell


What is a Path

Good understanding of the path will help us grasp the concept of shell in Linux. Path is an environment variable that stores some special information. In general path is the location of some object in the file system. We can store paths in variables, so we can use them in commands. The path variable stores the list of directories that the system will search to execute commands, without entering relative or absolute paths. As you will see, paths in Linux are very similar to paths in DOS world.

We actually differentiate two types of paths. The first type is called the absolute path, and the other type a relative path. An absolute path is the full location from the root directory in the file system. Absolute paths in Linux will always start with the “/” sign (root directory). For example, “/usr” is absolute path. A relative path on the other hand will refer to objects relative to our current working directory. The working directory is our current position in the file system. To see the absolute path of our current working directory we can use the command: “pwd“. So, we can refer to objects in the file system based on where we are now, and to do that we use relative paths. When we use relative paths, we use certain symbols. One of the symbol is the single dot “./”, and the other symbol is the double dot “../”. These are actually two entries that exist in every directory. These are actually files, but they have a special meaning. The double dot refers to the directory above our current directory. So, to go one level up we can simply use the command “cd .. ” (cd stands for change directory). The single dot refers to the directory we are in. This is used a lot in Linux world. In contrast to Windows, in Linux when we try to run something from the current working directory we have to use the “. /” designation. In Windows, the command automatically tries to run the command in the current directory, while in Linux we have to refer to it if we want to run something from the current directory. We do that by using the the relative path of “./”. This is actually a sort of security feature and that’s why we use it.

When we navigate the file system, there are two shortcuts that are really helpful. Paths can be really long, so there will be a lot of typing, especially for absolute paths. Because of that there are shortcuts in Shell which help us to type faster. Bash, which is a GNU shell which is often used in Linux, has an auto complete feature. To use it we press the TAB key while we are typing in a command. The Bash will then search our path for anything that starts with what we typed and will auto complete that word for us. If there are multiple entries that match, we can press the TAB key multiple times to display all of the entries that match what we entered. Another useful shortcut is the history. Bash remembers every command that we run in order.  the five or to write command and then you immediately run the same command again I wouldn’t have to type it out again. To use this feature we press the up and down arrow keys. That way if we need to run some command again, we can simply get it from the history without typing it in again.