LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2005, 11:08 PM   #1
manoharsoft
LQ Newbie
 
Registered: Oct 2005
Location: Bangalore
Posts: 2

Rep: Reputation: 0
Help me...its urgent


I want to search for a file in a particular directory. how can i acheive this through a shell program....pls help me....
Thank u.....
 
Old 10-27-2005, 11:40 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, and welcome to LQ!

And can you please be a bit more specific? I don't
understand what you're trying to achieve, if you know
its location and its name, what is there to search for?

[edit]
Btw, next time choose a more sensible thread title,
please, one that gives people a clue as to what your
problem might be.
[/edit]


Cheers,
Tink

Last edited by Tinkster; 10-27-2005 at 11:55 PM.
 
Old 10-28-2005, 12:12 AM   #3
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 57
man ls
man grep
 
Old 10-28-2005, 02:18 AM   #4
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
man find
man locate

--Ian
 
Old 10-28-2005, 04:42 AM   #5
manoharsoft
LQ Newbie
 
Registered: Oct 2005
Location: Bangalore
Posts: 2

Original Poster
Rep: Reputation: 0
help me...its urgent

I want to accept a directory name from the user.Then i want to search for the directory starting from the root and check if the directory exits or not. Then i want to ask the user to enter a file name and then search for the same in the specified directory only. How can i do this using shell programming.

Looking forward for the reply...Pls help me...


Thank U...

Mano.....
 
Old 10-28-2005, 07:40 AM   #6
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
You have all the information you need to do that in those man pages, except for how to read input from a keyboard in shell scripts. Now you have that, it's just a question of putting the commands together to make a shell script.

If you run into any specific problems while doing this, that you can't solve using Google or by re-reading the man pages, then post them here by all means. However, I've never liked the policy of giving people scripts and saying "this should work". Give a man a fish... It's much better to learn by doing things yourself - or even better, trying and failing, then having someone help you over a specific hurdle.
 
Old 10-28-2005, 07:57 AM   #7
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
However putting them togehter is the tricky bit...

To find the directory you could use:

find / -name [dirname] -type d

The results of this command could be placed in a variable:

FOUND_DIR="`find / -name [dirname] -type d`"

Files work the same way except that you don't do -type d, so you would continue:

[ "$FOUND_DIR" ] && find $FOUND_DIR -name [filename]

the first [ "$FOUND_DIR" ] tests if there actually was a matching directory and only tries to search for files if there was. In it's most simple form your script would look something like:

#!/bin/bash
FOUND_DIR="`find / -name $1 -type d`"
[ "$FOUND_DIR" ] && find $FOUND_DIR -name $2

The above script will take the name of the directory to be searched as it's first parameter and the name of the file as the Read could be used to make the script to ask for these instead. Also note that that first find statement might in some cases return several matches which you will have to deal with using a loop. Which would make it something like:

#!/bin/bash
echo -n "Directory to search? "
read SEARCHDIR
echo -n "File to search within $SEARCHDIR? "
read SEARCHFILE

FOUND_DIR="`find / -name $SEARCHDIR -type d`"
if [ "$FOUND_DIR" ]; then
IFS="\n"
for EACH in "$FOUND_DIR"; do
find $EACH -name $SEARCHFILE
done
unset IFS
fi

Didn't test them, so there might be some glitches, but that's the basic idea.

Last edited by alienDog; 10-28-2005 at 08:20 AM.
 
Old 10-28-2005, 12:02 PM   #8
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 57
Quote:
However putting them togehter is the tricky bit...
Exactly, and in cases such as this where the user is hesitant to give out information as to why they want to do it and the question is very clear cut, again like this one, chances are it could be school work or the like. Now you see why DTsazza, IBall and myself have given out pretty vague answers. Never fear, hopefully if the OP passes they'll come back and let you know
 
