directory current linux How to get full path of a file?

15 Answers

I suppose you are using Linux.

I found a utility called realpath in coreutils 8.15.

realpath realpath
how to find the path of a file in unix

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


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"))'

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

If you are in the same directory as the file:

ls "`pwd`/file.txt"

Replace file.txt with your target filename.

Works on Mac, Linux, *nix:

This will give you a quoted csv of all files in the current dir:

ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'

The output of this can be easily copied into a python list or any similar data structure.

For Mac OS X, I replaced the utilities that come with the operating system and replaced them with a newer version of coreutils. This allows you to access tools like readlink -f (for absolute path to files) and realpath (absolute path to directories) on your Mac.

The Homebrew version appends a 'G' (for GNU Tools) in front of the command name -- so the equivalents become greadlink -f FILE and grealpath DIRECTORY.

Instructions for how to install the coreutils/GNU Tools on Mac OS X through Homebrew can be found in this StackExchange arcticle.

NB: The readlink -f and realpath commands should work out of the box for non-Mac Unix users.

You can save this in your "shell.rc" or just put in console

function absolute_path { echo "$PWD/$1"; }

alias ap="absolute_path"


ap somefile.txt

will output


You may use this function. If the file name is given without relative path, then it is assumed to be present in the current working directory:

abspath() { old=`pwd`;new=$(dirname "$1");if [ "$new" != "." ]; then cd $new; fi;file=`pwd`/$(basename "$1");cd $old;echo $file; }


$ abspath file.txt

Usage with relative path:

$ abspath ../../some/dir/some-file.txt

With spaces in file name:

$ abspath "../../some/dir/another file.txt"
/I/am/in/some/dir/another file.txt

find / -samefile file.txt -print

Will find all the links to the file with the same inode number as file.txt

adding a -xdev flag will avoid find to cross device boundaries ("mount points"). (But this will probably cause nothing to be found if the find does not start at a directory on the same device as file.txt)

Do note that find can report multiple paths for a single filesystem object, because an Inode can be linked by more than one directory entry, possibly even using different names. For instance:

find /bin -samefile /bin/gunzip -ls

Will output:

12845178    4 -rwxr-xr-x   2 root     root         2251 feb  9  2012 /bin/uncompress
12845178    4 -rwxr-xr-x   2 root     root         2251 feb  9  2012 /bin/gunzip


find `pwd` | grep <filename>

Alternatively, just for the current folder:

find `pwd` -maxdepth 1 | grep <filename>

In Mac OSX, do the following steps:

  1. cd into the directory of the target file.
  2. Type either of the following terminal commands.
ls "`pwd`/file.txt"
echo $(pwd)/file.txt
  1. Replace file.txt with your actual file name.
  2. Press Enter

Another Linux utility, that does this job:

fname <file>

fp () {
PHYS_DIR=`pwd -P`
echo $RESULT | pbcopy
echo $RESULT

Copies the text to your clipboard and displays the text on the terminal window.


(I copied some of the code from another answer but cannot find that answer anymore)

To get full path of a file :

1) open your terminal in the folder containing your file, by pushing on the keyboard following keys:


2) then type "pwd" (acronym of Print name of Working Directory):

your@device ~ $ pwd

that's all folks!

Create a function like the below (echoes the absolute path of a file with pwd and adds the file at the end of the path:

abspath() { echo $(pwd "$1")/"$1"; }

Now you can just find any file path:

abspath myfile.ext