- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 157字
- 2021-07-09 19:46:32
How to do it...
To enumerate file type statistics, follow these steps:
- To print the type of a file, use the following command:
$ file filename $ file /etc/passwd /etc/passwd: ASCII text
- Print the file type without the filename:
$ file -b filename ASCII text
- The script for file statistics is as follows:
#!/bin/bash # Filename: filestat.sh if [ $# -ne 1 ]; then echo "Usage is $0 basepath"; exit fi path=$1 declare -A statarray; while read line; do ftype=`file -b "$line" | cut -d, -f1` let statarray["$ftype"]++; done < (find $path -type f -print) echo ============ File types and counts ============= for ftype in "${!statarray[@]}"; do echo $ftype : ${statarray["$ftype"]} done
The usage is as follows:
$ ./filestat.sh /home/slynux/temp
- A sample output is shown as follows:
$ ./filetype.sh /home/slynux/programs ============ File types and counts ============= Vim swap file : 1 ELF 32-bit LSB executable : 6 ASCII text : 2 ASCII C program text : 10