How to find directories with the name of specific length

4.1k Views Asked by At

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?

4

There are 4 best solutions below

4
ghostdog74 On BEST ANSWER

few ways with GNU find

$ find . -type d -name "?????????????????????????????????"

$ find /path -type d -printf "%f\n" | awk 'length==33'
2
duffymo On

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.

0
Aaron Digulla On

Sure:

find dir -name '?????????????????????????????????'

(that's 33 time ?).

0
Prosunjit Biswas On
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