LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-08-2007, 07:57 PM   #1
And_pas
LQ Newbie
 
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3

Rep: Reputation: 0
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!
 
Old 05-08-2007, 09:21 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The problem is not the script, it's the shell expanding the regular expression before it's passed in.

Try this:

./findsrc.sh \*.c
 
Old 05-09-2007, 03:54 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
yes,

or use single quotes instead
 
Old 05-09-2007, 04:31 AM   #4
And_pas
LQ Newbie
 
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you both very much,
but i was wondering if there's a way it can be done,
without the backslash or quotes.
 
Old 05-09-2007, 04:42 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 05-09-2007, 05:07 AM   #6
And_pas
LQ Newbie
 
Registered: May 2007
Location: Greece
Distribution: FC6
Posts: 3

Original Poster
Rep: Reputation: 0
OK, i was just curious to know if it can be done.
Thank you very much.
 
Old 05-09-2007, 05:11 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
no, the shell will always expand any wildcards before it does anything else.

so you need single quotes or backslash escapes.

 
Old 05-10-2007, 04:33 PM   #8
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
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 04:35 PM.
 
  


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
using bash find command to list *.h and *.cpp wgillett Linux - Software 3 12-15-2005 12:55 PM
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-31-2004 12:37 AM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 09:04 AM
bash cant find format or mkfs command Dunadan1821 Linux - Software 3 08-12-2004 02:27 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM


All times are GMT -5. The time now is 04:33 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration