Finding the directory of a file

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Finding the directory of a file

Post by Ollie Saunders »

I'm iterating over a bunch of directories looking for files called Interface.php

Code: Select all

for x in $(find . -name 'Interface.php')
do echo $x
done
I want to find out what the name of directory each Interface.php is it. So if it is in /foo/bar/Interface.php I want to get "bar" and put it in a variable.

This is what I'm doing at the moment but there's probably a better way.

Code: Select all

for x in $(find . -name 'Interface.php')
do  php -r '
        $split = explode("/", $argv[1]);
        $last = $split[count($split) - 2];
        exit($last);
    ' $x; 
    last=${?:1}
    echo $last
done
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Have a look at the basename and dirname commands in UNIX.

Sadly basename doesn't implement a pipe interface so you can't do this:

/var/log/messages <--- want "log" back

Code: Select all

dirname /var/log/messages | basename  #Gives error
basename `dirname /var/log/messages` #works
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

but this

Code: Select all

find . -name 'Interface.php' -exec dirname {} \; | while read d; do dirname $d; done
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I only want the last directory name btw. Not the whole path.
I wrote:So if it is in /foo/bar/Interface.php I want to get "bar" and put it in a variable.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

find . -name "Interface.php" | while read d; do basename `dirname $d`; done
works for me.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops typo, replace one dirname by basename in my script.

Code: Select all

find . -name 'Interface.php' -exec dirname {} \; | while read d; do basename $d; done
If feyd has tested his version take it, mine is untested.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'll clarify that mine works on OS X. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm not sure what the while() is needed for:

Code: Select all

for i in $(find . -name 'Interface.php'); do basename `dirname $i`; done
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And I could say: I'm not sure what the for() is needed for. ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

volka wrote:And I could say: I'm not sure what the for() is needed for. ;)
:lol: That dawned on me about 10 seconds after I posted my version. It's been a long day 8O
Post Reply