You are not logged in.

#1 2011-01-27 04:11:33

Lite
Member
Registered: 2009-07-09
Posts: 6

Bash script and wildcards

At my computer job, we scan most customers' hard disk drives for viruses before we work on the computers. Since the virus scanning machines dual boot between Linux and Windows I decided to use my spare time to write a bash script that would delete leftover and temp files beforehand to cut down on scanning time. The case-sensitivity/insensitivity has left me with a twisted mesh of wildcards, but I've tested it on a dozen machines and at least it has done what I expected of it. Has anyone (assuming more gracefully) dealt with something like this before?

#!/bin/bash
## Version - Jan. 26th 2011
## This script will automatically delete Windows temporary files.
## Run this script from the root directory of the Windows partition.
BLUE="\033[0;36m"
RED="\033[0;31m"
GREEN="\E[32m"
ENDCOLOR="\033[0m"

echo -e $BLUE"Drive space (MB):"
df -B 1M .

echo -e $RED"Emptying Recycle Bins..."
rm -rf RECYCLE*/* > /dev/null 2>&1
rm -rf ?ecycle*/* > /dev/null 2>&1
rm -rf *RECYCLE*.* > /dev/null 2>&1
rm -rf .Trash-100* > /dev/null 2>&1

echo -e "Deleting Windows temps..."$ENDCOLOR
rm -rf *ystem\ ?olume\ ?nformation/ > /dev/null 2>&1
rm -rf SYSTEM\ VOLUME\ INFORMATION/ > /dev/null 2>&1
rm -rf ?????*/TEMP*/* > /dev/null 2>&1
rm -rf ?????*/Temp*/* > /dev/null 2>&1
rm -rf ?????*/temp*/* > /dev/null 2>&1
rm -rf ?????*/MEMORY.DMP
rm -rf WUTemp > /dev/null 2>&1
rm -rf WUTEMP > /dev/null 2>&1
rm -rf found.??? > /dev/null 2>&1
rm -rf FOUND.??? > /dev/null 2>&1
rm -rf ?rogram?ata/?icrosoft/?indows/WER/?eport*/* > /dev/null 2>&1
rm -rf ???????/\$NtUninstallKB* > /dev/null 2>&1
rm -rf ???????/*.log > /dev/null 2>&1
rm -rf ???????/*.LOG > /dev/null 2>&1
rm -rf ?esktop.ini > /dev/null 2>&1
rm -rf */?esktop.ini > /dev/null 2>&1
rm -rf */*/?esktop.ini > /dev/null 2>&1
rm -rf \$AVG > /dev/null 2>&1
rm -rf \$AVG8.VAULT\$ > /dev/null 2>&1
## Yes, I actually do run into customers who store
## documents and pictures in the root temp folder
mv -vn ?emp/*.doc . > /dev/null 2>&1
mv -vn ?emp/*.docx . > /dev/null 2>&1
mv -vn ?emp/*.odt . > /dev/null 2>&1
mv -vn ?emp/*.xls . > /dev/null 2>&1
mv -vn ?emp/*.jpg . > /dev/null 2>&1
mv -vn TEMP/*.doc . > /dev/null 2>&1
mv -vn TEMP/*.docx . > /dev/null 2>&1
mv -vn TEMP/*.odt . > /dev/null 2>&1
mv -vn TEMP/*.xls . > /dev/null 2>&1
mv -vn TEMP/*.jpg . > /dev/null 2>&1
rm -rf temp > /dev/null 2>&1
rm -rf Temp > /dev/null 2>&1
rm -rf TEMP > /dev/null 2>&1

echo -e $RED"Deleting user temps..."$ENDCOLOR
rm -rf ?sers/*/?pp?ata/?ocal/?emp/* > /dev/null 2>&1
rm -rf ?sers/*/?pp?ata/?ocal/TEMP/* > /dev/null 2>&1
rm -rf ?sers/*/?pp?ata/?ocal/?icrosoft/?indows/WER/Report*/* > /dev/null 2>&1
rm -rf DOCUMENTS\ AND\ SETTINGS/*/?ocal\ ?ettings/?emporary\ ?nternet\ ?iles/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?pp?ata/?oaming/?icrosoft/?indows/IE*/index.dat > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?pp?ata/?oaming/?icrosoft/?indows/?ookies/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?esktop.ini > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?emp/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/TEMP/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?emporary\ ?nternet\ ?iles/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?esktop.ini > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?pplication\ ?ata/?esktop.ini > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?icrosoft/?indows/?emporary\ ?nternet\ ?iles/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?ozilla/?ire?ox/?rofiles/*/?ache/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?ozilla/?ire?ox/?rofiles/*/*.*/?ache*/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?oogle/?hrome/*/?efault/?ache/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?pp?ata/?ocal/?oogle/?hrome/*/?efault/?ache/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?ocal\ ?ettings/?pplication\ ?ata/?un/?ava/?eployment/?ache/* > /dev/null 2>&1
rm -rf ?ocuments\ ?nd\ ?ettings/*/?pplication\ ?ata/?un/?ava/?eployment/?ache/* > /dev/null 2>&1

echo -e $GREEN"Done."$ENDCOLOR
echo -e "Current drive space:"
df -B 1M .

Offline

#2 2011-01-27 07:37:10

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Bash script and wildcards

http://www.tuxera.com/community/ntfs-3g-faq/ wrote:

When mounting with the lowntfs-3g variant, you can get case insensitive access to files by using the option ignore_case. Doing so, all files will be shown with lower case letters in directory listings.
Alternately, to get case insensitivity handling and/or restriction on special characters, you may export the NTFS volume via Samba which supports this functionality the same way as it does for other POSIX file systems. Another solution for case insensitivity support is using ciopfs which mounts a normal directory on a regular file system in case insensitive fashion.

Assuming the drives are NTFS, perhaps one of those options could work for you?

If I had to write the script, I'd utilize `find` more. For example:

echo -e $RED"Emptying Recycle Bins..."
find ./ -iname '?ecycle' -type d | xargs rm -Rf

Edit: Spelling.

Last edited by fukawi2 (2011-01-27 07:37:48)

Offline

#3 2011-01-27 10:37:11

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Bash script and wildcards

Try this:

$ shopt -s nocaseglob
$ echo /VA*
/var

Offline

#4 2011-01-27 23:01:23

Lite
Member
Registered: 2009-07-09
Posts: 6

Re: Bash script and wildcards

Thanks for the replies. I've been waiting for a slow day like today and yesterday to write some time-saving scripts.
I wanted to use find, but I am not familiar enough (yet) to use it effectively. I didn't even know about shopt. I'll definitely see what I can do to cut down on length and clutter.

Last edited by Lite (2011-01-27 23:02:05)

Offline

#5 2011-01-31 20:09:02

Lite
Member
Registered: 2009-07-09
Posts: 6

Re: Bash script and wildcards

Just in case anyone was interested in what I came up with, I managed to clean it up a bit while keeping things simple. I've tested it on several drives now with no problem, but I'm sure I'll tweak it again in the future now and then.

#!/bin/bash
## Version - Jan. 28th 2011
## This script will automatically delete Windows temporary files.
## Run this script from the root directory of the Windows partition.
BLUE="\033[0;36m"
RED="\033[0;31m"
GREEN="\E[32m"
ENDCOLOR="\033[0m"

shopt -s nocaseglob
echo -e $BLUE"Drive space (MB):"
df -B 1M .

echo -e $RED"Emptying Recycle Bins..."
rm -rf recycle*/* > /dev/null 2>&1
rm -rf *recycle*.* > /dev/null 2>&1
rm -rf .trash-100* > /dev/null 2>&1

