Advertisement
Advertisement


How to get full path of a file?


Question

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
2014/06/05
1
741
6/5/2014 7:07:49 PM

Accepted Answer

Use readlink:

readlink -f file.txt
2014/07/31
1231
7/31/2014 5:26:08 PM

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

2018/11/12

The following usually does the trick:

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

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

[email protected]:~$ python -c 'import os; print(os.path.abspath("cat.wav"))'
/home/jcomeau/cat.wav

[email protected]:~$ ls $PWD/cat.wav
/home/jcomeau/cat.wav
2016/08/18

find $PWD -type f | grep "filename"

or

find $PWD -type f -name "*filename*"
2013/09/09

If you are in the same directory as the file:

ls "`pwd`/file.txt"

Replace file.txt with your target filename.

2015/03/14

Source: https://stackoverflow.com/questions/5265702
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]