How to get full path of a file?
Asked Answered
O

36

1047

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
Oz answered 10/3, 2011 at 20:45 Comment(0)
B
1695

Use readlink:

readlink -f file.txt
Baresark answered 10/3, 2011 at 20:52 Comment(16)
I guess this works only for symbolic links..<br>Usage: readlink FILE If FILE exists and is a symlink, print the contents of the link and exit successfully. Otherwise print nothing and fail. --help display this help and exit --version output version information and exitOz
See #1056171Baresark
on mas osx it returns -f is illegal and proposed to use -n instead but returned nothing.Leakey
This is true, but has been observed before. Have a look at the link that I posted for Mac OS X (and possibly other in-the-wild implementations of readlink).Baresark
Just tested on 10.9.2, works fine. which readlink /usr/local/opt/coreutils/libexec/gnubin/readlink readlink --version readlink (GNU coreutils) 8.22Ninetta
@J0hnG4lt: that's because you installed coreutils with homebrew and changed your PATH to point to the unprefixed binaries. Installing coreutils, leaving PATH alone, and using "greadlink" would work as well.Messiah
Warning: this will print the target of a symbolic link if the argument is a symbolic link. So this solution does not work with symbolic links. ZeRemz's answer worksGard
@Baresark The -f is for full path. Works well in Linux, and the asker was using linux... hence why this is the correct answer.Caressive
on MAC: install homebrew then brew install coreutils then greadlink -f file.txtBibliotaph
readlink doesn't exist on AIX (IBM Unix) and probably not other other *nixes eitherFung
From the man page of readlink: Note realpath is the preferred command to use for canonicalization functionality.Trichosis
This answer has methods for both linux and windows https://mcmap.net/q/53139/-how-to-get-full-path-of-a-fileWheeled
Works great for both relative (./file.txt) and absolut path (/home/user/file.txt)!Lm
macOS 12.3 added support for readlink -f.Portauprince
It's better to use realpath -s file-or-dir or realpath --no-symlinks file-or-dir since the question is about how to get the full/absolute path of a file and not about how to get the target of symlinksCorody
realpath -s or readlink -m also works to get the absolute path of non-existing filesHandout
A
413

I suppose you are using Linux.

I found a utility called realpath in coreutils 8.15.

realpath -s file.txt
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/file.txt

Since the question is about how to get the full/absolute path of a file and not about how to get the target of symlinks, use -s or --no-symlinks which means don't expand symlinks.

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

Arvind answered 4/11, 2014 at 3:24 Comment(7)
Works with cygwin (with mintty 2.7.9)Doublepark
realpath was committed to the coreutils repo end of 2011, release 8.15 was done in January 2012, I answered the question (with the readlink suggestion) in March 2011 :)Baresark
realpath doesn't check for the file's existence, it just resolves paths. Asking for a full path of a not existing file should result in an error.Virgievirgil
@styrofoamfly realpath -e prints an error if the argument doesn't exist.Gritty
This is better than readlink since you can get the full path of files AND folders!Backman
Add the -s option to not expand symlinks.Eward
It's work for me with argument -q. Full command: realpath -q file_nameAffranchise
S
102

The following usually does the trick:

 echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"
Stokehold answered 10/3, 2011 at 21:1 Comment(6)
Hello. I found this answer best to my requirement. Can you explain how can I use a local variable instead of command line parameter(i.e. $1)Hakeem
LOCAL_VARIABLE="filename.txt" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P)/$(basename "$LOCAL_VARIABLE")Unipersonal
@SopalajodeArrierez Because readlink doesn't work if the file is a symlink, it will show you the target of the symlink instead of the symlink itself.Gard
I like this, because readlink takes me back to the parent dir where the symbolic link generates from, but this ignores it.Abeabeam
set .. && echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1") ### Output: /home/ravi/..Infliction
I think this should be the accepted answer because of its portability.Part
J
51

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"))'
/home/jcomeau/cat.wav

jcomeau@intrepid:~$ ls $PWD/cat.wav
/home/jcomeau/cat.wav
Joettajoette answered 10/3, 2011 at 20:57 Comment(0)
W
48

On Windows:

  • Holding Shift and right clicking on a file in Windows Explorer gives you an option called Copy as Path. This will copy the full path of the file to clipboard.

On Linux:

  • You can use the command realpath yourfile to get the full path of a file as suggested by others.
Wheeled answered 15/8, 2017 at 9:47 Comment(0)
M
35
find $PWD -type f | grep "filename"

or

find $PWD -type f -name "*filename*"
Moskva answered 1/7, 2013 at 6:8 Comment(2)
For me, on mac, this worked - instead of $pwd - find `pwd` -type file -name \*.dmpDevin
Thanks! This inspired me to use a simpler find $PWD that works perfectly for me when it's just a few filesMeliamelic
M
27

If you are in the same directory as the file:

ls "`pwd`/file.txt"

Replace file.txt with your target filename.

Malonylurea answered 14/3, 2015 at 0:43 Comment(1)
you can also do echo $(pwd)/file.txtPopple
S
7

I know that this is an old question now, but just to add to the information here:

The Linux command which can be used to find the filepath of a command file, i.e.

$ which ls
/bin/ls

There are some caveats to this; please see https://www.cyberciti.biz/faq/how-do-i-find-the-path-to-a-command-file/.

Supernatant answered 21/12, 2016 at 10:55 Comment(1)
This answer is too specific, the OP is not about finding commands.Hereby
N
5

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

% pwd
/Users/adamatan/bins/scripts/fpn

% ls
LICENSE   README.md fpn.py

% fpn *
/Users/adamatan/bins/scripts/fpn/LICENSE
/Users/adamatan/bins/scripts/fpn/README.md
/Users/adamatan/bins/scripts/fpn/fpn.py

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.

Neighborly answered 11/12, 2013 at 12:46 Comment(1)
Generally, pwd by itself should run fine on Linux and most other Unix-like OS environments.Aho
I
4

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.

Inductor answered 18/7, 2017 at 22:41 Comment(2)
prints current pwd pathStepdame
Yes, comma separated full paths for all files in pwd, for a file just do ls 'filename' instead of just 'ls'.Inductor
S
3
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
Selfeffacing answered 8/12, 2016 at 18:27 Comment(0)
C
3

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

Usage:

$ abspath file.txt
/I/am/in/present/dir/file.txt

Usage with relative path:

$ abspath ../../some/dir/some-file.txt
/I/am/in/some/dir/some-file.txt

With spaces in file name:

$ abspath "../../some/dir/another file.txt"
/I/am/in/some/dir/another file.txt
Chrissa answered 6/9, 2018 at 8:52 Comment(0)
W
3

This solution uses commands that exist on Ubuntu 22.04, but generally exist on most other Linux distributions, unless they are just too hardcore for s'mores.

The shortest way to get the full path of a file on Linux or Mac is to use the ls command and the PWD environment variable.

<0.o> touch afile
<0.o> pwd
/adir
<0.o> ls $PWD/afile
/adir/afile

You can do the same thing with a directory variable of your own, say d.

<0.o> touch afile
<0.o> d=/adir
<0.o> ls $d/afile
/adir/afile

Notice that without flags ls <FILE> and echo <FILE> are equivalent (for valid names of files in the current directory), so if you're using echo for that, you can use ls instead if you want.

If the situation is reversed, so that you have the full path and want the filename, just use the basename command.

<0.o> touch afile
<0.o> basename $PWD/afile
afile
Whoosh answered 10/9, 2021 at 8:30 Comment(0)
M
3

I was surprised no one mentioned located.

If you have the locate package installed, you don't even need to be in the directory with the file of interest.

Say I am looking for the full pathname of a setenv.sh script. This is how to find it.

$ locate setenv.sh
/home/davis/progs/devpost_aws_disaster_response/python/setenv.sh
/home/davis/progs/devpost_aws_disaster_response/webapp/setenv.sh
/home/davis/progs/eb_testy/setenv.sh

Note, it finds three scripts in this case, but if I wanted just one I would do this:

$ locate *testy*setenv.sh
/home/davis/progs/eb_testy/setenv.sh
Marauding answered 26/1, 2022 at 15:31 Comment(0)
M
2

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

Maun answered 19/12, 2012 at 6:16 Comment(1)
voted for this as this is the easiest and most relevant. However if you type ./test.csh you will have a path ending with /test/.Decent
E
2

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

function absolute_path { echo "$PWD/$1"; }
alias ap="absolute_path"

example:

ap somefile.txt

will output

/home/user/somefile.txt
Escalate answered 20/2, 2013 at 15:58 Comment(2)
But then ap ../foobar.txt will give /home/user/../foobar.txt, which is not generally what you want.Seventh
Also ap /tmp/foobar.txt will give /home/user//tmp/foobar.txt, which is wrong.Bonin
H
2

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.

