0

I am using find to find all folders which include more than 20 of a specific type (.txt) as below:

 find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c | awk '$1 > 20'

The output looks like:

  44 ./fold9863
    202 ./fold689
    122 ./fold45
    202 ./fold2

with the numbers showing the number of the .txt files in each folder. Is there any way that I could use find to get the folder names without numbers (and without the leading ./), but seperated by a space? like this:

fold9863 fold689 fold45 fold2
  • Try changing the awk part to awk -v ORS=' ' '$1 > 20 { print $2 }'. (untested) – user414777 Sep 18 at 6:16
  • When I try find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c | awk -v ORS=' ' '$1 > 20 { print $2 }' , it doesn't give any outputs. @user414777 – Marco Sep 18 at 6:25
  • It may be because of your prompt. Add an ; echo after the command. Or use a (unquoted) command substitution: echo $(... | awk '$1 > 20 { print $2 }) which takes care of translating newlines to spaces. Just like your original example, all these will run into issues with filenames containing spaces or newlines. – user414777 Sep 18 at 6:31
  • Is there any way to remove ./ befor the name of folders? The output looks like ./fold9863 ./fold689 ./fold45 ./fold2 now @user414777 – Marco Sep 18 at 8:31
0

The following does print the folder names on one line delimited by a single space:

find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c | \
  awk '$1 > 20 { sub("^[.]/","",$2); printf("%s ",$2) } END { printf("\n") }'

A caveat is that if the folder name contains spaces, probably something will go wrong when you use the list. It is possible to quote the names:

find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c | \
  awk '$1 > 20 { sub("^[.]/","",$2); printf("\"%s\" ",$2) } END { printf("\n") }'

An extra space is at the end of the line, but with extra code that could also be avoided. One possibility:

find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c | \
  awk '$1 > 20 {sub("^[.]/","",$2);printf("%s\"%s\"",X,$2);X=" "} END {printf("\n")}'
| improve this answer | |
0
find . -type f -iname '*.txt' -printf '%h\n'|sort|uniq -c|awk '$1>20{sub("./","");printf "%s ",$2}'

Awk first removes ./ with the sub function. Then it prints the names followed by a single space with printf. As said in comments, you may want to add an echo so that the prompt does not appear to the right of the results.

| improve this answer | |
0

Replace your awk expression with this one, which removes any leading ./ from the folder name

awk '$1 > 20 { sub("^\\./", "", $2); printf "%d\t%s\n", $1, $2 }'

To get the folder names in a line, rather than in a column, modify the awk again, this time to avoid printing the numbers

awk '$1 > 20 { sub("^\\./", "", $2); printf "%s ", $2 } END { printf "\n" }'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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