Advertisement

Shell Scripting: extracting path from filename

Started by April 06, 2005 10:42 PM
4 comments, last by cloak 19 years, 7 months ago
Howdy, folks! I'm wrestling with a bash script and I'm stuck. Are there any gurus in the house who can suggest a way to move to a directory where a particular file resides? For example, an argument to my script may be 'somedir/moredir/file.txt'. How can I cd to the directory that the file is in? Is there any way to extract the path to the that text file into a variable? Any tips would be appreciated...
use basename or dirname to extract the path / filename

[root@moonshine ~]# basename 'somedir/moredir/file.txt'
file.txt
[root@moonshine ~]# dirname 'somedir/moredir/file.txt'
somedir/moredir

so $dir = `dirname 'somedir/moredir/file.txt'`

hope that helps,

Chris
Advertisement
Holy @^#&! Talk about fast response... this solves my problem. Thank you very much, Chris. Will this work on other shells besides BASH as well?
It should since (at least on my system) dirname and basename are binary programs in /bin, not builtin commands of bash.
thanx!
Glad to be of service
(actually got around to logging in)

-Chris
cloakwww.theblackroom.ca

This topic is closed to new replies.

Advertisement