Mala alatka za analizu sadržaja usb flash memorija i diskova

Misko_2083

Member
Joined
Mar 15, 2015
Messages
306
Reaction score
18
Mali zenity GUI koji sam napravio. Može da pokaže koje se sve vrste fajlova nalaze na USB Flash memorijama i diskovima.
U suštini, i nije baš nešto preterano koristan skript. 😃

Code:
#!/bin/bash
[HEADING=1]A tool to analyze the usb drive content[/HEADING]
[HEADING=1]Milos Pavlovic, 2015[/HEADING]
[HEADING=1]This program is free software; you can redistribute it and/or modify it[/HEADING]
[HEADING=1]under the terms of the GNU General Public License as published by[/HEADING]
[HEADING=1]the Free Software Foundation; either version 2 of the License, or[/HEADING]
[HEADING=1](at your option) any later version.[/HEADING]
[HEADING=1]This program is distributed in the hope that it will be useful,[/HEADING]
[HEADING=1]but WITHOUT ANY WARRANTY; without even the implied warranty of[/HEADING]
[HEADING=1]MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the[/HEADING]
[HEADING=1]GNU General Public License for more details.[/HEADING]
[HEADING=1]You should have received a copy of the GNU General Public License[/HEADING]
[HEADING=1]along with this program; if not, write to the Free Software[/HEADING]
[HEADING=1]3 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,[/HEADING]
[HEADING=1]MA 02110-1301, USA.[/HEADING]
[HEADING=1]Make temporary file[/HEADING]
list=$(mktemp /tmp/XXXXXX)
[HEADING=1]get a list of devices[/HEADING]
devs=[ICODE]ls -al /dev/disk/by-path/*usb*part* 2>/dev/null | awk '{print($11)}'[/ICODE]
if [[ ${?} != 0 || -z ${devs} ]]
then
zenity --warning --text “No USB drive plugged in!”
rm $list
exit
fi
[HEADING=1]Initialize list and make sure it is empty[/HEADING]
[QUOTE]
$list
[/QUOTE]
[HEADING=1]Now get the info about our devices and put it in a list[/HEADING]
for dev in $devs; do dev="${dev##*/}";
[HEADING=1]Check if device is mounted[/HEADING]
[HEADING=1]This way unmounted devices will not be in the list[/HEADING]
grep -qs “/dev/${dev}” /proc/mounts
if [[ ${?} = 0 ]]
then
echo “FALSE” >> $list;
echo “/dev/$dev” >> $list;
echo [ICODE]cat /sys/block/${dev:0:3}/device/vendor 2>/dev/null[/ICODE] >> $list;
echo [ICODE]cat /sys/block/${dev:0:3}/device/model 2>/dev/null[/ICODE] >> $list;
echo [ICODE]lsblk 2>/dev/null | grep \[/ICODE]echo -E ${dev}` | awk ‘{print($4)}’ [ICODE]B >> $list; echo [/ICODE]lsblk 2>/dev/null | grep `echo -E ${dev}` | cut -c 37- [ICODE]>> $list; echo -n[/ICODE]lsblk -o NAME 2>/dev/null | grep `echo -E ${dev}`| awk ‘{print($2)}’ [ICODE]>> $list; df -T[/ICODE]echo -E /dev/${dev}[ICODE]2>/dev/null | tail -1 | awk '{print $2}' >> $list; echo[/ICODE]ls -l /dev/disk/by-uuid/ 2>/dev/null | grep `echo -E ${dev}` | awk ‘{print($9)}’ ` >> $list;
fi
done
[HEADING=1]Check if the list is empty[/HEADING]
if [[ ! -s ${list} ]]
then
zenity --warning --text=“No USB drive mounted!” --timeout=10
rm $list
exit 0
fi
[HEADING=1]clear array[/HEADING]
devfs=()
[HEADING=1]Read in the array using a line tmp variable[/HEADING]
while read -r line
do
devfs+=("$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.\nOnly mounted usb drives are shown here.\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[@]}”)

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

#Check if anything was selected
echo ${disktmp} | grep ^[/] &>/dev/null
if [[ ${?} != 0 ]]
then
zenity --info --text=“Nothing was selected!\nStart again and select a USB drive.” --timeout=10
exit 0
fi
[HEADING=1]Extract device[/HEADING]
disk=${disktmp:5:4}
[HEADING=1]check if device is on the mount list again, just to be sure[/HEADING]
grep -qs “/dev/${disk}” /proc/mounts
if [[ ${?} != 0 ]]
then
zenity --info --text=“Device /dev/$disk is not mounted!” --timeout=10
exit 0
fi
[HEADING=1]Start finding file types and file sizes and convert bytes to human readable[/HEADING]
{
echo “#Please wait, this may take a while\nDepending on the drive content”
find “$(echo [ICODE]lsblk 2>/dev/null | grep \[/ICODE]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[10243]=“GiB”;hum[10242]=“MiB”;hum[1024]=“KiB”; for (x=10243; 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[10003]=“GB”;hum[10002]=“MB”;hum[1000]=“KB”; for (x=10003; 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
[HEADING=1]Display results[/HEADING]
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 ]]
then
rm ~/usbinform
rm ~/usbinform1
rm ~/usbinform2
exit 0
fi

#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 ]]
then
rm ~/usbinform
rm ~/usbinform1
rm ~/usbinform2
exit 0
fi

#save
paste -d’\t’ ~/usbinform2 ~/usbinform1 ~/usbinform > “$zSavePath”
[HEADING=1]Remove temp files[/HEADING]
rm ~/usbinform
rm ~/usbinform1
rm ~/usbinform2
exit
 
Last edited:
Top