LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 06: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, 08:21 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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, 02:54 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yes,

or use single quotes instead
 
Old 05-09-2007, 03: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, 03:42 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 04: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, 04:11 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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, 03: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 03:35 PM.
 
  


Reply



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 11:55 AM
trouble with find command in bash vrdhananjay Linux - Newbie 3 10-30-2004 11:37 PM
bash connot find the command Dunadan1821 Linux - Software 9 08-30-2004 08:04 AM
bash cant find format or mkfs command Dunadan1821 Linux - Software 3 08-12-2004 01:27 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 12:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:05 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
Open Source Consulting | Domain Registration