2

Need to find files/folders with names longer than n characters (OS X 10.9.1).

Could you please help?

3 Answers 3

3

The most basic way is probably to use globbing:

find . -type f -name '???????????*'

This will list all files with a filename length greater than 10 characters. There are 11 question marks, and the asterisk matches those with longer length. Modify the number of question marks to match what you're looking for.

For greater than or equal to 10, use ??????????*, or for equal to 10 use ??????????.

3
  • 1
    You can also do it by regex, but it's less efficient because find -regex matches against the whole path: find -E . -type f -regex '.*/[^/]{11,}'
    – pietrodn
    Dec 23, 2013 at 19:45
  • 1
    → pietrodn: make this an answer, because it is a good one and scaling correctly to the variable nature of n in the OQ.
    – dan
    Dec 24, 2013 at 10:52
  • @danielAzuelos done! :-)
    – pietrodn
    Dec 25, 2013 at 21:52
5

You can use the find command with the -regex (regular expression) option. This is probably less efficient than globbing, because -regex matches against the entire path.

find -E . \( -type f -or -type d \) -and -regex '.*/[^/]{11,}'

Precisely, this command does the following:

  • recursively searches for files in the current folder and in the subdirectories (you can limit the depth with an additional -maxdepth n clause);
  • uses the extended syntax (-E) for regexes.
  • searches for files and folders (-type f -or -type d)
  • regex match: the string which follows the last slash in the file path must be 11 characters or longer.
2
  • 11 characters or longer including the extension?, please explain why globbing is more efficient.
    – l'L'l
    Dec 25, 2013 at 23:29
  • 11 character or longer including the extension, yes. I presume globbing is more efficient because it only matches against the filename and it doesn't use the full regex engine... but it's really only speculation.
    – pietrodn
    Dec 26, 2013 at 16:03
1

You can also just filter the output of find:

find .|awk -F/ 'length($NF)>=11'

-F sets IFS, NF is the number of fields, and $NF is the last field.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .