Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1673  / 2 Years ago, sat, july 9, 2022, 1:22:58

With this part of script I can get a necessary information about files in my directory(and subdirectories). The only information I need is the extension and the size of file.



for file in `find . -type f`; do
size=$(stat -c '%s' ${file})
file=$(echo "${file}" | awk -F/ '{print $NF}')
ext=$(echo "${file}" | grep '..*.' | awk -F. '{print $NF}' | grep '[A-Za-z0-9]')
if [ -z ${ext} ]; then
echo "NOTE: no extention"
else
EXTS="${EXTS}${ext}${newLine}"


It is only part of the script. So my question is: How can I put this informarion into an array? I mean I want an array with elements to look like this:



 c/123 /12 h/90 /0 txt/0


where c, h and txt are file extension and 123, 12 and 0 are file sizes. So lately I can separately work with sizes and extensions

I hope, I've neatly put my question down. Sorry for mistakes. :)


More From » bash

 Answers
4

First, don't do for file in $(find …). That's very fragile.



Now, you can simplify your code a bit by getting find to print the filenames and sizes together, using -printf:



find . -type f -printf '%s.%f/'


Then, you can use awk to process this output to get cumulative sizes by extension. Note that I used . to separate the filename (%f) from the size (%s), and I added a / after that. Hence, I can use . as a field separator in awk. And since the only characters not allowed in filenames are / and the ASCII NUL, I can safely use / as the record separator.



So:



awk -F. -v RS=/ 'NF > 2 {size[$NF] += $1; next}
{size["/"] += $1}
END {for (i in size) {print i,"/",size[i]}'


Here, I'm using / as the index if there's no extension.



Combined:



$ find . -type f -printf '%s.%f/' | awk -F. -v RS=/ 'NF > 2 {size[$NF] += $1; next}
{size["/"] += $1}
END {for (i in size) {printf "%s/%d
", i, size[i]}}'
h/780
md/2509
tex/23961
c/13557
//5109
txt/2349291
sh/1166
py/12248


Now, if your extensions don't contain spaces, you could just do:



my_array=( $(find . -type f -printf '%s.%f/' | awk -F. -v RS=/ 'NF > 2 {size[$NF] += $1; next} {size["/"] += $1} END {for (i in size) {printf "%s/%d
", i, size[i]}}') )


Or, you can use process substitution and read each entry in:



my_arr=()
while IFS='' read -r entry
do
my_arr+=( "$entry" )
done < <(find . -type f -printf '%s.%f/' | awk -F. -v RS=/ 'NF > 2 {size[$NF] += $1; next} {size["/"] += $1} END {for (i in size) {printf "%s/%d
", i, size[i]}}')


As before:



$ printf "%s
" "${my_arr[@]}"
h/780
md/2509
tex/23961
c/13557
//5109
txt/2349291
sh/1166
py/12248

[#16070] Sunday, July 10, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
otputler

Total Points: 53
Total Questions: 105
Total Answers: 93

Location: Zimbabwe
Member since Wed, Nov 9, 2022
2 Years ago
;