Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve absolute path given relative

Is there a command to retrieve the absolute path given a relative path?

For example I want $line to contain the absolute path of each file in dir ./etc/

find ./ -type f | while read line; do
   echo $line
done
like image 910
nubme Avatar asked Sep 25 '22 10:09

nubme


People also ask

Can an absolute path be a relative path?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

How do you use absolute and relative paths?

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 is defined as the path related to the present working directly(pwd).

How do you convert relative path to absolute path in Python?

relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory.

How do I find absolute path in Linux?

To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.


2 Answers

Try realpath.

~ $ sudo apt-get install realpath  # may already be installed
~ $ realpath .bashrc
/home/username/.bashrc

To avoid expanding symlinks, use realpath -s.

The answer comes from "bash/fish command to print absolute path to a file".

like image 252
epere4 Avatar answered Oct 07 '22 12:10

epere4


If you have the coreutils package installed you can generally use readlink -f relative_file_name in order to retrieve the absolute one (with all symlinks resolved)

like image 140
Moritz Bunkus Avatar answered Oct 07 '22 10:10

Moritz Bunkus