Quantcast
Channel: How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)? - Stack Overflow
Browsing all 46 articles
Browse latest View live

Answer by Arj for bash/fish command to print absolute path to a file

The simplest if you want to use only builtins is probably: find `pwd` -name fileName Only an extra two words to type, and this will work on all unix systems, as well as OSX.

View Article



Answer by Eugen Konkov for bash/fish command to print absolute path to a file

The dogbane answer with the description what is coming on: #! /bin/sh echo "$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" Explanation: This script get relative path as argument "$1" Then we get...

View Article

Answer by Atika for bash/fish command to print absolute path to a file

An alternative to get the absolute path in Ruby: realpath() {ruby -e "require 'Pathname'; puts Pathname.new('$1').realpath.to_s";} Works with no arguments (current folder) and relative and absolute...

View Article

Answer by fred.johnsen for bash/fish command to print absolute path to a file

I have placed the following script on my system & I call it as a bash alias for when I want to quickly grab the full path to a file in the current dir: #!/bin/bash /usr/bin/find "$PWD" -maxdepth 1...

View Article

Answer by Alek for bash/fish command to print absolute path to a file

This is not an answer to the question, but for those who does scripting: echo `cd "$1" 2>/dev/null&&pwd||(cd "$(dirname "$1")";pwd|sed "s|/*\$|/${1##*/}|")` it handles / .. ./ etc correctly....

View Article


Answer by icyyd for bash/fish command to print absolute path to a file

Hey guys I know it's an old thread but I am just posting this for reference to anybody else who visited this like me. If i understood the question correctly, I think the locate $filename command. It...

View Article

Answer by ShellFish for bash/fish command to print absolute path to a file

#! /bin/bash file="$@" realpath "$file" 2>/dev/null || eval realpath $(echo $file | sed 's/ /\\ /g') This makes up for the shortcomings of realpath, store it in a shell script fullpath. You can now...

View Article

Answer by Alexander Klimetschek for bash/fish command to print absolute path...

This relative path to absolute path converter shell function requires no utilities (just cd and pwd) works for directories and files handles .. and . handles spaces in dir or filenames requires that...

View Article


Answer by bsingh for bash/fish command to print absolute path to a file

For directories dirname gets tripped for ../ and returns ./. nolan6000's function can be modified to fix that: get_abs_filename() { # $1 : relative filename if [ -d "${1%/*}" ]; then echo "$(cd...

View Article


Answer by peterh for bash/fish command to print absolute path to a file

Forget about readlink and realpath which may or may not be installed on your system. Expanding on dogbane's answer above here it is expressed as a function: #!/bin/bash get_abs_filename() { # $1 :...

View Article

Answer by babou for bash/fish command to print absolute path to a file

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). An...

View Article

Answer by Flimm for bash/fish command to print absolute path to a file

$ readlink -m FILE /path/to/FILE This is better than readlink -e FILE or realpath, because it works even if the file doesn't exist.

View Article

Answer by wjv for bash/fish command to print absolute path to a file

Here's a zsh-only function that I like for its compactness. It uses the ‘A’ expansion modifier — see zshexpn(1). realpath() { for f in "$@"; do echo ${f}(:A); done }

View Article


Answer by lessthanideal for bash/fish command to print absolute path to a file

The find command may help find $PWD -name ex* find $PWD -name example.log Lists all the files in or below the current directory with names matching the pattern. You can simplify it if you will only get...

View Article

Answer by hluk for bash/fish command to print absolute path to a file

If you don't have readlink or realpath utilities than you can use following function which works in bash and zsh (not sure about the rest). abspath () { case "$1" in /*)printf "%s\n" "$1";; *)printf...

View Article


Answer by Dennis Williamson for bash/fish command to print absolute path to a...

Try readlink which will resolve symbolic links: readlink -e /foo/bar/baz

View Article

Answer by dogbane for bash/fish command to print absolute path to a file

#! /bin/sh echo "$(cd "$(dirname "$1")"; pwd -P)/$(basename "$1")"

View Article


Answer by Benjamin Bannier for bash/fish command to print absolute path to a...

Try realpath. $ realpath example.txt /home/username/example.txt

View Article

bash/fish command to print absolute path to a file

Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it? Usage case: I'm in directory /a/b and I'd like to print the full path to file c on the...

View Article

Answer by Zorayr for bash/fish command to print absolute path to a file

Answer with Homebrewrealpath is the best answer, but if you don't have it installed, you must first run brew install coreutils which will install coreutils with lots of awesome functions. Writing a...

View Article
Browsing all 46 articles
Browse latest View live




Latest Images