LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regular expressions : match 2 file names (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expressions-match-2-file-names-845505/)

hattori.hanzo 11-20-2010 08:34 AM

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

grail 11-20-2010 10:00 AM

-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

colucix 11-20-2010 10:45 AM

Or you can strictly search only these two files:
Code:

find . \( -name hosts.txt -o -name ipaddress.txt \)

GazL 11-20-2010 11:27 AM

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.

hattori.hanzo 11-21-2010 05:08 PM

Thanks to all.

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


All times are GMT -5. The time now is 08:20 AM.