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.

6
  • 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) ? Commented Oct 13, 2018 at 9:46
  • @Goro for a medium-sized directory I receive -bash: /usr/bin/file: Argument list too long Commented Oct 13, 2018 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. Commented Oct 13, 2018 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... Commented Oct 13, 2018 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 Commented Oct 13, 2018 at 12:26

1 Answer 1

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
4
  • 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'. Commented Oct 13, 2018 at 10:32
  • I think -exec file -b {} + will reduce the number of file processes created. Commented Oct 13, 2018 at 16:52
  • Certainly will, great update, tks, I wanted to answer his original question as close as possible :-) Commented Oct 13, 2018 at 19:34
  • 1
    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. Commented Oct 14, 2018 at 8:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.