Wednesday, August 09, 2017

Getting Windows and Cygwin to Behave and then Using du for Disk Space Accounting

One use of Cygwin Windows can't match (I mean a unix feature Redmond hasn't heard of) is du

Your disk space is running low and of course Windows gives you accounting info - accurate but useless info - it doesn't tell you who the culprits are..

Unix - via Cygwin can help

But...

user@host /cygdrive/c
$ echo  `ls -d */ | grep ocumen | perl -p -e 's%/.*$%%;  s/\h/\134 /g;'`
Documents\ and\ Settings

user@host /cygdrive/c
$ du -ks  `ls -d */ | grep ocumen | perl -p -e 's%/.*$%%;  s/\h/\134 /g;'`
du: cannot access 'Documents\': No such file or directory
du: cannot access 'and\': No such file or directory
du: cannot access 'Settings': No such file or directory

Smell a rat? It's been bugging me for too long..

The ultimate goal is to be able to do $ for dir in `asdfdas` ; do ; du -ks $dir ; done ... But, it's turning out to be harder than I thought..

Here's the other problem. You do

$ for dir in `ls -d */` ; do ; echo $dir ; done
you get

Documents
and
Settings

which will make du curse you. Here's my workaround : 

for dir in `ls -1 -d */ | perl -p -e 's@/.*@@; s/\h/\%20/g; '`
do
tdir=`echo $dir | perl -p -e 's/%20/ /g;'`
du -ks "$tdir"
done

You're first giving bash's for loop a list with spaces in filenames substituted with %20. This is just so the for loop behaves (yes, you can mess with the IFS variable)
Then, you create a variable with the filename restored.
Then you call du on this new variable

No comments: