I'd like to find all files in a certain folder which have a filename of at least n
characters (extension included). Is there any way to achieve this with find
?
I'd like to find all files in a certain folder which have a filename of at least n
characters (extension included). Is there any way to achieve this with find
?
You could use the find
command with a -regex
test
$ find /path/to/folder -regextype posix-basic -regex '.*/.{5,}'
or
$ find /path/to/folder -regextype posix-extended -regex '.*/.{5,}'
Note that -regex
is a path match rather than a file match - hence you need to match the leading .*/
as well, before the 5+ character filename
Alternatively, for a pure bash solution, you could enable extended shell globbing and then use the pattern !(@(?|??|???|????))
meaning 'anything that does not match one or two or three or four characters'
$ shopt -s extglob
$ ls -d /path/to/folder/!(@(?|??|???|????))
If you want to include subdirectories, you can enable the globstar
option as well and add a **
wildcard i.e.
$ shopt -s extglob globstar
$ ls -d /path/to/folder/**/!(@(?|??|???|????))
for example
$ ls -d **/!(@(?|??|???|????))
abcde abcdef abcdefg subdir subdir/abcde subdir/abcdef subdir/abcdefg
while the non inverted matches (files shorter than 5 characters) are
$ ls -d **/@(?|??|???|????)
a ab abc abcd subdir/a subdir/ab subdir/abc subdir/abcd
To unset the options afterwards, use
$ shopt -u extglob globstar