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

15 Answers

Use readlink:

readlink -f file.txt
show terminal

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


The following usually does the trick:

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

find $PWD -type f | grep "filename"


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

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.

You could use the fpn (full path name) script:

% pwd

% ls

% fpn *

fpn is not a standard Linux package, but it's a free and open github project and you could set it up in a minute.

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

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

This is explanation of what is going on at @ZeRemz's answer:

  1. This script get relative path as argument "$1"
  2. Then we get dirname part of that path (you can pass either dir or file to this script): dirname "$1"
  3. Then we cd "$(dirname "$1") into this relative dir
  4. && pwd -P and get absolute path for it. -P option will avoid all symlinks
  5. After that we append basename to absolute path: $(basename "$1")
  6. As final step we echo it

This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.

function abspath () {
   echo $1 | awk '\
      # Root parent directory refs to the PWD for replacement below
      /^\.\.\// { sub("^", "./") } \
      # Replace the symbolic PWD refs with the absolute PWD \
      /^\.\//   { sub("^\.", ENVIRON["PWD"])} \
      # Print absolute paths \
      /^\//   {print} \'

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")"; }

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,


This works with both Linux and Mac OSX ..

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

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.

the easiest way I found is

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

it works well for me

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.


linux file unix path