LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-28-2012, 04:23 AM   #1
kingston
Member
 
Registered: Mar 2008
Location: Bengaluru, India
Distribution: RHEL 5.5, Solaris 5.10
Posts: 215
Blog Entries: 1

Rep: Reputation: 21
find with "regex" option not working !!!


Hi Gurus
I have the following files.

-bash-3.2$ ls
123 23232 3455
-bash-3.2$ pwd
/home/xkinved/scripts/find

I was trying to list all these files by using the find command with regex option. But i failed, i am doing this as a part of learning "find with regex".

Quote:
-bash-3.2$ find . -type f -regex ^[1-3]*
-bash-3.2$ find . -type f -regex ^[1-3]*$
The above command returned nothing. Help me please.
 
Old 03-28-2012, 04:25 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
put the regex in single quotes, as otherwise, bash will interpret the *'s and other characters and turn it into mush.
 
Old 03-28-2012, 04:32 AM   #3
kingston
Member
 
Registered: Mar 2008
Location: Bengaluru, India
Distribution: RHEL 5.5, Solaris 5.10
Posts: 215

Original Poster
Blog Entries: 1

Rep: Reputation: 21
find . -type f -regex '^[1-3]*$'

I tried the above and didnt work. Infact i tried the following too,

find . -type f -regex \(^[1-3]*$\)
find . -type f -regex "\(^[1-3]*$\)"

Somewhere i read, "/\*" portion meant the starting of the expression and "\*/" meant the ending of the expression, but i failed in all
these cases. I tried with all other regextypes too.

Quote:
find . -type f -regex "/\*\(^[1-3]*$\)\*/"
Nothing is working as of now.
 
Old 03-28-2012, 04:55 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So when you read the man page on -regex, did you read it carefully?
Code:
-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.
 
Old 03-28-2012, 04:58 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This will not work: '^[1-3].... That is because the found file(s) always start with ./ It is followed by the a file name or directory depending on where the file is located.

If you are standing inside the directory with the files (I do believe that is: /home/xkinved/scripts/find), this would work:
Code:
find . -type f -regex '\./[1-3].*'
It looks for patterns starting with a dot (\.), followed by a forward slash (/) followed by 1, 2 or 3 ([1-3]) followed by any character (.*).

Have a look at the find man page.

Hope this helps.

A speedy grail beat me to it
 
1 members found this post helpful.
Old 03-28-2012, 04:58 AM   #6
kingston
Member
 
Registered: Mar 2008
Location: Bengaluru, India
Distribution: RHEL 5.5, Solaris 5.10
Posts: 215

Original Poster
Blog Entries: 1

Rep: Reputation: 21
find . -type f -regex '^[1-3]*$' -print

@Grail: Ok, if so, the above should print the files which matches the regex pattern. isnt it?
But in this case, its not doing that.
 
Old 03-28-2012, 05:01 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
-regex matches the whole path, not just the last component. find . -type f -regex '^\./[1-3]*$' will do what you want.
 
1 members found this post helpful.
Old 03-28-2012, 05:03 AM   #8
kingston
Member
 
Registered: Mar 2008
Location: Bengaluru, India
Distribution: RHEL 5.5, Solaris 5.10
Posts: 215

Original Poster
Blog Entries: 1

Rep: Reputation: 21
Quote:
find . -type f -regex '\./[1-3].*'
It works. And thanks for the explanation too Drunna.
Thanks to acid_kewpie and grail too, for your lightning reply.

Thanks Catkin.... your command is also working fine...

Last edited by kingston; 03-28-2012 at 05:05 AM.
 
  


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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
Using " find " with " -regex " Some help needed..!! WhisperiN Linux - Newbie 6 10-11-2010 03:30 PM
help :() How to find an address using "grep" & regEx's. linuxmaveric Programming 8 04-28-2010 12:31 AM
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM

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

All times are GMT -5. The time now is 12:00 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