Find filenames of a certain length

To find files whos filenames have a certain length, you can use various combinations of the ls, egrep and find commands.

E.g. find files that have 9 characters followed by .txt:

me@pc ~/tmp $ ls ?????????.txt
analytics.txt  capilinks.txt  particles.txt  tmp123123.txt  tmp123333.txt
me@pc ~/tmp $ ls | egrep '^.{9}.txt'
analytics.txt
capilinks.txt
particles.txt
tmp123123.txt
tmp123333.txt

You can also use find, but this will search by full path not just filename:

me@pc ~tmp $ find . -regextype posix-egrep -regex '^./.{9}.txt'
./vlad/test.txt
./particles.txt
./tmp123123.txt
./capilinks.txt
./analytics.txt
./tmp123333.txt

Reference: How can I search for a file with fixed name length using ls?

Last modified: 27/11/2016 Tags: ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top