f



How to get rid of '../..'

Hi,

How can I get rid of '../..' in file name?.. For example, I need to
get absolute file name from string '../../usr/bin/'. I find one
solution with sed and I want to find maybe more elegant solution or
just another way how I can make it.
0
Sapfeer
2/27/2009 12:52:39 PM
comp.unix.shell 15484 articles. 2 followers. Post Follow

6 Replies
585 Views

Similar Articles

[PageSpeed] 39

Sapfeer <Sapfeer@gmail.com> writes:

>Hi,

>How can I get rid of '../..' in file name?.. For example, I need to
>get absolute file name from string '../../usr/bin/'. I find one
>solution with sed and I want to find maybe more elegant solution or
>just another way how I can make it.


In POSIX shell syntax:

	fullname=$(cd $(dirname $file); pwd)/$(basename $file)

Casper
-- 
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
0
Casper
2/27/2009 1:36:05 PM
2009-02-27, 13:36(+00), Casper H.S  Dik:
> Sapfeer <Sapfeer@gmail.com> writes:
>
>>Hi,
>
>>How can I get rid of '../..' in file name?.. For example, I need to
>>get absolute file name from string '../../usr/bin/'. I find one
>>solution with sed and I want to find maybe more elegant solution or
>>just another way how I can make it.
>
>
> In POSIX shell syntax:
>
> 	fullname=$(cd $(dirname $file); pwd)/$(basename $file)

fullname=$(cd -P -- "$(dirname -- "$file")" && pwd -P) &&
  fullname=$fullname/$(basename -- "$file")


-- 
St�phane
0
Stephane
2/27/2009 2:32:28 PM
Sapfeer <Sapfeer@gmail.com> writes:
> How can I get rid of '../..' in file name?.. For example, I need to
> get absolute file name from string '../../usr/bin/'. I find one
> solution with sed and I want to find maybe more elegant solution or
> just another way how I can make it.

Be careful of solutions involving simple textual substitution.  In the
presence of symbolic links, a Unix directory system isn't necessarily
just a tree.

For example, suppose you have a structure like this:

    /home/user1 -> /fs1/home/user1
    /home/user2 -> /fs2/home/user2

Then the following:

    cd /home/user2
    cd ../user1

will (try to) take you to /fs2/home/user1, which may not exist.

-- 
Keith Thompson (The_Other_Keith) kst@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Keith
2/27/2009 8:55:33 PM
On 27 Feb, 12:52, Sapfeer <Sapf...@gmail.com> wrote:
> Hi,
>
> How can I get rid of '../..' in file name?.. For example, I need to
> get absolute file name from string '../../usr/bin/'. I find one
> solution with sed and I want to find maybe more elegant solution or
> just another way how I can make it.

If your system has the readlink command then you can do
readlink -m filename
0
Spiros
2/27/2009 9:31:47 PM
Keith Thompson <kst-u@mib.org> writes:

> For example, suppose you have a structure like this:
>
>     /home/user1 -> /fs1/home/user1
>     /home/user2 -> /fs2/home/user2
>
> Then the following:
>
>     cd /home/user2
>     cd ../user1
>
> will (try to) take you to /fs2/home/user1, which may not exist.

Some shells understand this, and have a variable which can be set to
control this.
0
Maxwell
3/2/2009 1:38:30 AM
Maxwell Lol wrote:

> Keith Thompson <kst-u@mib.org> writes:
> 
>> For example, suppose you have a structure like this:
>>
>>     /home/user1 -> /fs1/home/user1
>>     /home/user2 -> /fs2/home/user2
>>
>> Then the following:
>>
>>     cd /home/user2
>>     cd ../user1
>>
>> will (try to) take you to /fs2/home/user1, which may not exist.
> 
> Some shells understand this, and have a variable which can be set to
> control this.

Since 2001, "some shells" includes all POSIX shells.  To force
the ../ to be interpreted physically, taking you to /fs2/home/user1
(if it exists), you would have to use cd -P ../user1

-- 
Geoff Clare <netnews@gclare.org.uk>
0
Geoff
3/2/2009 2:12:14 PM
Reply: