Command Line – Find Files by Filename Length

command linefilenamesfind

I want to find all files by length of the file names.

For example if I want to find files of length 1, such as a.go, b.go.

I put:

grep '.\{1\}' file

But this does not work. What command can I use to find files by file name length?

Best Answer

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.)

Related Question