Take the 2-minute tour ×
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

5 Answers 5

up vote 1 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

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

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

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

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.