Old 10-28-2005, 02:33 PM   #9
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
I still see no problem in sharing information be it a school project or not. In my opinion it doesn't make a lot of sense to refrain from answering or get very vague for the fear that the information might used in a way that somebody might dissaprove of. That way we could answer no questions at all, as we can never be quite sure what's the situation behind the question. Furthermore, I don't even think it's any of our business.

The way I see it, we're not here to guard people and their uses of information, but to help them out when a need arises. Information wants to be free

Manpages don't tell you how to connect UNIX commands together, that's their weakness. (S)he can learn it from the script example above, and if (s)he doesn't care, then what difference does it make anyway? I don't think people can be forced to learn.

Last edited by alienDog; 10-28-2005 at 02:35 PM.
 
Old 10-28-2005, 02:45 PM   #10
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
Oh no, I never meant to guard information - I just think too many people are ready to say, in essence, "here's what I want to do - now do it for me" - and I've seen in certain forum threads (just from Googling, not here) the person who supplies the answer making two or three revisions, based on feedback from the OP. That's just free software consulting, not help. And while it's fine if the responder's willing to do that, I don't think it's that good for the OP - even if they try to do it themselves and fail, they can at least grasp the concepts involved.

To put it more concisely, I'm against spoon-feeding and I won't do it, on the basis that it makes societies as a whole weaker.
 
Old 10-28-2005, 03:16 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally posted by alienDog
I still see no problem in sharing information be it a school project or not. In my opinion it doesn't make a lot of sense to refrain from answering or get very vague for the fear that the information might used in a way that somebody might dissaprove of. That way we could answer no questions at all, as we can never be quite sure what's the situation behind the question. Furthermore, I don't even think it's any of our business.

The way I see it, we're not here to guard people and their uses of information, but to help them out when a need arises. Information wants to be free ;)

Manpages don't tell you how to connect UNIX commands together, that's their weakness. (S)he can learn it from the script example above, and if (s)he doesn't care, then what difference does it make anyway? I don't think people can be forced to learn.
Give the man a fish, and he'll come back the next day for more.
Teach him to fish, and he'll be out there drinking beer all day. :D

And yes, I'm totally with Dtsazza on this one, help needs to be
help towards self-improvement, to widen ones scope, to learn how to
do things for oneself.

Pointing someone at the appropriate information is the BEST thing
one can do for them.


Cheers,
Tink
 
Old 10-28-2005, 03:26 PM   #12
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
There are significantly differing opinions on the subject of build strong societies. Maybe we won't get into that right now. It might be slightly off topic ;D I do see your point however.
 
Old 10-28-2005, 03:29 PM   #13
Dtsazza
Member
 
Registered: Oct 2005
Location: Oxford, UK
Distribution: Debian Etch (w/ dual-boot XP for gaming)
Posts: 282

Rep: Reputation: 30
Heh, yes, I do tend to over-justify things and make them all grandiose. I think we'd best not get into that now - and I harbour no ill will to people with different opinions, so we can just agree to disagree.
 
Old 10-28-2005, 08:34 PM   #14
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 57
Each to their own I guess
 
Old 10-29-2005, 05:07 PM   #15
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
If the user is entering a directory name you can use the shell's test (or [ ) command to determine its existence (man sh or bash for details). If the directory exists, cd into it and use the test for existence of the file the same way (different option, of course, but the man page will tell you all you need to know (and much more).
 
  


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
boot sector and lilo collapse !!!!! URGENT URGENT URGEN !!!!! frelihm Linux - Software 21 12-02-2009 11:21 AM
Urgent Question Regarding Urgent Questions! Need Help Now! Crashed_Again LQ Suggestions & Feedback 11 10-17-2007 09:07 PM
urgent help please !!! deepdark SUSE / openSUSE 5 01-03-2005 12:39 AM
Urgent..Im Having A Nightmare..Urgent!!!! midgcool Linux - Software 41 11-30-2004 11:19 AM
Urgent Urgent !!!! Mozilla Keeps All Your Deleted Emails !!!! odin123 Linux - Software 2 01-31-2004 03:22 AM


All times are GMT -5. The time now is 02:34 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration