Development > Scripting and Bash

Windows-like file size info

(1/1)

anon222:
First let's make it clear
    "1 KB" means 1024 bytes (as Windows would report it, traditional usage)
    "1 KB" means 1000 bytes (as Linux would report it, IEC usage)
    "1 KiB" means 1024 bytes (unambiguous, but perhaps unfamiliar terminology, IEC usage)
This is why, in Windows:
1 Terabyte hard drive is only 932 Gibabytes
CD filesize is 700 MB
and DVD isn't     4.7 GB

This script displays file sizes as Windows would.
You can select multiple files and with a right-click and call the Thunar Custom Action which is going to call this script.
It will not work with Directories.

Directions are in the script.
command:

--- Code: --->/tmp/1024conv; >/tmp/1024convR;for file in %N; do echo "$file" >> /tmp/1024conv ;done; /usr/bin/1024convert  %N
--- End code ---

--- Code: ---#!/bin/bash
# Thunar custom action to calculate and display file sizes in Kib, MiB and Gib in a zenity list dialog
# Command for the thunar custom action:
# >/tmp/1024conv; >/tmp/1024convR;for file in %N; do echo "$file" >> /tmp/1024conv ;done; /usr/bin/1024convert  %N
# This file should be saved in /usr/bin and named 1024convert
# and made executable (chmod +x)
# Mark all file types except directories
# File pattern *
# Cheers from Misko

# Calculate File Sizes in Kib, MiB and Gib and write the results in /tmp/1024convR
stat -c%s "$@" |
awk '{
sum=$1;

hum[1024**3]="GiB";
hum[1024**2]="MiB";
hum[1024]="KiB";

for (x=1024**3; x>=1024; x/=1024)
{
if (sum>=x)
{
  printf "%.2f %s\n",sum/x,hum[x];
break
}
if (sum<1024)
{
printf sum" bytes\n";
break
}
}
}' > /tmp/1024convR


# store the file list
list=/tmp/1024list

# Merge the results from two files and display the result
paste -d'\n' /tmp/1024conv /tmp/1024convR > $list

# clear our array
LIST_Z=()

# read in the array using a line tmp variable. This let's zenity see it line by line
while read -r line
do
    LIST_Z+=("$line")
done < /tmp/1024list

zenity --list --column=Name --column=Size "${LIST_Z[@]}" --width=800 --height=700 --title="1024 byte conversion" --text="File size displayed in the IEC standard"
# Remove temporary files
rm /tmp/1024list
rm /tmp/1024conv
rm /tmp/1024convR
--- End code ---

Windows-like file size info... Yeah, I know :)
Spent my creativity on the scipt.

Navigation

[0] Message Index

Go to full version