Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 10630  / 1 Year ago, fri, november 18, 2022, 11:55:44

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?


More From » find

 Answers
4

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

[#28903] Sunday, November 20, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fishutt

Total Points: 391
Total Questions: 137
Total Answers: 106

Location: Mexico
Member since Tue, Aug 11, 2020
4 Years ago
;