'ls' can show the file name, e.g

ls config.inc.php

'pwd' show current folder full path, e.g


Is there a command can put them together, would be able to show:


9 Answers 9


Use realpath


$ realpath README.md 

Answered here on stackoverflow: https://stackoverflow.com/a/3915075/441960

Make sure you download the coreutils on Homebrew as it isn't installed by default on all macs:

brew install coreutils

FYI, my version of MacOS (OSX):

$ uname -a
Darwin my-machine 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
  • Darwin 23.3.0 Darwin Kernel Version 23.3.0 seems to have realpath by default.
    – Ambareesh
    Mar 14 at 21:51

From here: https://stackoverflow.com/a/4031502/804713

macports and homebrew provide a coreutils package containing greadlink (GNU readlink). credit to Michael Kallweitt post in mackb.com

brew install coreutils

greadlink -f file.txt

  • 2
    This was driving me nuts. I was thinking why -f wouldn't work on non-GNU and how to make it work. This works. Thanks
    – user351764
    Oct 13, 2017 at 10:14
  • 1
    Note that this also works for directories Sep 30, 2018 at 10:40

There are many ways to do that; here is one example that may work for you:

claw:~ jonv$ echo `pwd`/`ls config.in.php`

If you want more examples, there are a bunch on this post at stackoverflow.


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.

You can also use the "find" command for listing all files with complete path:

find DirectoryName -type f

or just the following:

find . -type f
  • 4
    This does not return the absolute path (which seems to be what OP was looking for)
    – Ad N
    Nov 8, 2018 at 14:56
  • 1
    Only shows the absolute path if you are not in the same directory as the file/directory you are targeting
    – lobi
    Aug 19, 2020 at 14:42

Didn't like any of the given solutions so I made up my own based off of https://stackoverflow.com/a/22684652/953327

Create alias which simply is a function call that combines pwd and ls $1. You can then add this to your .bash_profile if you choose.

alias lsf='function _lsf(){ echo "$(pwd)/$(ls $1)"; };_lsf'

Example of use:

lsf registry.lock
-> /tmp/registry.lock

I chose lsf for "list full" or "list file", makes sense to me but feel free to modify.

Update (Thanks @santiago-arizti) To be able to use this with relative paths (e.g. lsf ../../readme.md)

alias lsf='function _lsf(){ (cd $(dirname "$1") && echo $(pwd)/$(basename "$1")) };_lsf'
  • I needed to call i like this lsf ../../readme.md so I altered your version a little bit alias lsf='function _lsf(){ (cd $(dirname "$1") && echo $(pwd)/$(basename "$1")) };_lsf' Jul 26, 2019 at 22:05
  • hope you don't mind, I added my own answer but with a script in a $PATH dir instead of an aliase Jul 26, 2019 at 22:18

The following will find a file within the working directory that matches file.txt and return its absolute path

find `pwd` -name file.txt

I wanted more than you needed, I needed to be able also to ask about files in a relative directory, like ../../readme.md, and the result should be something like /Users/santi/readme.md

So I created a (executable) script in a folder that is part of the $PATH called lsf with the following contents:

F=${1:?'usage lsf ../../readme.md'}
( cd $(dirname "$F") && echo $(pwd)/$(basename "$F") )

So you can use it like this:

santi@santis-mac:~/p/dir1/dir2$ lsf ../../readme.md 

No parameters gives you warning

santi@santis-mac:~/p/dir1/dir2$ lsf
/Users/santi/bin/lsf: line 3: 1: usage lsf ../../readme.md

Fake directory gives you warning (because of dirname)

santi@santis-mac:~/p/dir1/dir2$ lsf ../../fakedir/readme.md
/Users/santi/bin/lsf: line 5: cd: ../../fakedir: No such file or directory

Fake file but real directory doesn't warn (you can add validation if you need)

santi@santis-mac:~/p/dir1/dir2$ lsf ../../fakefile.md 

I wanted a solution that wouldn't require me to type out the name of my target file, so I added this to my ~./bash_profile file:

alias lsa="ls|xargs printf \"$(pwd)/%s\n\" *"

Now, when I run lsa in the terminal, I get an output like this:

enter image description here

If I really need just one file, I just grep it like lsa|grep Filename. I guess there are more advanced solutions with their own advantages, but this is good enough for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .