Bash absolute path of a file




















The downside is, it's Linux only - I spent about 5 minutes trying to find the OSX equivalent before coming to Stack overflow. I'm sure it's out there though. You're safe if you're targeting a directory, basename will return nothing and you'll just end up with double slashes in the final output. The initial question was very confused to start with, with an example poorly related to the question as stated.

The selected answer actually answers the example given, and not at all the question in the title. The first command is that answer is it really? And I fail to see what the second command is doing. One may or may not want to resolve explicity such symlinks. This may or may not yield an absolute path name, depending on how it is done.

And one may, or may not want to resolve it into an absolute pathname. The command readlink foo without option gives an answer only if its argument foo is a symbolic link, and that answer is the value of that symlink. No other link is followed. The answer may be a relative path: whatever was the value of the symlink argument. However, readlink has options -f -e or -m that will work for all files, and give one absolute pathname the one with no symlinks to the file actually denoted by the argument.

This works fine for anything that is not a symlink, though one might desire to use an absolute pathname without resolving the intermediate symlinks on the path.

This is done by the command realpath -s foo. In the case of a symlink argument, readlink with its options will again resolve all symlinks on the absolute path to the argument, but that will also include all symlinks that may be encountered by following the argument value. You may not want that if you desired an absolute path to the argument symlink itself, rather than to whatever it may link to.

Again, if foo is a symlink, realpath -s foo will get an absolute path without resolving symlinks, including the one given as argument. Without the -s option, realpath does pretty much the same as readlink , except for simply reading the value of a link, as well as several other things. It is just not clear to me why readlink has its options, creating apparently an undesirable redundancy with realpath. Exploring the web does not say much more, except that there may be some variations across systems.

Conclusion : realpath is the best command to use, with the most flexibility, at least for the use requested here. My favourite solution was the one by EugenKonkov because it didn't imply the presence of other utilities the coreutils package. But it failed for the relative paths ". It still fails if the user doesn't have the permission to cd into the parent directory of the relative path, though. This deals correctly with the case where the last element of the path is..

It does require python to work, but it seems to cover all or most of the edge cases and be very portable solution. In case of find , it's probably easiest to just give the absolute path for it to search in, e. If you are using bash on Mac OS X which neither has realpath existed nor its readlink can print the absolute path, you may have choice but to code your own version to print it.

Here is my implementation:. I was unable to find a solution that was neatly portable between Mac OS Catalina, Ubuntu 16 and Centos 7, so I decided to do it with python inline and it worked well for my bash scripts. Here's a rather short function that can be used to fully absolutize and canonicalize any given input path using only POSIX shell and readlink semantics:. If the referenced file does not exist only the directory path leading up to the final filename will be resolved.

If the any of the directories leading up to the file path does not exist a cd error will be returned. The number of 8 was chosen as this was maximum limit in Linux for many years before the changed it a total limit of 40 resolution for the entire path in version 4.

This code can also be easily reused for ensuring the current working directory matches the executed script's location in the filesystem a common requirement for shell scripts , by simply removing the function and subshell wrapper:. I found Eugen Konkov's answer to be the best as it doesn't require installing any program. However, it will fail for non-existent directories.

It solves the problem of non-existent directories by traversing up with dirname until cd succeeds, then returning the current directory plus everything that was removed by dirname. If you want to transform a variable containing a relative path into an absolute one, this works :.

This is a chained solution from all others, for example, when realpath fails, either because it is not installed or because it exits with error code, then, the next solution is attempted until it get the path right. Based on this answer by EugenKonkov and this answer by HashChange , my answer combines the brevity of the former with the handling of.

We can easily navigate throughout the filesystem with the help of these commands using both the relative and absolute path. Just follow the given basic example. Here, you can see how cd and pwd command help in navigation for different directories, i.

JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Bash Tutorial. Next Topic Bash Comments. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7.

Dalin: Try installing coreutils. The binary is named grealpath. This tool was introduced in GNU coreutils 8. WalterTross, it is available from the coreutils formula via Homebrew.

Show 16 more comments. Dennis Williamson Dennis Williamson k 87 87 gold badges silver badges bronze badges. I would rather use '-f' instead of '-e' so that we can see absolute path of a nonexistent file. This looks like the simplest to me, while also being portable. It seems to me that -m is the best option to use. Or did you mean those options are not found on OS X?

It seems to be a stunted BSD version If the given argument is not a symbolic link, readlink will print nothing and exit with an error , so readlink won't work for this purpose on OSX — SnoringFrog.

Show 6 more comments. James Bedford Don't forget to quote all the stuff. If you don't understand why, try your program on a file a b two spaces between a and b, SO eats one of them. Show 8 more comments. Expanding on dogbane's answer above here it is expressed as a function:! Why do I like this solution? What if dir doesn't exist? You can expand the function to handle that situation:!

How do you handle trailing '.. Well, it does give an absolute path in that case, but not a minimal one. If you want to resolve the '.. Not sure I understand your comment. What is it in the solution proposed here that excludes use from an interactive environment?

I would like to add that realpath comes from coreutils package, which also contains tools such as who , touch or cat. It's quite standar set of GNU tools, so you can be quite sure that this is installed on almost any linux based machine. That said, you are right: there are 2 issues with it: i you will likely miss it in default install on non GNU unix systems like OpenBSD ii this tools was introduced in version 8.

Well, it seems at least that Continuum Analytics makers of the Anaconda Python distribution liked this answer. It is implemented with a reference linking back here in their "activate" script , which is used to activate virtual environments created by the conda tool.

So… good one! This only works for directories since there is no else and cannot cope with ".. See my answer that should handle everything: stackoverflow. It may become somewhat standard over time, but it's certainly not now. Bash is more widely used than that. Flimm Flimm k 35 35 gold badges silver badges bronze badges. I do have readlink available on OS X KennethHoste: the -m option is not available on Mavericks. This relative path to absolute path converter shell function requires no utilities just cd and pwd works for directories and files handles..

Alexander Klimetschek Alexander Klimetschek 3, 28 28 silver badges 35 35 bronze badges. This doesn't work for directories with spaces, i. To overcome this, instead of quoting the entire argument to echo is there a reason for these quotes? I looked through dozens of half-cocked solutions and this definitely appears to be the most concise and accurate bash-only solution. There is no need for echo here. Six True, updated. The braces Matteo Italia k 17 17 gold badges silver badges bronze badges.

This approach can be improved with use of -maxdepth 1. Also don't forget about quoting - potential security issues here, depending how you use this. This fails if the file name is given as ". Show 1 more comment. Wow that's an elegant solution!! Unfortunately abspath. Looks portable to me. On the other hand, will break for example on a filename containing a newline. Again, you're right! Really, the behavior of echo command in zsh is different from bash. However, it is defined under XSI extension namely, echo should interpret the escape sequences.

Sorry, but I don't see the point. I already provided an answer as a script with more features that this and it doesn't need to be used within a shell script. Hence an absolute path is just a path relative to the root directory. This calls for several remarks: symbolic links can only be resolved if whatever they are supposed to link to is already created, which is obviously not always the case.

The commands realpath and readlink have options to account for that. Hence the concept is time or environment dependent. This expands a brief discussion of the topic in an answer to another similar question at Bash: retrieve absolute path given relative My conclusion is that realpath is better designed and much more flexible than readlink. Community Bot 1 1 1 silver badge.

I do not mind being downvoted, but I like to understand why so that I can learn something, either technical or regarding what is considered proper practice on the site.



0コメント

  • 1000 / 1000