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
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Mon Aug 13, 2007 6:53 am
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
doneI 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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Aug 13, 2007 7:43 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Aug 13, 2007 8:04 am
but this
Code: Select all
find . -name 'Interface.php' -exec dirname {} \; | while read d; do dirname $d; done
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Mon Aug 13, 2007 8:34 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 13, 2007 8:54 am
Code: Select all
find . -name "Interface.php" | while read d; do basename `dirname $d`; doneworks for me.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Aug 13, 2007 8:58 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 13, 2007 9:20 am
I'll clarify that mine works on OS X.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Aug 13, 2007 10:20 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Mon Aug 13, 2007 11:26 am
And I could say: I'm not sure what the for() is needed for.
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098 Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia
Post
by Chris Corbyn » Mon Aug 13, 2007 1:12 pm
volka wrote: And I could say: I'm not sure what the for() is needed for.
That dawned on me about 10 seconds after I posted my version. It's been a long day