How could I find directories with the name of specific length?
For example, I have bunch of directories which have length of the name equal to 33 chars ('a92e8cc611fdebcca3cf2fc8dc02c918', 'c442fb3f46d6c8bd17d27245290a9512' and so on).
Does find
utility accepts condition in form of the 'wc -c'? Or maybe some other utilities should be piped together?
Asked
Active
Viewed 3,939 times
3

Allan Bowe
- 12,306
- 19
- 75
- 124

altern
- 5,829
- 5
- 44
- 72
4 Answers
10
few ways with GNU find
$ find . -type d -name "?????????????????????????????????"
$ find /path -type d -printf "%f\n" | awk 'length==33'

ghostdog74
- 327,991
- 56
- 259
- 343
-
+1 but I think it should be "-type d" to find directories rather than files – Kaze no Koe Nov 04 '09 at 11:16
-
find . -regextype posix-extended -type d -regex ".{5}" – mhaller Nov 04 '09 at 11:23
-
aeh sorry, use 35 instead of 5. you need to count the leading "./" – mhaller Nov 04 '09 at 11:24
-
note that -regex match on the whole path. Therefore using -regex will not work if i start to find from a directory like for example: find /path -regextype posix-extended -type d -regex ".{35}" and not current directory – ghostdog74 Nov 04 '09 at 11:53
0
You don't have a Java tag, but if you did you'd write a FileFilter to encapsulate whatever criteria you have in mind and pass it to the java.io.File.list() method. If you want to traverse the entire directory structure tree you'll have to recurse, but it's entirely doable.
Sorry, it's just not *nix scripting.
The Apache Commons IO JAR has some nice file and directory utilities that could make this an easy job.

duffymo
- 305,152
- 44
- 369
- 561
-
-
Maybe not...I wrote 90% of it before I noticed the lack of a "java" tag, so I posted it anyway. – duffymo Nov 04 '09 at 11:16
0
Sure:
find dir -name '?????????????????????????????????'
(that's 33 time ?
).

Aaron Digulla
- 321,842
- 108
- 597
- 820
0
find . -type d -exec bash -c 'x=`basename {}`; [[ ${#x} == 33 ]] && echo $x ' \;
This might be slow based on number of files you have.
To clarify:
${#x}
-> finds the length of a string and
basename
-> finds the name of the final directory

Prosunjit Biswas
- 935
- 9
- 16