LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2004, 12:10 PM   #1
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Rep: Reputation: 15
how do i get the path of the filename


# /bin/bash

how do i get the path of the filename

e.g
/root/a/b/c/d/e/filename

is there any command to pas in
e.g.
command /root/a/b/c/d/e/filename

and it will return
/root/a/b/c/d/e/
 
Old 06-09-2004, 12:18 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
This is not a trivial problem.

The simple answer is to code this in the script:
Code:
mypath=`dirname $0`
mypath is now the path of the script itself.


However, it will not always work because of the different ways the script can be invoked. Sometimes mypath will show "./" for example.
 
Old 06-09-2004, 12:25 PM   #3
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
thanks jim mcnamara

i'm getting . as the result

is there, any other way that will definately return the path?
wouldn't mind if it's a script
 
Old 06-09-2004, 12:52 PM   #4
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
It sounds like you want just the pathname, without the filename?

A simple 'pwd' will do this. Or 'echo $PWD'.

Or, if the input '/bin/bash' is coming from elsewhere, you can do something like this:
Code:
cd `which bash`/..
pwd
cd -
TMTOWTDI...

Last edited by rkef; 06-09-2004 at 12:58 PM.
 
Old 06-09-2004, 01:07 PM   #5
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
thanks, but not seem to get the result i want

ok situation:

script.sh
# /bin/sh
echo `pwd`

location for script.sh:
/root/a/b/c/d/e/script.sh

at console if i execute it, i'll get the correct result
/root/a/b/c/d/e

but if i double click script.sh from gnome, the result is
/root

how do i get the directory of script.sh when double click on it
 
Old 06-09-2004, 01:10 PM   #6
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Well, that's a whole different beast. I have no idea how Gnome handles something like that (don't use it ).

As for 'echo `pwd`' though; I can say that it's redundant... 'pwd' itself gives exactly the same output.

HTHGL
 
Old 06-09-2004, 01:13 PM   #7
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
"Well, that's a whole different beast. I have no idea how Gnome handles something like that (don't use it )."
anybody else who can help me

"As for 'echo `pwd`' though; I can say that it's redundant... 'pwd' itself gives exactly the same output"
thanks
 
Old 06-09-2004, 02:11 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
try:
a="$(cd $(dirname $0);pwd)"
echo $a
 
Old 06-09-2004, 05:17 PM   #9
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
pwd may not be where he xecuted the script from
example
Code:
cd /someplace
source  /anotherplace/myscript
pwd will give you "/someplace" which is not where the script that was invoked lives. This is what I mean when I said it ain't easy.

And if the script is exec'ed it gets worse or if somebody does something wierd like this:

Code:
cat /anotherplace/myscript | /bin/ksh
-- there's almost no way to know where the script came from.
 
Old 06-09-2004, 07:27 PM   #10
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by jlliagre
try:
a="$(cd $(dirname $0);pwd)"
echo $a
results if i double click

dirname: too many arguments
Try `dirname --help' for more information.
/root
 
Old 06-09-2004, 07:35 PM   #11
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
It is possible, but it's not trivial.
To have it work from gnome (nautilus actually), try this:
Code:
#!/bin/bash

MYPID=$$
TMP=$(cat /proc/$MYPID/cmdline | tr '\000' '\n' | sed -n 2p)
SCRIPTDIR=${TMP%/*}

echo "This script lives in $SCRIPTDIR"
sleep 30  # Delay to see result in temp xterm
To make it also work when the script is started "the normal way", i.e. from the command line:
Code:
#!/bin/bash

MYPID=$$
TMP=$(cat /proc/$MYPID/cmdline | tr '\000' '\n' | sed -n 2p)
SCRIPTDIR=${TMP%/*}

if [ "${SCRIPTDIR:0:1}" != "/" ] ; then
 	SCRIPTDIR=$PWD/$SCRIPTDIR
	SCRIPTDIR=${SCRIPTDIR%/.}
fi

echo "This script lives in $SCRIPTDIR"
sleep 30  # Delay to see result in temp xterm

Last edited by Hko; 06-09-2004 at 07:38 PM.
 
Old 06-09-2004, 07:42 PM   #12
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
Hko:
your second way worked for both! though i don't really understand, but yeah!

thank you very much and also the rest who helped

Last edited by linuxzouk; 06-09-2004 at 07:45 PM.
 
Old 06-09-2004, 07:55 PM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
It's still not 100% complete. E.g. if the current working dir is /home/hko/dummy and the script is in /home/hko/bin. Then you can start the script with:

shell$ ../bin/thescript.sh

...but it will not correctly print the directory.

To solve this it becomes quite more complex, unless you have the "realpath" program installed. But AFAIK only Debain includes it, and Debian does not install it by default.
 
Old 06-09-2004, 08:00 PM   #14
linuxzouk
Member
 
Registered: Apr 2004
Location: Malaysia
Distribution: Fedora Core
Posts: 93

Original Poster
Rep: Reputation: 15
oh, too bad then

and btw, if i copied your second method into /
it will return /root when double click
 
Old 06-09-2004, 08:12 PM   #15
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Sorry about that.
But having the script in / is really an exceptional case, and a bad idea anyways.

Does your distribution have the "realpath" program available?
This would make it easier.

Last edited by Hko; 06-09-2004 at 08:28 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to seperate filename from path in perl mengle Programming 2 08-28-2005 01:35 AM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM
path and filename of compressed kernel ? ohernandez Linux - General 3 05-29-2002 06:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration