How to get full path of a file?

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 <command>

dir> <command> file.txt  

should print

/nfs/an/disks/jj/home/dir/file.txt

Solution 1:

Use readlink:

readlink -f file.txt

Solution 2:

I suppose you are using Linux.

I found a utility called realpath in coreutils 8.15.

realpath file.txt
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/file.txt

As per @styrofoam-fly and @arch-standton comments, realpath alone doesn't check for file existence, to solve this add the e argument: realpath -e file

Solution 3:

The following usually does the trick:

 echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"

Solution 4:

I know there's an easier way that this, but darned if I can find it...

jcomeau@intrepid:~$ python -c 'import os; print(os.path.abspath("cat.wav"))'
/home/jcomeau/cat.wav

jcomeau@intrepid:~$ ls $PWD/cat.wav
/home/jcomeau/cat.wav

Solution 5:

On Windows:

  • Holding Shift and right clicking on a file in Windows Explorer gives you an option called Copy as Path. This will copy the full path of the file to clipboard.

On Linux:

  • You can use the command realpath yourfile to get the full path of a file as suggested by others.