LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do i get the path of the filename (https://www.linuxquestions.org/questions/programming-9/how-do-i-get-the-path-of-the-filename-191545/)

linuxzouk 06-09-2004 12:10 PM

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/

jim mcnamara 06-09-2004 12:18 PM

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.

linuxzouk 06-09-2004 12:25 PM

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

rkef 06-09-2004 12:52 PM

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...

linuxzouk 06-09-2004 01:07 PM

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

rkef 06-09-2004 01:10 PM

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

linuxzouk 06-09-2004 01:13 PM

"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 :)

jlliagre 06-09-2004 02:11 PM

try:
a="$(cd $(dirname $0);pwd)"
echo $a

jim mcnamara 06-09-2004 05:17 PM

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.

linuxzouk 06-09-2004 07:27 PM

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

Hko 06-09-2004 07:35 PM

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


linuxzouk 06-09-2004 07:42 PM

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 :)

Hko 06-09-2004 07:55 PM

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.

linuxzouk 06-09-2004 08:00 PM

oh, too bad then:(

and btw, if i copied your second method into /
it will return /root when double click

Hko 06-09-2004 08:12 PM

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.


All times are GMT -5. The time now is 05:17 PM.