How to use command “find” to list files with specific filename length

centosfindregex

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?

Thanks!

Best Answer

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}"
Related Question