For instance, it doesn't work on paths where the last component is a symlink. I like the answer, but it only works, if the user is allowed to cd into the directory. This might not always be possible. My personal favourite : Unfortunately, it failed when I fed it ".
Slightly improved version: stackoverflow. Show 1 more comment. ImHere 6, 25 25 silver badges 49 49 bronze badges. On Linux you can use readlink -e in tandem with dirname. Excellent entry with dirname , readlink , and basename. That helped me to get the absolute path of a symbolic link -- not its target.
Doesn't work when you want to return path to symbolic links which I just happen to need to do How could you ever find the absolute path to a path that doesn't exist? Ernest A Ernest A 7, 7 7 gold badges 32 32 silver badges 38 38 bronze badges. There's no need to cd back again. See stackoverflow. Your's is the best answer on this page, IMHO. For those interested the link gives an explanation as to why this solution works.
This is not very portable, dirname is a GNU core utility, not common to all unixen I believe. Oh my God, thank you. NB, this doesn't do the right thing with a path ending in..
Several issues are mixed : changing a relative pathname into an absolute one, whatever it denotes, possibly nothing. 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.
Eugen Konkov No-op if the path is already absolute. Alex Coventry Alex Coventry With resolving symlinks: python -c "import os,sys; print os.
Arkku Arkku Meow Meow 3, 1 1 gold badge 17 17 silver badges 16 16 bronze badges. Tobu Tobu Shunfang Lan Shunfang Lan 11 1 1 bronze badge.
This solution only works for bash, see also stackoverflow. Josh Barton Josh Barton 11 11 bronze badges. Thank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
Ivory Blakley Ivory Blakley 61 4 4 bronze badges. Dan Bray Dan Bray 6, 3 3 gold badges 46 46 silver badges 59 59 bronze badges. Eric H. On bash Sean Sean 2 2 silver badges 8 8 bronze badges. The Overflow Blog. Podcast Helping communities build their own LTE networks.
It is not just a marker, but already a full qualified address, a path. Exactly the same is true for all web servers. On Windows, the filesystem doesn't have the common root for the whole system but split between disks, so an absolute paths starts from the drive letter. So you can tell that windows is rather confusing, but for the simplicity we would pretend that we have only one disk, and within its boundaries the rules are pretty much the same as in Unix.
So now you can tell an absolute path from a relative one - it is starting from the root, which is:. The simplest example of relative path is just a file name, like index.
So one should be careful with relative paths. What you ought to know is that the system, when encountered a relative path, always builds it up to the absolute one. Both web-server and file system are doing that but different ways.
So, let's learn them. For the browser, this path is perfectly absolute, starting from the root of the web-server.
Whereas for the script it's only a part of the full path - the filesystem path. To get the working path to this file, we have to add the missing part. Like it was said before, for the browser, there are no files on the server. A site user never has an access to the server's file system. For the browser, there is a site root only.
Which is constant and always simply a slash. Therefore, to make an HTML link absolute, just build it from the site root - and you will never see a error for the existing file again!
So it goes for all the internal links on the site - images, js and css files, hyperlinks or any other resource that can be clicked on or loaded on the page.
So we are bound to use paths relative to the calling script, derived from the current script's location. Although technically absolute starting from a slash , this path is essentially relative to the calling script, because if the calling script will be moved into another directory, it won't find the configuration file anymore. This is why it is recommended to use a single entry point for your application. Or - as in our case - two entry points, one for web requests and one for console commands.
So for our fictional application we would have three files - an entry point for the web front, an entry point for console applications and a bootstrap file:. From now on we can use it to build absolute paths starting from the root directory as long as our scripts are called through the entry point either web or console one :. Of course, both entry points should implement a sort of resolver to call all other pages and console scripts but that's slightly out of scope of this article. There are may hepful commands and constants in PHP to ease the path interpolation.
Some of them are:. I am the only person to hold a gold badge in , and on Stack Overflow and I am eager to show the right way for PHP developers. Besides, your questions let me make my articles even better, so you are more than welcome to ask any question you have. Please refrain from sending spam or advertising of any sort.
Messages with hyperlinks will be pending for moderator's review. In response to the comment on my previous post. Since your post is pretty high up on search engines, I hope it helps somebody to get the path from web root:. DIR works well for including files in scripts. But for pointing to images or for URL generation, it doesn't work since you need a web root. Here an example:. On linux, on windows, on apache or nginx, with PHP backend or node. Sometimes the project path is different between local development and live deployment.
In this case it's helpful to have the path from web root to script if absolute paths from web root are used. And yes, relative urls just shouldn't be used at all. All local urls should be written as absolute paths, i. Great introduction, but doesn't help with the confusions introduced by require or include.
There are aleady better answers, but if for some reason you wanted to use the shell to get the full path of the file this is more useful if you're going to perform some other operation on the file with the shell , on a Unix-like system you could run:. A realworld example might be if you're editing a config file, but don't have write permissions and don't want ot run Vim as root , you could edit the file, then run:.
I use vim airline for that and some nice features:. You can show the complete path of the file you are editing in the title bar, which is a convenient way to show the file path in my opinion. In order to do that, you need to set the title option and titlestring. Current I am using the following settings:. It will show the hostname of Vim, then the complete path of the file and followed by last modified time of the file.
See the figure below for a demo. I wrote this function to get the full path of my current directory and then mapped it to the lualine any status line is the same. I am pretty much sure, this solution is package ignostic, so no need to worry about the lualine status bar, any status bar is ok. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. How can I see the full path of the current file? Ask Question. Asked 6 years, 11 months ago. Active 10 days ago. Viewed k times. Improve this question. Add a comment. Active Oldest Votes. If the count is higher than 1 the current buffer number is also given. Improve this answer. Jasper Good catch!
0コメント