find: "weird" regex behaviour


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find: "weird" regex behaviour
# 1  
Old 01-24-2011
Question find: "weird" regex behaviour

I have these two files in current dir:
  • oos.txt
  • oos_(copy).txt
I execute this find command:
Code:
find . -regex './oos*.txt'

And this outputs only the first file (oos.txt)! Smilie

Only if I add another asterisk to the find
Code:
find . -regex './oos*.*txt'

do I also get the second file (oos_(copy).txt).

What am I doing wrong? Smilie
# 2  
Old 01-24-2011
The correct thing should be:

Code:
find . -regex "./oos.*\.txt"

You're looking for any number of 's' before the dot ( that excludes the parentheses )
# 3  
Old 01-24-2011
Why is the red dot (after "oos") needed?
Code:
find . -regex './oos.*\.txt'

# 4  
Old 01-24-2011
It means any character so will match the parentheses .
This User Gave Thanks to Klashxx For This Post:
# 5  
Old 01-24-2011
Yup... you can also use wildcards:

Code:
find -name "oos*.txt"

This User Gave Thanks to verdepollo For This Post:
# 6  
Old 01-24-2011
In POSIX regular expressions, modifiers like * need a character before them to describe what they're modifying. . is a special character meaning 'match any character'. So .* means 'match any number of any character'.

I don't think you should be putting the path inside the expression. I don't think the path is actually part of what gets matched. You can limit what directories it goes inside with mindepth and maxdepth, to limit it to ./ that would be -mindepth 1 -maxdepth 1

If you use -name, you get behavior like you were expecting: find ./ -mindepth 1 -maxdepth 1 -name 'oos*.txt'

-iname acts like -name but is case-insenstive. It may be unavailable depending on your system, though.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-24-2011
unsure of the -regex

However,
Code:
$ find . -name 'oos*.txt'
./oos.txt
./oos_(copy).txt

works.

p.s. I did try your command, and did re-create the oddity you described. Just not sure about the -regex as part of a find command.
This User Gave Thanks to joeyg For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Explaining behaviour of sudo bash "$0" "$@";

I've found this script part on the stackoverflow: if ; then sudo bash "$0" "$@"; exit "$?"; fi I realized that sudo bash "$0" "$@"; is the only needed for me. But the strange thing happens when I move this line outside the IF statement: sudo bash "$0" "$@"; stops the... (9 Replies)
Discussion started by: boqsc
9 Replies

2. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

3. Shell Programming and Scripting

How to find a file which are not ends with ".zip" and which are ends with "*.log*" or "*.out*"?

I am new to bash/shell scripting. I want to find all the files in directory and subdirectories, which are not ends with “.zip” and which are contains in the file name “*.log*” or “*.out*”. I know below command to get the files which ends with “.log”; but I need which are not ends with this... (4 Replies)
Discussion started by: Mallikgm
4 Replies

4. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

5. UNIX for Dummies Questions & Answers

Behaviour of "find" command

Hi, I'm trying to understand why the find command below is not listing a directory which was modified long back from the number of days specified in the mtime part. :confused: user-aster :/mydir $ ls -ld 1607570a-4fed44bb-4988 drwxr-xr-x 3 xyz abc 4096 Jun 29 01:02 1607570a-4fed44bb-4988... (4 Replies)
Discussion started by: aster007
4 Replies

6. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. UNIX for Dummies Questions & Answers

"find": search the a regex of FILES??

I want to use the find command to search a ton of files, but I want to break it up into multiple machines. I want to search for files with "filename." in the title. The location I want to search is: /u/*/*/*/stuff On the first computer I want to search: /u//*/*/stuff Right now I am doing... (1 Reply)
Discussion started by: msf5042
1 Replies

9. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies

10. Shell Programming and Scripting

grep to find content in between curly braces, "{" and "},"

problem String ~~~~~~~~~~~~~~~~~~ icecream= { smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" } aend = {smart vc4 eatr kalu} output needed ~~~~~~~~~~~~~~~~~~ smart peopleLink "good" LC "happy" , smartpeopleLink "dull" LC "sad" smart vc4... (4 Replies)
Discussion started by: keshav_rk
4 Replies
Login or Register to Ask a Question