Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand a possible relative path in bash

As arguments to my script there are some file paths. Those can, of course, be relative (or contain ~). But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved.

Is there any function for this?

like image 418
laar Avatar asked Aug 19 '11 19:08

laar


People also ask

How do I add a relative file path?

To set this option, look under the File menu and click Map Document Properties. Here, you can specify whether to store absolute or relative paths.

How do I get the full path in Linux terminal?

The best Linux command to get file path is using pwd command. To use this command, type “pwd” into your terminal and press enter. This command will print the current working directory. The output will be the file path.

How do you convert an absolute path to a relative 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 you set a relative path in Linux?

In the Linux operating system, the relative path is used to represent the current working directory. There is no concept to start or share the Linux relative path from the “/” (starting from root location). Instead, the relative path location or value starts with the current or present working directory.


1 Answers

MY_PATH=$(readlink -f $YOUR_ARG) will resolve relative paths like "./" and "../"

Consider this as well (source):

#!/bin/bash dir_resolve() { cd "$1" 2>/dev/null || return $?  # cd to desired directory; if fail, quell any error messages but return exit status echo "`pwd -P`" # output full, link-resolved path }  # sample usage if abs_path="`dir_resolve \"$1\"`" then echo "$1 resolves to $abs_path" echo pwd: `pwd` # function forks subshell, so working directory outside function is not affected else echo "Could not reach $1" fi 
like image 147
Amir Afghani Avatar answered Sep 21 '22 06:09

Amir Afghani