How can I count different types of files in a folder using linux terminal?

Hey, I am amazed at how to count the number of files / extensions of different types in a folder. I also need to print them in a TXT file.

For example, I have 10 txt 20.docx files mixed into several folders.

Help me!

+4
source share
3 answers
find ./ -type f |awk -F . '{print $NF}' | sort | awk '{count[$1]++}END{for(j in count) print j,"("count[j]" occurences)"}' 

Gets all filenames with find , then uses awk to get the extension, and then uses awk again to count the occurrences

+10
source

Just with bash: version 4 needed for this code

 #!/bin/bash shopt -s globstar nullglob declare -A exts for f in * **/*; do [[ -f $f ]] || continue # only count files filename=${f##*/} # remove directories from pathname ext=${filename##*.} [[ $filename == $ext ]] && ext="no_extension" : ${exts[$ext]=0} # initialize array element if unset (( exts[$ext]++ )) done for ext in "${!exts[@]}"; do echo "$ext ${exts[$ext]}" done | sort -k2nr | column -t 
+1
source

this one so far seems unresolved, so here is how far I got the count files and sorted them:

 find . -type f | sed -n 's/..*\.//p' | sort -f | uniq -ic 
0
source

All Articles