user1304473
user1304473

Reputation: 39

Linux:How to list the information about file or directory(size,permission,number of files by type?) in total

Suppose I am staying in currenty directory, I wanted to list all the files in total numbers, as well as the size, permission, and also the number of files by types.

here is the sample outputs:

Here is a sample :

Print information about "/home/user/poker"

total number of file : 83

pdf files : 5

html files : 9

text files : 15

unknown : 5

NB: anyfile without extension could be consider as unknown.

i hope to use some simple command like ls, cut, sort, unique ,(just examples) put each different extension in file and using wc -l to count number of lines

or do i need to use grep, awk , or something else?

Hope to get the everybody's advices.thank you!

Upvotes: 1

Views: 485

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

Best way is to use file to output only mimetype and pass it to awk.

file * -ib | awk -F'[;/.]' '{print $(NF-1)}' | sort -n | uniq -c

On my home directory it produces this output.

 35 directory
  3 html
  1 jpeg
  1 octet-stream
  1 pdf
 32 plain
  5 png
  1 spreadsheet
  7 symlink
  1 text
  1 x-c++
  3 x-empty
  1 xml
  2 x-ms-asf
  4 x-shellscript
  1 x-shockwave-flash

If you think text/x-c++ and text/plain should be in same Use this

 file * -ib | awk -F'[;/.]' '{print $1}' | sort -n | uniq -c

  6 application
  6 image
 45 inode
 40 text
  2 video

Change the {print $1} part according to your need to get the appropriate output.

Upvotes: 3

Andy Ross
Andy Ross

Reputation: 12033

find . -type f | xargs -n1 basename | fgrep . | sed 's/.*\.//' | sort | uniq -c | sort -n

That gives you a recursive list of file extensions. If you want only the current directory add a -maxdepth 1 to the find command.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

You need bash.

files=(*)
pdfs=(*.pdf)

echo "${#files[@]}"
echo "${#pdfs[@]}"
echo "$((${#files[@]}-${#pdfs[@]}))"

Upvotes: 0

Related Questions