LinuxQuestions.org
Visit Jeremy's Blog.
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 03-13-2007, 08:53 AM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
find with regexp


hi,

how to use find with regexp? For example to remove all files that conatain t1, t2, t3 words I would write

find -type f -regex /t(1|2|3)/ -exec rm {} \;

but it does not work


thanks
oliver
 
Old 03-13-2007, 09:02 AM   #2
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
actually this does not work

find -type f -regex /t[123]/ -exec rm {} \;
 
Old 03-13-2007, 10:04 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Code:
find . -type f -print -exec egrep "t1|t2|t3" {} -exec rm -i {} \;
 
Old 03-13-2007, 10:04 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

I never tried -regex with files. But -name also takes patterns. Like:
find -type f -name "t[123]"
 
Old 03-13-2007, 10:29 AM   #5
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
thanks,

I have tried

find . -type f -print -exec egrep "t1|t2|t3" {} -exec rm -i {} \;

reports that there is no such directory (although there is)




on the other hand

find -type f -name "t[123]"

works if I make a small modification like

find -type f -name "*t[123]*"

but I am not sure how it will handle the situation when I have filenames with t8 t9 t10 words in it. Namelly "*t[8910]*" will affect files with t1 also
 
Old 03-13-2007, 12:18 PM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
If you remove that last * it wont match t10.
 
Old 03-13-2007, 08:18 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by man find
-regex pattern

File name matches regular expression pattern. This is a match on
the whole path, not a search. For example, to match a file named
`./fubar3', you can use the regular expression `.*bar.' or
`.*b.*3', but not `f.*r3'. The regular expressions understood by
find are by default Emacs Regular Expressions, but this can be
changed with the -regextype option.
First this means that "/" as delimiters are not needed. To take your first example:

Code:
 find -type f -regex '.*t\(1\|2\|3\).*' -exec rm {} \;
Note the .* on both sides, otherwise you're unlikely to match anything. To get rid of the annoying backslashes, don't use Emacs notation (it kind of sucks as a default):
Code:
 find -type f -regextype posix-egrep -regex '.*t(1|2|3).*' -exec rm {} \;
Although this particular regexp can use a character set:
Code:
 find -type f -regex '.*t[123].*' -exec rm {} \;
 
Old 03-13-2007, 11:30 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
how about this:
Code:
find . -name "t[0-9]*"
 
Old 03-14-2007, 02:19 AM   #9
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
thanks,

all works nicely
 
  


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
regexp help cliff76 Linux - Newbie 3 03-07-2008 02:15 PM
ls and regexp ygloo Programming 8 10-05-2006 03:50 PM
regexp stomach Linux - Software 7 02-15-2006 06:33 PM
little help for regexp EmOuBi Linux - Newbie 6 08-06-2005 02:19 AM
regexp help ... pld Programming 1 03-15-2005 03:45 PM

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

All times are GMT -5. The time now is 02:39 AM.

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