How can you quickly get the complete path to a file for use in terminal?

7 Answers 7

readlink -f foo.bar

or (install it first)

realpath foo.bar
  • This answer is more accurate than one accepted.
    – Kunok
    Oct 4, 2016 at 9:23
  • For the complete folder: ls | xargs realpath. May 17, 2017 at 13:57
  • 2
    The downside of readlink is that it will work even if the file doesn't exist. This can perpetuate bugs in very odd ways.
    – GregRos
    Jun 28, 2018 at 17:30
  • to copy the path to the os clipboard realpath foo.bar | xclip -selection c Feb 20, 2020 at 12:58

Just drag and drop the file in the terminal.

  • 1
    I'm putting this here so that I don't forget, let's hope it helps some of you :D Jan 26, 2011 at 19:33
  • Returns an "smb://" prefixed path for SMB mounted shares instead of the actual mounted path.
    – Kupiakos
    Sep 26, 2013 at 23:21
  • @Kupiakos: for me, gnome-terminal happily translates the dropped file path to '/home/alexcohn/.gvfs/…'
    – Alex Cohn
    Mar 17, 2014 at 15:11

All good answers; Here is a tip for another situation.

If you are browsing your files using nautilus and you want the complete path of your current directory, then press CTRL+L. This changes the breadcrumb buttons temporarily back to the old-style address bar, allowing you to copy the path.

  • … but this is still smb://-style, so it cannot be reused in terminal.
    – Alex Cohn
    Mar 17, 2014 at 12:36
  • Interesting; on my system (Ubuntu 13.10) I do not get a smb://-style path.
    – Sicco
    Mar 17, 2014 at 13:03
  • Exactly what I was looking for, I mean the terminal is a great place to ls but there is those times you work in a file folder views : ' )
    – edencorbin
    Feb 1, 2017 at 6:25

If it's an executable, then execute (in a terminal):

$ which your_executable

For example: $ which ls

  • This is the answer i was looking for Jun 15, 2015 at 19:18

In addition to dragging the icon, there are a few ways to get the full path without nautilus (or thunar, konqueror, et al.). You would then triple-click or click-drag and copy, potentially saving this in your clipboard manager*, and paste it where you need.
(pastie, klipper, glippy, glipper, anamnesis)

  • You can use find in a directory above your file. (If you don't know where it is, start where your shell drops you, [generally] in the top of your home directory.)
    find . | egrep filename

  • You can use locate to get the filename. (Run sudo updatedb if that hasn't been done recently.)

A more realistic example of using find would be something like :

$ find | egrep askubuntu | grep txt

To cut out the ones you don't like, e.g.:

find | egrep askubuntu | grep txt | egrep -v iteration
find | egrep askubuntu | grep txt | egrep -v 'iteration|meta|other'

locate is used much the same way, though grep is frequently more necessary:

locate myfile | egrep home | egrep -v 'mozilla|cache|local|bin|\.pyc|test' | grep \.py

This isn't the most efficient way to type this, but usually if I've lost a file, I do this iteratively, adding grep clauses as I go.


Easily done in python using os.realpath() function:

$ python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' ./VirtualBox\ VMs/                                      
/mnt/HDD/VirtualBox VMs

From a related answer,you can also use readlink

$ readlink -e ./out.txt                                                                                                  

If you simply copy a file in Nautilus, then the full path is copied.
Then paste it in the terminal. By simply pasting you get:


If you right-click and choose "Paste filenames" then you get:


with the quotes as shown.
This differs from Windows, that copies the file content instead of its name.

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