Quantcast
Channel: How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)? - Stack Overflow
Browsing index pages (46 articles)

Answer by jo_ for How to obtain the absolute path of a file via Shell...

in bash this:ls -d $(pwd)/the-fileworks also with wild cardls -d $(pwd)/*.ts

View Article


Answer by smac89 for How to obtain the absolute path of a file via Shell...

In zsh, yes there is.Assuming your path is stored in a variable, you can use the :P modifier to get the absolute path of the file.Example:f='../example.txt'echo ${f:P}The above will print the absolute...

View Article


Answer by Marinos An for How to obtain the absolute path of a file via Shell...

The top answers in this question may be misleading in some cases. Imagine that the file, whose absolute path you want to find, is in the $PATH variable:# node is in $PATH variabletype -P node#...

View Article

How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?

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 Benjamin Bannier for How to obtain the absolute path of a file via...

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

View Article


Answer by dogbane for How to obtain the absolute path of a file via Shell...

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

View Article

Answer by Dennis Williamson for How to obtain the absolute path of a file via...

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

View Article

Answer by hluk for How to obtain the absolute path of a file via Shell...

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 lessthanideal for How to obtain the absolute path of a file via...

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

View Article


Answer by wjv for How to obtain the absolute path of a file via Shell...

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 Flimm for How to obtain the absolute path of a file via Shell...

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

View Article

Answer by babou for How to obtain the absolute path of a file via Shell...

There is generally no such thing as theabsolute 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 peterh for How to obtain the absolute path of a file via Shell...

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/bashget_abs_filename() { # $1 :...

View Article


Answer by bsingh for How to obtain the absolute path of a file via Shell...

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 ${1%/*};...

View Article

Answer by Alexander Klimetschek for How to obtain the absolute path of a file...

This relative path to absolute path converter shell functionrequires no utilities (just cd and pwd)works for directories and fileshandles .. and .handles spaces in dir or filenamesrequires that file or...

View Article


Answer by ShellFish for How to obtain the absolute path of a file via Shell...

#! /bin/bashfile="$@"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 icyyd for How to obtain the absolute path of a file via Shell...

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 Alek for How to obtain the absolute path of a file via Shell...

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. I...

View Article

Answer by fred.johnsen for How to obtain the absolute path of a file via...

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 Atika for How to obtain the absolute path of a file via Shell...

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 file...

View Article
Browsing index pages (46 articles)


Latest Images