How to show full path of a file including the full filename in Mac OSX terminal?
'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/phpmyadminIs 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.mdAnswered here on stackoverflow:
Make sure you download the coreutils on Homebrew as it isn't installed by default on all macs:
brew install coreutilsFYI, 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:
2macports 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
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.phpIf you want more examples, there are a bunch on this post at stackoverflow.
1In Mac OSX, do the following steps:
cdinto the directory of the target file.- Type either of the following terminal commands.
ls "`pwd`/file.txt"
echo $(pwd)/file.txt- Replace
file.txtwith your actual file name. - Press Enter.
You can also use the "find" command for listing all files with complete path:
find DirectoryName -type for 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.lockI 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.mdNo parameters gives you warning
santi@santis-mac:~/p/dir1/dir2$ lsf
/Users/santi/bin/lsf: line 3: 1: usage lsf ../../readme.mdFake 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 directoryFake 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