How to show full path of a file including the full filename in Mac OSX terminal?

43

9

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

ls config.inc.php
config.inc.php 

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

pwd
/Application/XAMPP/xamppfiles/phpmyadmin

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

/Application/XAMPP/xamppfiles/phpmyadmin/config.inc.php

Rob L

Posted 2014-02-15T00:30:37.857

Reputation: 621

Answers

3

Use realpath

E.g.:

$ realpath README.md 
/Users/joe/my/long/directory/structure/README.md

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

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

Purplejacket

Posted 2014-02-15T00:30:37.857

Reputation: 146

31

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

stephenbez

Posted 2014-02-15T00:30:37.857

Reputation: 491

1This was driving me nuts. I was thinking why -f wouldn't work on non-GNU and how to make it work. This works. Thanks – sdkks – 2017-10-13T10:14:23.677

1Note that this also works for directories – Paul Razvan Berg – 2018-09-30T10:40:17.973

20

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

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

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

yoonix

Posted 2014-02-15T00:30:37.857

Reputation: 633

1

Most good answers on in http://stackoverflow.com/questions/5265702/how-to-get-full-path-of-a-file don't work out of the box on OS X, however.

– Quentin Pradet – 2015-06-09T08:29:38.703

9

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.

Mowzer

Posted 2014-02-15T00:30:37.857

Reputation: 1 109

3

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'

FGreg

Posted 2014-02-15T00:30:37.857

Reputation: 205

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' – santiago arizti – 2019-07-26T22:05:19.077

hope you don't mind, I added my own answer but with a script in a $PATH dir instead of an aliase – santiago arizti – 2019-07-26T22:18:51.863

2

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

Mireia

Posted 2014-02-15T00:30:37.857

Reputation: 29

1This does not return the absolute path (which seems to be what OP was looking for) – Ad N – 2018-11-08T14:56:43.450

2

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:

#!/bin/bash
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 
/Users/santi/p/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 
/Users/santi/p/fakefile.md

santiago arizti

Posted 2014-02-15T00:30:37.857

Reputation: 121

1

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

find `pwd` -name file.txt

mark

Posted 2014-02-15T00:30:37.857

Reputation: 539

Perfect. 654321 – Jan Kyu Peblik – 2019-11-05T20:55:53.850