Is there an easy way I can print the full path of file.txt
?
file.txt = /nfs/an/disks/jj/home/dir/file.txt
The
In mac mentioned below line works. No need to add any fancy lines.
> pwd filename
Beside "readlink -f" , another commonly used command:
$find /the/long/path/but/I/can/use/TAB/to/auto/it/to/ -name myfile /the/long/path/but/I/can/use/TAB/to/auto/it/to/myfile $
This also give the full path and file name at console
Off-topic: This method just gives relative links, not absolute. The readlink -f
command is the right one.
Use readlink:
readlink -f file.txt
This works with both Linux and Mac OSX ..
echo $(pwd)$/$(ls file.txt)
The following usually does the trick:
echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"
This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.
function abspath () {
echo $1 | awk '\
# Root parent directory refs to the PWD for replacement below
/^\.\.\// { sub("^", "./") } \
# Replace the symbolic PWD refs with the absolute PWD \
/^\.\// { sub("^\.", ENVIRON["PWD"])} \
# Print absolute paths \
/^\// {print} \'
}