How to get full path of a file?
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
Read more… Read less…
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
If you are in the same directory as the file:
ls "`pwd`/file.txt"
Replace file.txt
with your target filename.
2015/03/14
Licensed under CC-BY-SA with attribution
Not affiliated with Stack Overflow
Email: [email protected]