Get /full/path/filename.ext From Filename.ext

Home » CentOS » Get /full/path/filename.ext From Filename.ext
CentOS 5 Comments

This should be simple, but it’s not, unless I’m forgetting something.

Writing a script, an arg is a filename. So

fname=$1

But I want that expanded to include the full path and filename, not just what is given as the arg on the command line.

E.g., if the user’s cwd is /home/joe/a/b/c/ and he specifies

../x/file-a.ext

then the function/utility should transform that into the absolute path with filename:

/home/joe/a/b/x/file-a.ext

In the simplest scenario, the answer would be $PWD/file-a.ext, but that would by no means cover a portion of the possible scenarios.

You’d think this functionality would be included already in one or another linux utility. It’s kinda like the complement to the ‘basename’
utility. I’ve looked into the dark corners of ls, stat, file, bash, type, find, and a few other linux standards, but nothing seems to do this.

Any gurus out there know the utility which does this?

5 thoughts on - Get /full/path/filename.ext From Filename.ext

  • ‘readlink -f FILE’

    The -f (or –canonicalize) follows symlinks and prints the canonical path.

  • I don’t know if this is of interest as an alternative.

    I did find a cool functionality called locate and updatedb Updatedb creates the database of your files, locate does superfast searches.

    It essentially does a superfast “find” on your root filesystem, giving you the fully qualified path of all hits. You can create db’s on your other filessytems.

    The problem is that it can get stale, but you can update it before doing your searches. Plus it gives you a fully qualified path name with the results.

    So if you need to do a set of searches on a filesystem (or whole system)

    run updatedb on each target filesystem to create the db for that filesystem. then use locate to search each filesystem “db”… it takes seconds like ls instead of minutes like find….the more files in the FS, the quicker the searches compared to other tools.

    the best part is you can run the db’s when your systems are quiet, and the databases use minimal diskspace.

  • It may or may not be, but it doesn’t help the OP, who already knows where the file is, and just wants the full absolute path. locate will not help him there, especially if there are multiple files with the same filename.ext on that filesystem.

    locate has been around for many many years, certainly since at least CentOS 4, likely much earlier.

    There is (or at least, there should be) a script in /etc/cron.daily which updates it nightly.

    You can configure locate (in /etc/updatedb.conf) with which directories to index or not index. Then you do not need to maintain your own indexes, you can simply use the single system index.

    –keith

  • Dan,

    Thanks for responding. I’ve been using those two utilities for a long time, and they are indispensable. But they don’t solve the issue I’m having. Consider the case where there are multiple files with the same name but different paths. Also, it takes quite a while for the data used by those utilities to update, much too long for an interactive script.