M HYPE SPLASH
// news

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

By Sarah Scott

'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

8 Answers

Use realpath

E.g.:

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

Answered here on stackoverflow:

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

From here:

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

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.

1

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.

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
2

Didn't like any of the given solutions so I made up my own based off of

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'
2

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

find `pwd` -name file.txt
1

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy