Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove characters in string of script

Status
Not open for further replies.

zsfhaergta

Programmer
Oct 6, 2008
26
US
Hi,

I am trying to remove the ../ in front of a directory and replace it with only the filename. How do I do this?

I have:
X==../../filename

I want
X==filename

Thanks
 
Have you tried using the "basename" command?

"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
Thanks,
As you can see I haven't done very much script programming. Now what if filename were instead a directory?

X==../../directory
to
X==directory
 
basename doesn't care what the "basename" in the end means, it just strips off all leading directory names:

[tt]
X==../../filename
X=$(basename $X) # X will now contain "filename"

X==../../dirname
X=$(basename $X) # X will now contain "dirname"
[/tt]


HTH,

p5wizard
 
I'm not suggesting that you use it in this case but, for completeness, if you're using ksh, then you can use
Code:
longname=../path/to/file_or_dir
shortname=${longname##*/} # shortname is longname with everything up to the last / removed from the left
echo $shortname
file_or_dir
There's a good FAQ here


On the internet no one knows you're a dog

Columb Healy
 
Got it..thanks...heh I spent a long time trying to figure out how to manipulate the string without realizing there was a command to do it already...life of a programmer i guess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top