Bash Get Absolute Path from Relative Path

Bash get absolute path from relative path

When working with shell scripts, it’s common to need to get the absolute path of a file or directory from its relative path. However, before we dive into how to get the absolute path from a relative path, we must learn the difference between the two types of paths.

The relative path is relative to the current working directory. For instance, if the current working directory is home/user, which contains a text file named File1.txtin the directory /home/user/documents, the relative path to the file would be documents/File1.txt.

An absolute path, on the other hand, is a path that starts from the root directory of the file system. Using the same example above, the file’s absolute path would be /home/user/documents/File1.txt.

Now you are aware of the directory details, let us proceed with learning about obtaining an absolute path from relative path.

Using readlink Command

Use the readlink command to get an absolute path from a relative path in Bash.

In the above code, the -f option specifies that we want readlink to output the full path (i.e., the absolute path), and documents/File1.txt is the relative path to the file we want to get the absolute path for.

Using realpath Command

Use the realpath command to get an absolute path from a relative path in Bash.

In the above code snippet, we used the realpath command to retrieve the absolute path of File1.txt using the relative path documents/File1.txt, realpath resolved the relative path to an absolute path by starting at the current directory (/home/user/) and working its way up the directory tree until it reached the root directory.

Then, it combined the relative path with the absolute path of the current directory to get the complete absolute path of File1.txt, which was /home/user/documents/File1.txt.

Using pwdCommand

Use the pwd command to get an absolute path from a relative path in Bash.

The pwd stands for "print working directory" and prints the current working directory. In the above code, the $ pwd prints the current working directory to the console, documents in this -example.

The $ relative_path variable represents the file’s relative path we want to get the absolute path for. Then the absolute_path variable assigns the value of the current working directory (obtained by running pwd) concatenated with the file’s relative path. Next, the concatenation separates the directory and file names using the / character. And finally, the output is displayed on the console using the $ echo command.

Using cd Command

Use the cd command to get an absolute path from a relative path in Bash.

The cd command is more robust than the pwd command, as it resolves symbolic links and returns the canonical absolute path. The above code fence is similar to the previous one but uses the cd command along with the dirname command to change the current working directory to the directory containing the file. This is done so that the pwd command can be used to obtain the file’s absolute path, which is then stored in a variable absolute_path.

The basename extracts the file name from the relative path and appends it to the current working directory obtained by the pwd command. Next, the absolute path is printed to the console using the echo command. Finally, the cd - command is executed to return to the previous working directory.

Considering the above solutions, obtaining the absolute path of a file or directory from its relative path is an essential task when working with Bash Script. Each method has its advantages and disadvantages, so it’s up to you to choose the method that best fits your needs. By mastering these methods, you’ll be well-equipped to handle any file system navigation tasks you encounter in your shell scripts.

That’s all about bash get absolute path from relative path.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *