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
share|improve this question
    
What about myfile or ./myfile – Leslie Satenstein Aug 18 '16 at 14:27

28 Answers 28

up vote 749 down vote accepted

Use readlink:

readlink -f file.txt
share|improve this answer
19  
That's what '-f' is for. – filmor Mar 10 '11 at 20:58
160  
Does not work on mac – mfcabrera May 15 '13 at 20:33
6  
See stackoverflow.com/questions/1055671/… – filmor May 16 '13 at 18:29
8  
@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. – Tim Smith Jul 25 '14 at 20:43
13  
on MAC: install homebrew then brew install coreutils then greadlink -f file.txt – To Kra Oct 16 '15 at 9:28

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} \'
}
share|improve this answer

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

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

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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/.

share|improve this answer
    
This answer is too specific, the OP is not about finding commands. – A.L Jun 21 '17 at 16:05
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
share|improve this answer

Another Linux utility, that does this job:

fname <file>
share|improve this answer

This works with both Linux and Mac OSX ..

 echo $(pwd)$/$(ls file.txt)
share|improve this answer

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
share|improve this answer
3  
Liking the "ls $PWD ..." method – David Mann Dec 12 '13 at 17:09
    
don't use python in such simple task as "get absolute path" – gaussblurinc Nov 6 '15 at 10:22
    
I generally don't. this was back in the days when I was trying to be first to correctly answer a question, and that was all I could come up with at the moment. – jcomeau_ictx Nov 7 '15 at 14:05
3  
Ah, you came to python from perl... :-) – Matt Stephenson Jan 6 '16 at 20:13

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
share|improve this answer

the below prints the absolute path of file.txt, which in this example is supposed to be in the current directory.

find file.txt

Let us suppose that file.txt is in the home directory of user foo. All of the below commands are equivalent and they all will return the same /home/foo/file.txt in Linux or /Users/foo/file.txt in OS X:

find ~/file.txt
cd ~/
find file.txt
find ./file.txt
share|improve this answer

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

share|improve this answer

Usually:

find `pwd` | grep <filename>

Alternatively, just for the current folder:

find `pwd` -maxdepth 1 | grep <filename>
share|improve this answer

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")"; }
}
share|improve this answer

the easiest way I found is

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

it works well for me

share|improve this answer

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.

share|improve this answer
2  
brew install coreutils – To Kra Oct 16 '15 at 9:24

If you are in the same directory as the file:

ls "`pwd`/file.txt"

Replace file.txt with your target filename.

share|improve this answer
3  
you can also do echo $(pwd)/file.txt – Alex Feb 7 '16 at 12:32
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)

share|improve this answer

I suppose you are using Linux.

I found a utility called realpath in coreutils 8.15.

realpath realpath
/data/ail_data/transformed_binaries/coreutils/test_folder_realpath/realpath
share|improve this answer
3  
Isn't it the simplest answer to the question? Single command that just exactly what was asked and works on Macs :P. Why is it so low scored? – Piotr Sobczyk Dec 3 '15 at 16:25
6  
@PiotrSobczyk, aha, because I answered this 3 years after the question was posted :) – computereasy Dec 3 '15 at 19:00
12  
Maybe because you have to install realpath for using it. – Maverobot Sep 16 '16 at 13:25
    
Works with cygwin (with mintty 2.7.9) – Scrambo Sep 27 '17 at 19:03

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.

share|improve this answer
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
share|improve this answer
find $PWD -type f | grep "filename"

or

find $PWD -type f -name "*filename*"
share|improve this answer
1  
For me, on mac, this worked - instead of $pwd - find `pwd` -type file -name \*.dmp – mrwaim Jan 5 '15 at 7:20

This will give you absolute path of the file:

find / -name file.txt 
share|improve this answer
4  
No, it will give the absolute path to every file on the filesystem of the same name, and take a long time to run. – Deestan Mar 20 '14 at 14:05
    
@Deestan 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. – DragonBobZ Dec 28 '17 at 18:55

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

share|improve this answer
3  
But then ap ../foobar.txt will give /home/user/../foobar.txt, which is not generally what you want. – bdesham Apr 9 '13 at 0:33
2  
Also ap /tmp/foobar.txt will give /home/user//tmp/foobar.txt, which is wrong. – David Given Oct 18 '14 at 10:39

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.

share|improve this answer
3  
This way works if you pass an "absolute path" to the find program... – rogerdpack Sep 9 '13 at 19:20

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

share|improve this answer
    
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/. – vinyll Dec 24 '15 at 11:27

The following usually does the trick:

 echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
share|improve this answer
    
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) – deeJ Feb 24 '14 at 8:23
1  
LOCAL_VARIABLE="filename.txt" && echo $(cd $(dirname "$LOCAL_VARIABLE") && pwd -P)/$(basename "$LOCAL_VARIABLE") – stiemannkj1 May 21 '14 at 2:47
5  
@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. – qwertzguy Nov 26 '14 at 19:02
1  
This works for AIX. readlink does not work for AIX. – anacron Apr 20 '15 at 6:59
1  
I like this, because readlink takes me back to the parent dir where the symbolic link generates from, but this ignores it. – Sukhdeep Singh May 26 '15 at 11:11

protected by Community Aug 19 '15 at 19:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.