LinuxQuestions.org
Review your favorite Linux distribution.
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 10-17-2007, 09:36 AM   #1
odinsride
LQ Newbie
 
Registered: Oct 2007
Location: DC
Distribution: Ubuntu, Red Hat
Posts: 3

Rep: Reputation: 0
Shell script to cd into the result of Find


I have this situation where I have a staging directory where a zip file reside. I then unzip it into this directory. I know the zip file will contain a Folder, but I will not know the name, depending on which zip file is there.

i.e., after zipping, my directory will look like this:

./staging/UnknownName.zip
./staging/UnknownName

And under this UnknownName directory will be a build.xml file that I can run ant on.

Is there a way to cd into the UnknownName directory without knowing the actual name? I tried find . -name build.xml|cd but it didn't do anything. Not sure if i'm doing this right.
 
Old 10-17-2007, 09:59 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Somtheing like this purhaps:
Code:
#!/bin/bash

find /top/dir -name build.xml | while read F ; do
    D=$(dirname "$F")
    cd "$D"
    #
    # Do stuff here.
    #
    cd -   # go back to previous working directory
done
 
Old 10-17-2007, 10:04 AM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
% pwd
/home/radoulov/t
zsh 4.3.4% ls -lR
.:
total 4
drwxr-xr-x 2 radoulov radoulov 4096 2007-10-17 16:52 dir1

./dir1:
total 0
-rw-r--r-- 1 radoulov radoulov 0 2007-10-17 16:52 build.xml
% find . -name build.xml -exec bash -c 'cd "${1%/*}";pwd' {} {} \;
/home/radoulov/t/dir1
with zsh:

Code:
zsh 4.3.4% pwd
/home/radoulov/t
zsh 4.3.4% eval "cd **/build.xml(:h)&&pwd"
/home/radoulov/t/dir1
 
Old 10-17-2007, 10:09 AM   #4
odinsride
LQ Newbie
 
Registered: Oct 2007
Location: DC
Distribution: Ubuntu, Red Hat
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hko View Post
Somtheing like this purhaps:
Code:
#!/bin/bash

find /top/dir -name build.xml | while read F ; do
    D=$(dirname "$F")
    cd "$D"
    #
    # Do stuff here.
    #
    cd -   # go back to previous working directory
done
I tried it but it won't CD into the directory. I did echo $D to make sure it's set correctly, and it was. Why isn't the cd command working?
 
Old 10-17-2007, 10:20 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by odinsride View Post
I tried it but it won't CD into the directory. I did echo $D to make sure it's set correctly, and it was. Why isn't the cd command working?
You said you wanted in a script - in the subshell you go to that directory ...
 
Old 10-17-2007, 10:33 AM   #6
odinsride
LQ Newbie
 
Registered: Oct 2007
Location: DC
Distribution: Ubuntu, Red Hat
Posts: 3

Original Poster
Rep: Reputation: 0
nevermind....

Last edited by odinsride; 10-17-2007 at 10:38 AM. Reason: nevermin d
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script: not able to find jags.singh Programming 8 06-15-2007 10:12 AM
how to find a pattern using shell script sharad Linux - General 1 05-23-2006 03:50 AM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
find shell script help liren Linux - Newbie 3 05-02-2005 03:05 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM


All times are GMT -5. The time now is 03:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration