![]() have been replaced by a path to whetever they link to. Some people call canonical path ( or canonical file name or resolved path) an absolute path in which all symbolic links have been resolved, i.e. If it does not, it is also somewhat impervious to changes in the linking structure, but this is not necessarily required or even desirable. Hence an absolute path is just a path relative to the root directory.Ī path (absolute or relative) may or may not contain symbolic links. ![]() When it is in a symbolic link in a directory, it is generally intended to be relative to that directory (though the user may have other uses in mind). It may be the working directory if it is a relative path being manipulated by an application ![]() An absolute path is any path that start from the root "/" and designates a file without ambiguity independently of the working directory.(see for example wikipedia).Ī relative path is a path that is to be interpreted starting from another directory. There is generally no such thing as the absolute path to a file (this statement means that there may be more than one in general, hence the use of the definite article the is not appropriate). But since this seems to be THE question on stackoverflow for that including shell scripts that want to have minimal dependencies, I put this script solution here, so I can find it later :) I also understand that the original question was about an existing command line utility. Note: This is based on the answers from nolan6000 and bsingh, but fixes the file case. dir/./dir => /parent/dir # anything cd can handleĪbspath doesnotexist => # empty result if file/dir does not existĪbspath /file.txt => /file.txt # handle absolute path input dir/file.txt => /parent/dir/file.txtĪbspath. # generate absolute path from relative pathĪbspath. handles absolute paths as input (passes them through essentially).returns nothing if nothing exists at the given path.requires no utilities (just cd and pwd).This relative path to absolute path converter shell function If you want to resolve the '.' you will need to make the script like: get_abs_filename() ")" It will look like: /Users/bob/Documents/. Well, it does give an absolute path in that case, but not a minimal one. How do you handle trailing '.' or '.' in input ? Now it will return an empty string if one the parent dirs do not exist. You can expand the function to handle that situation: #!/bin/bash What if dir doesn't exist?Īs given above the function will fail and print on stderr if the directory path given does not exist. It is portable and doesn't require neither readlink or realpath which often does not exist on a default install of a given Linux/Unix distro. The solution exploits the fact that the Bash built-in pwd command will print the absolute path of the current directory when invoked without arguments. You can then use it like this: myabsfile=$(get_abs_filename "././foo/bar/file.txt") Forget about readlink and realpath which may or may not be installed on your system.Įxpanding on dogbane's answer above here it is expressed as a function: #!/bin/bashĮcho "$(cd "$(dirname "$1")" & pwd)/$(basename "$1")"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |