linux - the - How to get full path of a file?

linux show current path in prompt (20)

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


In windows you can :-

  • Hold shift and right click on a file which gives you can option called "Copy as Path"

    This will copy the full path of the file to clipboard.

In Linux you can use the command :-

  • realpath yourfile to get the full path of a file as suggested by many.

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.

Beside "readlink -f" , another commonly used command:

$find  /the/long/path/but/I/can/use/TAB/to/auto/it/to/ -name 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.

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

For Mac OS, if you just want to get the path of a file in the finder, control click the file, and scroll down to "Services" at the bottom. You get many choices, including "copy path" and "copy full path". Clicking on one of these puts the path on the clipboard.

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

[email protected]:~$ ls $PWD/cat.wav

I like many of the answers already given, but I have found this really useful, especially within a script to get the full path of a file, including following symlinks and relative references such as . and ..

dirname `readlink -e relative/path/to/file`

Which will return the full path of the file from the root path onwards. This can be used in a script so that the script knows which path it is running from, which is useful in a repository clone which could be located anywhere on a machine.

basePath=`dirname \`readlink -e $0\``

I can then use the ${basePath} variable in my scripts to directly reference other scripts.

Hope this helps,


I suppose you are using Linux.

I found a utility called realpath in coreutils 8.15.

realpath realpath

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

In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:

set script_dir = `pwd`/`dirname $0`

$0 stores the exact string how the script was executed.

For e.g. if the script was launched like this: $> ../../test/test.csh, $script_dir will contain /home/abc/sandbox/v1/../../test

This is naive, but I had to make it to be POSIX compliant. Requires permission to cd into the file's directory.

if [ ${#} = 0 ]; then
  echo "Error: 0 args. need 1" >&2
  exit 1

if [ -d ${1} ]; then

  # Directory

  base=$( cd ${1}; echo ${PWD##*/} )
  dir=$( cd ${1}; echo ${PWD%${base}} )

  if [ ${dir} = / ]; then

  if [ -z ${base} ] || [ -z ${parentPath} ]; then
    if [ -n ${1} ]; then
      fullPath=$( cd ${1}; echo ${PWD} )
      echo "Error: unsupported scenario 1" >&2
      exit 1

elif [ ${1%/*} = ${1} ]; then

  if [ -f ./${1} ]; then

    # File in current directory

    base=$( echo ${1##*/} )
    parentPath=$( echo ${PWD} )

    echo "Error: unsupported scenario 2" >&2
    exit 1
elif [ -f ${1} ] && [ -d ${1%/*} ]; then

  # File in directory

  base=$( echo ${1##*/} )
  parentPath=$( cd ${1%/*}; echo ${PWD} )

  echo "Error: not file or directory" >&2
  exit 1

if [ ${parentPath} = / ]; then


if [ ! -e ${fullPath} ]; then
  echo "Error: does not exist" >&2
  exit 1

echo ${fullPath}

This will work for both file and folder:

    [[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } || 
    { cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }

This works with both Linux and Mac OSX ..

 echo $(pwd)$/$(ls file.txt)

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):

[email protected] ~ $ pwd

that's all folks!


find `pwd` | grep <filename>

Alternatively, just for the current folder:

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

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

the easiest way I found is

for i in `ls`; do echo "`pwd`/$i"; done

it works well for me

find $PWD -type f | grep "filename"


find $PWD -type f -name "*filename*"

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