Space in the directory name

I've got a small script that deletes all the trash from mailusers Trash directory. I run this script once in a while to make some space. (We have lots of IMAP users, who keep their mail on server!) Occansionaly, the users create directories with space in the name (e.g. "My Mail"). And sometimes the users will delete the whole directory. Is there a way to grep the whole name of such occurances. Here is the script I made:
echo "Enter volume name to fetch Trash:"
read volname
cd /usr/$volname
du -k |grep Trash | sort +0nr |head -40 >/tmp/t_$volname.dat
cat /tmp/t_$volname.dat |awk '{print "rm -ef " $2 "/200*"}' >/tmp/tr$volname.del

I think the problem starts with grep. It should get the entire directory name. Any help will be greatly appreciated.
Nitin :smiley:

I looked into my script and saw that "grep" is doing fine a job by capturing the names of directories with space. The awk is not transferring the names correctly. It cuts the directory field until it encounters a space. I am thinking of doing some kind of substitution. Can anyone help me now?
Thanks in advance,
Nitin :smiley:

my guess in your awk statement you are only pasing $1 if you also pass $2 you will get the 2nd word if the dir. either that or you can change the field seperator.

OR

use $1 for the most part and tos an if -d to see if your awk vaiable is valid if yes then delte if no add $2 rerun directory check.

You might try changing your field separator
environment variable to somthing like...
export FS=":"

By default, the field separator is whitespace.
I haven't actually tried this with awk but I
believe awk will assume $1 = "My" and $2 = "Mail"
using whatever the field separator is.
If you have names with ":" in them then try
a different character.

Well, I have to say that there may be a more practical way of doing what you want... I mean, why not just have something which prunes away files that are over say 30-days old?...

Something like:

#!/usr/bin/perl

$volume=$ARGV[0];
@dirs=`find /usr/$volume -name Trash`;
$curtime=time();
$number_days=30;
foreach(@dirs)
{
  chomp();
  opendir(DIR,$_);
  while($fname=readdir(DIR))
  {
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)=stat($fname};
    # 60 seconds, 60 minutes, 24 hours
    if($ctime < $curtime-(60*60*24*$number_days))
    {
      unlink $fname;
    }
  }
}

NOTE: You will want to test this because I haven't (!!!!!).... It's just an idea...

  • dEvNuL

added code tags for readability --oombera

I had thought of deleting all the files older than a week. But, Netscape (mail server) creates some control files (e.g. __GENNAME__, __lock__, etc.) in the Trash directory of users. If removed users won't be able to see the Folders at client end (and raise hell on me! :mad: ). I could look into your script and tell it not to delete the files starting with "__".
Thanks a lot though. :smiley: