Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
a.go is a filename of length 4. a is a filename of length 1. This is Unix, not DOS where the name and extension were separate and an implicit . was always part of the filename. – Peter Cordes Aug 16 at 14:52

6 Answers 6

up vote 3 down vote accepted

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

share|improve this answer
This will also find directories that also match the pattern '?.go'. – slm Oct 1 '13 at 4:36
@slm, directories are one of many types of files, the OP didn't specify what type of files he was after. – Stéphane Chazelas Aug 15 at 21:05

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

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

share|improve this answer

You don't specify but if the files are within a directory structure you can use find to locate files that are length 1, with the help of grep:

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


$ find . -type f  | grep -P '/.{1}\.' | head -10
share|improve this answer
On MacOS X, use egrep and remove the -P option – Bart Kummel May 25 at 11:19
@BartKummel - actually on OSX I use ggrep: Also realize that this Q was not tagged as OSX so the use of grep is apt. – slm May 25 at 15:52

I would use something like this.

find . -type f -name "?.go"

So if you put two "?" then you are looking for file names with two characters and the extension.

share|improve this answer

When you want to use regular exxpression in find command , you should use "" such as:

find /var/log -iname "*.err" 

for yor case use :

find yourpath -type f -a -name "?.go"

You should replace your real path with yourpath, -a means And -type f means that you are only looking for regular files.

? in your query means one charachter such as DOS if you recall.

share|improve this answer

ls ?.* should give you desired results.

share|improve this answer
welcome to Unix.SE. It's unfortunately not useful to add answers to old questions that already have lots of good answers. (Unless there's a new better way to solve an old problem, or you have a better solution than any of the old answers.) If you want to answer some questions, look at the new questions on the front page. – Peter Cordes Aug 16 at 14:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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