Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert relative path to absolute path in Unix

Tags:

unix

I want to convert

Relative Path - /home/stevin/data/APP_SERVICE/../datafile.txt

to

Absolute Path - /home/stevin/data/datafile.txt

Is there a built-in tool in Unix to do this or any good ideas as to how can I implement this?

like image 901
qwarentine Avatar asked Jun 28 '12 13:06

qwarentine


People also ask

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

How do I get the absolute path of a current directory in Linux?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.

How do you set an absolute path in bash?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.


Video Answer


1 Answers

readlink -f /home/stevin/data/APP_SERVICE/../datafile.txt should do what you're looking for, assuming your Unix/Linux has readlink.

like image 73
twalberg Avatar answered Oct 27 '22 08:10

twalberg