LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-20-2010, 08:34 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
Regular expressions : match 2 file names


How can we do a simple match by regular expressions on two filenames. I plan to use it in the command 'find -regex'

Code:
hosts.txt
ipaddress.txt
Thanks & Regards
 
Old 11-20-2010, 10:00 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
-regex is not really required here as -name can still use globbing and apart from the .txt ending there are no other real similarities, hence:
Code:
find <path> -name '*.txt'
This will find all files ending in .txt
 
1 members found this post helpful.
Old 11-20-2010, 10:45 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or you can strictly search only these two files:
Code:
find . \( -name hosts.txt -o -name ipaddress.txt \)
 
2 members found this post helpful.
Old 11-20-2010, 11:27 AM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 4,408
Blog Entries: 7

Rep: Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859Reputation: 1859
If you really want to use the '-regex' option of find, then you can do it like this:
Code:
find . -regextype posix-extended -regex '.*/hosts\.txt|.*/ipaddress\.txt'
Watch out for the special meaning of '.' in filenames, you'll have to escape it with '\'
Also remember that -regex works on the full path, not just the filename.

colucix' -name version is probably easier though.
 
1 members found this post helpful.
Old 11-21-2010, 05:08 PM   #5
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Thanks to all.

I ended up using colucix's version with the -o operator. It was interesting to see the regex solution though.
 
  


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
extracting emails from file using regular expressions in php on linux sriphp Linux - Software 3 09-14-2009 03:34 AM
Q: Searching text within a file WITHOUT regular expressions stormcloud Linux - General 1 07-30-2009 04:41 AM
mv and regular expressions, mv file(_*).foo folder jmcejuela Linux - Newbie 3 04-01-2009 03:35 PM
regular expressions: How can I match something except a word? [KIA]aze Programming 6 08-01-2007 07:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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