Skip to main content

Output Absolute Path

In Linux, if you want to output the absolute path of a file, you can use the readlink command or the realpath command. Here is how to use both:

  1. Using the readlink command:

    readlink -f filename
  2. Using the realpath command:

    realpath filename

For example, if you have a file named example.txt and you want to output its absolute path, you can use either of the following commands:

readlink -f example.txt

or

realpath example.txt

Both commands will output the absolute path of the example.txt file.

In Linux, you can use the realpath command to get the relative path of a file. By default, realpath outputs the absolute path, but you can use the --relative-to option to specify the base directory for the relative path.

For example, if you want to get the relative path of example.txt relative to the current directory:

realpath --relative-to=. example.txt

If you want to get the relative path of example.txt relative to a specific directory (e.g., /home/user):

realpath --relative-to=/home/user example.txt

This will output the relative path of example.txt relative to the /home/user directory.

If your system does not have the realpath command, you can also use readlink combined with other commands to achieve similar functionality, but realpath is the most straightforward and convenient method.