Reputation: 141
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$"
Upvotes: 2
Views: 13314
Reputation: 36391
Just use -name
option. It accepts pattern for the last component of the path name as the doc says:
-name pattern True if the last component of the pathname being examined matches pattern. Special shell pattern matching characters (``['', ``]'', ``*'', and ``?'') may be used as part of pattern. These characters may be matched explicitly by escaping them with a backslash (``\'').
So:
$ find -type f -name "[0-9]*.c"
should work.
Upvotes: 2
Reputation: 1059
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...
Upvotes: 2