Hamill answered 19/6, 2015 at 23:27 Comment(1)
brew install coreutilsBibliotaph
C
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,

Dave

Chimney answered 19/2, 2016 at 12:24 Comment(0)
C
1

This works with both Linux and Mac OSX:

echo $(pwd)$/$(ls file.txt)
Cecillececily answered 9/11, 2016 at 11:6 Comment(0)
F
1

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} \'
}
Fifteen answered 28/12, 2017 at 20:27 Comment(0)
U
1

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

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


if [ -d ${1} ]; then


  # Directory


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

  if [ ${dir} = / ]; then
    parentPath=${dir}
  else
    parentPath=${dir%/}
  fi

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

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

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


    # File in current directory

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

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


  # File in directory

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

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

if [ ${parentPath} = / ]; then
  fullPath=${fullPath:-${parentPath}${base}}
fi

fullPath=${fullPath:-${parentPath}/${base}}

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

echo ${fullPath}
Unheard answered 13/10, 2018 at 3:37 Comment(0)
W
0

This will work for both file and folder:

getAbsolutePath(){
    [[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } || 
    { cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
}
Wailful answered 6/8, 2015 at 16:58 Comment(0)
A
0

Usually:

find `pwd` | grep <filename>

Alternatively, just for the current folder:

find `pwd` -maxdepth 1 | grep <filename>
Archon answered 21/10, 2015 at 13:54 Comment(0)
G
-1
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
Gebelein answered 11/12, 2013 at 13:28 Comment(0)
P
-1

Another Linux utility, that does this job:

fname <file>
Pemmican answered 10/11, 2016 at 7:24 Comment(0)
H
-1

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.

Haemophiliac answered 22/3, 2017 at 1:6 Comment(0)
K
-2
fp () {
PHYS_DIR=`pwd -P`
RESULT=$PHYS_DIR/$1
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 stack overflow answer but cannot find that answer anymore)

Kokanee answered 26/2, 2015 at 16:11 Comment(0)
W
-2

The easiest way I found is:

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

It works well for me.

Wales answered 25/6, 2015 at 10:17 Comment(0)
C
-2

In Mac OSX, do the following steps:

  1. cd into the directory of the target file.
  2. Type either of the following terminal commands.
Terminal
ls "`pwd`/file.txt"
echo $(pwd)/file.txt
  1. Replace file.txt with your actual file name.
  2. Press Enter
Curtilage answered 23/3, 2016 at 6:12 Comment(0)
L
-3

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

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

Lutherlutheran answered 27/2, 2012 at 14:10 Comment(0)
S
-3

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
Sexagesima answered 29/2, 2016 at 15:32 Comment(0)
D
-3

This can be simplified

 ls <file_name> | pwd
Decapod answered 6/3, 2022 at 4:47 Comment(0)
W
-3
you@you:~/test$ ls
file
you@you:~/test$ path="`pwd`/`ls`"
you@you:~/test$ echo $path
/home/you/test/file
Windham answered 5/11, 2022 at 12:45 Comment(0)
N
-4

To get full path of a file :

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

CTRL + ALT + T

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

your@device ~ $ pwd

that's all folks!

Negligence answered 15/6, 2018 at 15:41 Comment(0)
O
-5

In mac mentioned below line works. No need to add any fancy lines.

> pwd filename
Olivette answered 18/12, 2018 at 19:56 Comment(1)
No it just prints the working directory (without the file name) as the name of the command suggests print working directoryMcclellan
C
-10

This will give you absolute path of the file:

find / -name file.txt 
Chiseler answered 25/7, 2013 at 11:29 Comment(3)
No, it will give the absolute path to every file on the filesystem of the same name, and take a long time to run.Impedance
@Impedance which is sometimes useful? Not really sure why this person's answer is so far downvoted, especially since it 1) answers the question 2) does not violate any of the asker's requirements, and 3) works in the case that you are not in the current working directory of the file. "It should only return ONE FILE PATH" was not in the asker's requirements.Why do you assume it would be? It turns out that this question is the one that came up when I wanted a full path for a file that was in a different working directory, and this answer does what I want. I'm sure there have been others like me.Aalst
@Aalst It is a bit like answering the question, "How do I get rid of the mice in my house?" with "Napalm" Technically, that will work, I suppose.Tour

© 2022 - 2024 — McMap. All rights reserved.