1

I am trying to summarise the file extensions in a directory recursively.

find .| xargs -d "\n" -I@ echo "${@##.*}" | sort |uniq -c

But this is giving me a series of blank lines. Not what I wanted.

I am aware of:

find . -type f | sed 's/.*\.//' | sort | uniq -c from a similar question, but am curious about why my formulation doesn't work.

  • What are you trying to accomplish : are you trying to summarize file types based on mime types or based on extensions (these are two different things) ? – don_crissti Oct 13 '18 at 9:46
  • @Goro for a medium-sized directory I receive -bash: /usr/bin/file: Argument list too long – Tim Oct 13 '18 at 10:02
  • @don_crissti I'd like to know how to use both methods. I realise there are differences, and prefer the mime types. If the file command is taking too long, I'd revert to extension-based type. – Tim Oct 13 '18 at 10:05
  • ... if you want the mime types then find . -type f -exec file --brief --mime-type {} \; | sort | uniq -c should do. Your code also works if you use a for loop, e.g. for f in **/*; do... – don_crissti Oct 13 '18 at 10:08
  • @Tim what is the objective of the process, I mean what you would like to achieve by the end, more specifically why you are using cut sort and uniq? The commands seems to be meaningless in providing correct output for cut and sort. If you would please provide example of the desired output this will be very helpful for us to help – user88036 Oct 13 '18 at 12:26
3

You can get find to execute file for each file found.

find . -exec file -b {} \; |cut -f1|sort|uniq -c

edit

As @Ed-Nevile's comment below removing cut provides more detail for ASCII files.

find . -exec file -b {} \; |sort|uniq -c
  • 1
    Upvoting as you use -b, but I don't think you should cut the output since several different file types will begin with 'ASCII'. – Ed Neville Oct 13 '18 at 10:32
  • I think -exec file -b {} + will reduce the number of file processes created. – Kamil Maciorowski Oct 13 '18 at 16:52
  • Certainly will, great update, tks, I wanted to answer his original question as close as possible :-) – X Tian Oct 13 '18 at 19:34
  • 1. Can you please say why exec is preferred here over xargs? and 2. Can you tell me what is wrong syntactically with my attempts using bash string manipulation? I am trying not just to achieve a result, but also to understand why other methods don't work. – Tim Oct 14 '18 at 8:36

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