Find files by the length of filename

If you just want to find the filenames, you can use the following command:

find -exec basename '{}' ';' | egrep '^.{100,}$'

That will run find, pulling off the name of the file or directory using basename and then look for any filename or directory name that is at least 100 characters. If you wanted to find a filename of an exact length, use {100} instead of {100,}.

If you want to find the filenames including the path, do this:

find | egrep '/[^/]{100,}$'

This will return the filename including the relative path to where you ran the find command.


grep looks for patterns in the contents of the files. If you want to look at the names of the files, you can just use a shell glob (if you want to consider only filenames in the current directory):

echo ?.go

(By the way, it's the shell that evaluates the glob, not the echo command.)

If you want to look in directories recursively, starting with the current directory, the tool to use is find:

find . -name '?.go'

(Note that you have to quote the pattern to prevent the shell from evaluating it as a glob before find gets invoked.)


First of all, grep searches through the contents of files, it will not search for file names. For that you want find. It does not have an option to set the filename length but you can parse it's output to get what you want. Here are some examples that I run in a directory containing the following files:

$ tree
.
├── a
├── ab
├── abc
├── abcd
└── dir
    ├── a
    ├── ab
    ├── abc
    └── abcd

Searching for files in this directory returns:

$ find . -type f | sort
./a
./ab
./abc
./abcd
./dir/a
./dir/ab
./dir/abc
./dir/abcd

So, to find the files with a length of X, we will need to remove the path and match only the characters after the last /:

 $ find . -type f | grep -P '/.{3}$'

The sed command just removes the ./. The -P flag activates Perl Compatible Regular Expressions which are needed for {n} which is a PCRE thing and the $ means "match the end of the string".

You could also simply do this:

find . -type f | grep -P '/...$'

That's fine for small numbers typing 15 dots to match filenames of length 15 is not very efficient.

Finally, if you want to ignore the extension and match only the file's name, do this (as @slm suggested):

find . -type f | grep -P '/.{1}\.'

However, this will also find files like a.bo.go. This is better if your filenames can contain more than one . :

find . -type f | grep -P '/.{1}\.[^.]+$'

NOTE: The solution above assumes that you have relatively sane filenames that don't contain new line characters etc. It will also count not ignore spaces in filenames when calculating the length which might not be what you want. If either of these is a problem, let me know and I'll update my answer.