VSIDO Community

VSIDO Support => Scripts and How To's => Zenity & Yad => Topic started by: misko_2083 on July 24, 2015, 07:38:09 PM

Title: A little GUI tool to analyze the content of usb drives
Post by: misko_2083 on July 24, 2015, 07:38:09 PM
This will display all of your mounted usb drives, then you select one.
The script will analyze the contents of the drive and display info about file types and size that file type takes on a drive.
I took some code from "makethumb" script and modified it for my needs.
As you can see it tell me how much space are images taking on this usb drive.
Code: [Select]
# A GUI tool to analyze the usb drive content
# Misko
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# 3 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

# Make temporary file
list=$(mktemp /tmp/XXXXXX)

# get a list of devices
devs=`ls -al /dev/disk/by-path/*usb*part* 2>/dev/null | awk '{print($11)}'`
if [[ ${?} != 0 || -z ${devs} ]]
    zenity --warning --text "No USB Mounted!"

# Initialize list and make sure it is empty

# Now get the info about our devices and put it in a list
for dev in $devs; do dev="${dev##*\/}";
    echo "FALSE" >> $list;
    echo -n "$dev" >> $list;
    echo " " >> $list;
    echo `cat /sys/block/${dev:0:3}/device/vendor 2>/dev/null` >> $list;
    echo `cat /sys/block/${dev:0:3}/device/model 2>/dev/null` >> $list;
    echo `lsblk 2>/dev/null | grep \`echo -E ${dev}\` |awk '{print($4)}' `B >> $list;
    echo `lsblk 2>/dev/null | grep \`echo -E ${dev}\` |cut -c 37- ` >> $list;
    echo -n `lsblk -o NAME 2>/dev/null | grep \`echo -E ${dev}\`| awk '{print($2)}'  ` >> $list;
    df -T `echo -E /dev/${dev}` 2>/dev/null | tail -1 | awk '{print $2}' >> $list;
    echo `ls -l /dev/disk/by-uuid/ 2>/dev/null | grep \`echo -E ${dev}\`| awk '{print($9)}' ` >> $list;

# clear our array

# read in the array using a line tmp variable. This let's zenity see it line by line
while read -r line
done < $list

rm $list
unset list

#Display the main dialog
disktmp=$(zenity --list --text="Select your USB drive from the list and click OK to begin.\nThis will calculate what file types you have on the drive and how much space do they consume.\nThe speed of this process depends on the drive size and on the number of files." --radiolist  --width=650 --height=350 --column="Pick" --column="Dev" --column="Vendor" --column="Model" --column="Size" --column="Mount" --column="FS" --column="UUID" "${devfs[@]}" --separator=":")

#Test for cancellation
if [[ ${?} != 0  ]]
    exit 0

#Check if anything was selected
echo ${disktmp} | grep ^[0-9a-zA-Z] &>/dev/null
if [[ ${?} != 0  ]]
    zenity --info --text="Nothing was selected..." --timeout=10
    exit 0
# Extract device

# Start calculating file sizes and convert bytes to human readable
echo "#Please wait, this may take a while"
find "$(echo `lsblk 2>/dev/null | grep \`echo -E ${disk}\` |cut -c 37- `)" -type f -exec file -b '{}' \; -printf '%s\n' | awk -F , 'NR%2 {i=$1} NR%2==0 {a[i]+=$1} END {for (i in a) printf("%12u %s\n",a[i],i)}' | sed -e 's/^[ \t]*//' > ~/usbinform
echo "#Calculating File size"
awk '{print $1}' ~/usbinform | 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 } }}' > ~/usbinform1
awk '{print $1}' ~/usbinform | awk '{ sum=$1 ; hum[1000**3]="GB";hum[1000**2]="MB";hum[1000]="KB"; for (x=1000**3; x>=1000; x/=1000){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } if (sum<1000) {printf sum" bytes\n";break } }}' > ~/usbinform2
sed -i 's/\w*.//' ~/usbinform
} | zenity --progress --pulsate --auto-close || exit

# Display results
paste -d'\n' ~/usbinform2 ~/usbinform1 ~/usbinform | zenity --list --title="${disk}" --column="Size" --column="Size KiB" --column="Description" --width=800 --height=650 --ok-label="Save" --cancel-label="OK" --text="Drive ${disk} Storage Summary\n1KB=1000 bytes, 1KiB=1024 bytes"

#Test for cancellation
if [[ ${?} != 0 ]]
    rm ~/usbinform
    rm ~/usbinform1
    rm ~/usbinform2
       exit 0

#Ask where to save our file
zNewData=$(paste -d'\t' ~/usbinform2 ~/usbinform1 ~/usbinform)
zSavePath=$(echo -n "$(zenity --file-selection --save --confirm-overwrite --filename="USBdrive_summary.txt" --title="Save USB drive info")")

#Test for cancellation
if [[ ${?} != 0 ]]
    rm ~/usbinform
    rm ~/usbinform1
    rm ~/usbinform2
       exit 0

echo -n "$zNewData" > "$zSavePath"

# Remove temp files
rm ~/usbinform
rm ~/usbinform1
rm ~/usbinform2
Edit: lol a small error fixed not 38 it's 37   ::)