[ale] bash, full filename

Geoffrey esoteric at 3times25.net
Sat Nov 29 22:26:11 EST 2003


David Corbin wrote:
> On Saturday 29 November 2003 18:46, Geoffrey wrote:
> 
>>David Corbin wrote:
>>
>>>How can I take a filename (which may or may not be relative) and
>>>convert it to be a fully qualified filename, in bash?
>>
>>type vi |awk '{print $NF}'
>>
>>This will only work if it's in y


our path.  If not, you're going to have
>>to use locate or find.
> 
> 
> I don't think that comes close to what I want.  (But thanks for trying).
> 
> I want to be able to convert:
> 
> foobar          to  /home/dcorbin/foobar  (if I'm in /home/dcorbin)
> /etc/passwd  to  /etc/passwd
> ../fred/foobar to  /home/fred/foobar (if I'm in /home/dcorbin)
> 
> PATHs  (in the sense of your example) are just not a factor.

My mistake, I assumed (incorrectly) you were looking for full paths to 
executables.

I don't think you're going to find a single tool for the examples you've 
outlined.  Maybe we need to better understand the problem, why is it you 
want the full path to these files?  What are all the possible scenarios?

It's likely you'll need to throw a script together to handle the various 
possibilities:

for the first example (foobar) a simple find will locate it.  For the 
filename that's already fully qualified (/etc/passwd) simply checking 
for the leading '/' will resolve this problem.  For the filename 
beginning with the '..' you'd check for the '..' if it's there you'd cd 
to the previous directory and list the file.  If there's multiple '..', 
you'll have to deal with a bit of recursion.

So, really, without knowing the purpose of the problem, it's hard to 
know what all the possible examples might be:

/etc/../etc/passwd

../../usr/lib

foobar when it's /home/dcorbin/foobar

foobar when it's /home/fred/foobar

-- 
Until later, Geoffrey	esoteric at 3times25.net

Building secure systems inspite of Microsoft



More information about the Ale mailing list