[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Cleaning out old backup (*~, *.bak) files



On Sat, Dec 24, 2005 at 01:52:44AM -0600, a.list.address@gmail.com wrote:
} I have a lot of *~ and *.bak files in my home directories, including
} recent ones, and ones from several years ago.  I'd like to set up a
} good system that gets rid of old ones--say, older than a month.  I also
} would like them to go to the KDE trash bin.
} 
} It turns out that making a good script to do this is not as simple as
} it sounds (well, not for me, anyway; I'm new to Bash scripting).  You
} can't just mv the files; if you do, they won't show up in the KDE
} "trash:/" interface.  You can use kfmclient to move them (like
} 'kfmclient move foo trash:/'), but that results in a progress box for
} each file, and runs at about one file per second.  I ran across a
} script by boredhacker (http://dot.kde.org/1105642626/1105722224/,
} scroll down near the bottom) that looks like a good start, but it's
} taking me some time to work it into a usable system.  I also found
} another thread
} (http://lists.kde.org/?l=kde-devel&m=110513719006887&w=2), but I
} haven't done any more searching to see if that guy's program is
} available anywhere.  There's also a decent-looking (although rather
} Windowsish) program called KleanSweep (on KDE-Apps), but it has a bug
} that prevents it from working at all (on my system, anyway).
} 
} Anyway, I'm posting to see what other Debianites do to clean out their
} old, unneeded files; what kinds of scripts, etc.  Any general
} suggestions?

It sounds like your main problem is with KDE rather than with finding and
getting rid of the files. I suspect that the issue is that you are calling
kfmclient for each file instead of for batches of files. This is the
problem with find -exec. It is also a problem with xargs since even though
kfmclient can take a list of files to move it needs an argument after the
file list (i.e. "trash:/"). A little experimentation on my system shows
that kfmclient moves a long list of files almost as quickly as a single
file. The solution is to put a small shell script named movetotrash in your
path that looks like this:

#!/bin/sh

exec kfmclient move "$@" trash:/

...and run the following find/xargs command:

find $HOME -L -daystart -mtime +30 -type f -uid $UID \
	'(' -name '*.bak' -o -name '*~' ')' -print0 | xargs -0 movetotrash

Note that this can cause issues with filenames that might have weird
characters (i.e. things that the shell might expand). An even better
solution is to replace the movetotrash shell script with an executable:

#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
  char arg0[] = "kfmclient";
  char **newargv = (char **)malloc(sizeof(char *) * (argc+3));
  int i;

  newargv[0] = arg0;
  newargv[1] = "move";
  newargv[argc+1] = "trash:/";
  newargv[argc+2] = 0;

  for (i=1;i<argc;++i) {
    int arglen = strlen(argv[i])+1;

    newargv[i+1] = (char *)malloc(sizeof(char) * arglen);
    strncpy(newargv[i+1], argv[i], arglen);
  }
  execvp(arg0, newargv);
}

I suspect that Perl/Python/Ruby could do what the above C program does in a
single line, actually, but I don't care for Perl or Python and I don't yet
know Ruby well enough.

--Greg



Reply to: