45

'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
| improve this question | | | | |
5

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
| improve this answer | | | | |
33

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

| improve this answer | | | | |
  • 1
    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 – sdkks Oct 13 '17 at 10:14
  • 1
    Note that this also works for directories – Paul Razvan Berg Sep 30 '18 at 10:40
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.

| improve this answer | | | | |
11

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.
| improve this answer | | | | |
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'
| improve this answer | | | | |
  • 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 Jul 26 '19 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 – santiago arizti Jul 26 '19 at 22:18
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
| improve this answer | | | | |
  • 1
    This does not return the absolute path (which seems to be what OP was looking for) – Ad N Nov 8 '18 at 14:56
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
| improve this answer | | | | |
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
| improve this answer | | | | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.