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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
05-08-2007, 06:57 PM
#1
LQ Newbie
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3
Rep:
Bash - find command
Hello - i need a little a help with the find command in a bash script.
I want the script to search the pwd and the subfolders for files, given a name pattern as a command line argument.
I have tried using
Code:
find . -type f -name "$1"
which works fine when the pattern is plain text.
But if the pattern contains wildcards
( for example: ./findsrc.sh *.c ), it doesn't work.
I have also tried using
Code:
find . -type f -name `echo $1 | sed 's/\*/\'\*\'/g'`
but still no luck.
I would greatly appreciate any help.
Thanks!
05-08-2007, 08:21 PM
#2
Moderator
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,755
Rep:
The problem is not the script, it's the shell expanding the regular expression before it's passed in.
Try this:
./findsrc.sh \*.c
05-09-2007, 02:54 AM
#3
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
yes,
or use single quotes instead
05-09-2007, 03:31 AM
#4
LQ Newbie
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3
Original Poster
Rep:
Thank you both very much,
but i was wondering if there's a way it can be done,
without the backslash or quotes.
05-09-2007, 03:42 AM
#5
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
Quote:
Originally Posted by And_pas
but i was wondering if there's a way it can be done,
without the backslash or quotes.
er, why?
basically no you can't, we have to have some rules y'know.
you can turn globbing off on the shell,
set -f
but then
ls * won't work
05-09-2007, 04:07 AM
#6
LQ Newbie
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3
Original Poster
Rep:
OK, i was just curious to know if it can be done.
Thank you very much.
05-09-2007, 04:11 AM
#7
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
no, the shell will always expand any wildcards before it does anything else.
so you need single quotes or backslash escapes.
05-10-2007, 03:33 PM
#8
Member
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102
Rep:
I don't know about bash but in zsh you can prefix a command with 'noglob' to prevent globbing expansion.
You might also want to look at the -regex/-iregex options to find (the pattern applies to the entire path though, so can be inconvenient).
Last edited by dawkcid; 05-10-2007 at 03:35 PM .
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 03:01 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News