Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want something like this:

find "$dir" -type f -printf '%i:<alpha_char_count_of_base_filename>:%d:%p\n'

Then I want to sort first on inode number, then on alpha_char_count_of_base_filename. I think I have part of the solution figured out.

I know I can get the alpha_char_count_of_base_filename with this code:

basename "$filename" | tr -cd [:alpha:] | wc -m

However, trying something like this (don't laugh) doesn't work:

find "$dir" -type f -printf '%i:$(basename -print | tr -cd [:alpha:] | wc -m):%d:%p\n'

I've tried a lot of ideas and I have not lucked onto a working solution. Here's another idea I tried:

find "$dir" -type f -printf '%i:%d:%p\n' | awk 'BEGIN{FS=":";OFS=":";} {print $1,system("basename "$3" | tr -cd [:alpha:] | wc -m"),$2,$3"\n";}'

This seems to be getting me closer, but it prints the alpha_char_count_of_base_filename in the wrong location. I'm also not confident that is is good programming form.

My shell programming book doesn't offer me anything that would help me come up with a solution to this specific question. Neither does Google (so far).

This will be used as a solution to this question.

share|improve this question
up vote 2 down vote accepted
find "$dir" -type f -printf '%i:%f:%d:%p\n' | 
awk -F: -v OFS=: '{n=$2; gsub(/[^[:alpha:]]/,"",n); $2=length(n); print}'

hmmm, don't need the temp variable:

awk -F: -v OFS=: '{gsub(/[^[:alpha:]]/,"",$2); $2=length($2); print}'
share|improve this answer
Thanks! The only issue is I'm getting the alpha count of the entire path instead of the desired basename (filename alone). – MountainX Mar 29 '12 at 19:30
I think I can live with this. Thank you. I'll accept this answer if a basename answer doesn't come along. – MountainX Mar 29 '12 at 20:04
@MountainX, updated my answer to only use the basename -- it uses the %f formatting specifier to send the basename to awk where it gets replaced with the "alpha length" – glenn jackman Mar 29 '12 at 20:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.