Regular Expression in Find command

Mohan

I want to list out the files which starts with a number and ends with ".c" extension. The following is the find command which is used. But, it does not give the expected output.

Command:

find -type f -regex "^[0-9].*\\.c$"
ColOfAbRiX

It's because the regex option works with the full path and you specified only the file name. From 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.

Try with this:

find -type f -regex ".*/[0-9][^/]+\.c$"

where you explicitly look for a string where "the format of your filename follows any string that terminates with a slash"

UPDATE: I made a correction to the regex. I changed .* in the filename to [^\]+ as after "any string that terminates with a slash" we don't want to find a slash in that part of the string because it wouldn't be a filename but another directory!

NOTE: The matching .* can be very harmful...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

find command from regular expression

From Dev

Regular expression find command cygwin?

From Dev

How to pass regular expression to the command find in Linux?

From Dev

Why is the hyphen symbol - not discoverable in a regular expression in the find command?

From Dev

sed command with regular expression

From Dev

find filename regular expression

From Dev

Find and replace regular expression

From Dev

Bash - find regular expression

From Dev

Regular expression to find abbreviations

From Dev

find: regular expression in name

From Dev

Find command with regular expressions

From Dev

Regular expression in CMake install command

From Dev

understand Regular expression in a sed command

From Dev

Regular expression to parse AT command response

From Dev

grep with regular expression in command line

From Dev

Regular Expression Match to Extract Command

From Dev

Regular expression to find bcrypt hash?

From Dev

regular expression to find until characters?

From Dev

Regular Expression to find uncommented strings?

From Dev

Java regular expression find substring

From Dev

Find matched text in regular expression

From Dev

Find with regular expression MongoDB + PHP

From Dev

php | find in array by regular expression

From Dev

Vim regular expression find and replace

From Dev

regular expression to find a particular code

From Dev

Find a regular expression for following string

From Dev

Find string with regular expression in python

From Dev

SAS find path with Regular Expression

From Dev

regular expression find end of string