echo -e "Deleting Windows temps..."
rm -rf system\ volume\ information* > /dev/null 2>&1
rm -rf {windows,winnt}*/temp*/* > /dev/null 2>&1
rm -rf {windows,winnt}*/{minidump*/*.dmp,memory.dmp*} > /dev/null 2>&1
rm -rf wutemp* > /dev/null 2>&1
rm -rf found.??* > /dev/null 2>&1
rm -rf programdata*/microsoft*/windows*/wer*/report*/* > /dev/null 2>&1
rm -rf windows*/\$ntuninstallkb* > /dev/null 2>&1
rm -rf windows*/*.log > /dev/null 2>&1
rm -rf {?,*/?,*/*/?}esktop.ini* > /dev/null 2>&1
rm -rf {\$AVG,\$AVG8.VAULT\$} > /dev/null 2>&1
mv -vn temp*/{*.doc,*.docx,*.odt,*.xls,*.jpg} . > /dev/null 2>&1
rm -rf temp* > /dev/null 2>&1

echo -e "Deleting user temps..."$ENDCOLOR
rm -rf users*/*/appdata*/local*/temp*/* > /dev/null 2>&1
rm -rf users*/*/appdata*/local*/microsoft*/windows*/wer*/report*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/appdata*/roaming*/microsoft*/windows*/{IE*/index.dat*,cookies*/*} > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/appdata*/roaming*/microsoft*/windows*/cookies*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/temp*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/microsoft*/windows*/temporary\ internet\ files*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/{application\ data,local\ settings}*/desktop.ini* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/desktop.ini* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/mozilla*/firefox*/profiles*/*/{?ache*,*.*/?ache*}/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/google*/chrome*/*/?efault/?ache/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/appdata*/local*/google*/chrome*/*/default*/cache*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/sun*/java*/deployment*/cache*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/application\ data*/sun*/java*/deployment*/cache*/* > /dev/null 2>&1
rm -rf documents\ and\ settings*/*/local\ settings*/application\ data*/apple\ computer*/quicktime*/downloads*/* > /dev/null 2>&1

shopt -u nocaseglob
echo -e $GREEN"Done."$ENDCOLOR
echo -e "Current drive space:"
df -B 1M .

Offline

#6 2011-01-31 23:16:52

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,231
Website

Re: Bash script and wildcards

Procyon wrote:

$ shopt -s nocaseglob

That's even better, nice smile

Offline

#7 2011-02-01 05:27:42

rowdog
Member
From: East Texas
Registered: 2009-08-19
Posts: 118

Re: Bash script and wildcards

fukawi2 wrote:
echo -e $RED"Emptying Recycle Bins..."
find ./ -iname '?ecycle' -type d | xargs rm -Rf

Since we're in the land of directory names with spaces, I would change that to

find ./ -iname '?ecycle*' -type d -print0 | xargs -0 rm -Rf

I wouldn't bother to rewrite the (now working) script but yeah, find is an insanely powerful tool and a good choice for this task.

Offline

Board footer

Powered by FluxBB