In my folder I got files like

/data/filename.log /data/filename.log.1 /data/filename.log.2 /data/filenamefilenamefilename.log /data/filenamefilenamefilename.log.2

I wish to use "find" command to list out files where length is greater than 15 characters.

I have tried the following, but none of them work:

find ./ -type f -iregex "/^.*{15,1000}$/" -print
find ./ -type f -iregex "/^.*{15}$/" -print
find ./ -type f -iregex "^.*{15}$" -print
find ./ -type f -iregex ".*{15}" -print
find ./ -type f -iregex ".{15}" -print
find ./ -type f -iregex ".{15,1000}" -print

Not sure what is the correct way?



I'd suggest Paul's version if you want to match on file name only. If you want to use -regex to match on the complete path, you can do e.g.

find /data -type f -regextype posix-egrep -regex ".{15}"

or for 15 or more characters

find /data -type f -regextype posix-egrep -regex ".{15}.*"

Check the man page for the different available regexp engines to use with -regextype.

You can also use the -regex option to look for only file names at 15 characters:

find /data -type f -regextype posix-egrep -regex ".*/[^/]{15}"

or 15 or more characters:

find /data -type f -regextype posix-egrep -regex ".*[^/]{15}"

The name parameter accepts simple globbing, so the following will work:

 find . -type f -name '????????????????*'

So that is 16 question marks, followed by an asterisk. The question mark matches a single character, but it must match, so 16 of them in a row ensure that there are 16 characters in the filename. The asterisk on the end permits any additional characters, meeting the "greater than 15 characters" requirement by matching "16 or more characters".

  • Actually it isn't clear whether you want the filename to be greater, or the entire path? – Paul Apr 11 '12 at 12:45
  • Thanks paul for the prompt reply and valuable feedback. I just want the filename..thanks :) – forestclown Apr 12 '12 at 1:41
  • Great. Then my approach is far simpler - it is just matching on filename. – Paul Apr 12 '12 at 1:51
  • This approach worked for me and is much simpler than defining regexptype, etc. Thanks! – Jesper Rønn-Jensen Jun 19 '13 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.