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