简体   繁体   中英

How to find files on Linux with a name shorter than n characters?

How can I find files on Linux that have a filename shorter than n characters?

For example, to have something to work with, I am looking recursively for all filenames within /home/myuser that are less than 5 characters long (so, a file with name foo should be found, but with name barbaz not because its name is longer than 4 characters) - how can I do so?

The -name option of find operates on the file name without the path. You can't say "shorter than" in a pattern, but you can say "longer than" and negate:

find . -not -name '?????*'

Please try out this

find . -type f -exec basename {} \; | awk '{ if(length($0) < n) print $0 }'

where 'n' is no. of characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM