Shell script get full path of file




















How can I get the full path of a file in VBScript? On Scripting. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to get the fully qualified path for a file in VBScript? Ask Question. Asked 11 years, 11 months ago. Active 11 years, 9 months ago. Viewed 93k times. Peter Mortensen 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. I do recommend it. BTW any comments about realpath vs readlink or even about symbolic links are surplus to what I asked. Second comment: short answers are very often more useful than long lectures.

I want to avoid the " I need thhe full path to print on the screen and out put in file. How do you export the output exactly? Idera uses cookies to improve user experience.

By using our community you consent to all cookies in accordance with our Cookie policy. Site Search User. Ask the Experts.



0コメント

  • 1000 / 1000