Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to monitor the rm command using perl, So that I can get to know what are all the files has been get deleted. give me some direction

Replies are listed 'Best First'.
Re: monitor rm command ?
by tirwhan (Abbot) on Sep 02, 2009 at 10:22 UTC

    man rm and perldoc perlop should answer that question perfectly (search for "backticks" on the latter). Update: Though since you're using Perl you might as well look at perldoc -f unlink and do without useless shelling.


    All dogma is stupid.
Re: monitor rm command ?
by jettero (Monsignor) on Sep 02, 2009 at 10:25 UTC
    In the long run, you'll be better off using something like this, otherwise, do what tirwhan said.
    unlink @files or warn "wtf: $!";

    -Paul

      Your example wouldn't show which files are deleted. For this, you would need something like

      for @files { unlink and push @deleted,$_ }

      -- 
      Ronald Fischer <ynnor@mm.st>
        Yours doesn't really show errors either...
        for(@files) { if( unlink ) { push @deleted, $_; } else { warn "$_ doesn't want to die: $!" } }

        -Paul

Re: monitor rm command ?
by JavaFan (Canon) on Sep 02, 2009 at 12:50 UTC
    You want to monitor *other* people using the rm command? You can't unless you're root, then you can replace 'rm' with any wrapper you like. Of course, there are still other ways to remove files.

    Depending on the OS and its security settings, you may be able to trap the unlink system call. But that will only monitor removal of the file name. File content can still be overwritten. And files may be renamed. To protect against that, you'd need to trap even more system calls (open/creat, write, rename, f?truncate to name a few of the obvious ones).

Re: monitor rm command ?
by leocharre (Priest) on Sep 02, 2009 at 15:23 UTC
    You could do something incredibly dangerous such as replace the rm command.. say.. move it from /bin/rm to /bin/rm.original .. and then write your own rm..
    #!/bin/sh echo "rm $@" >> /tmp/rm.log /bin/rm.original $@

    If you did this in my network I would take you outside and beat you.

      If you did this in my network I would take you outside and beat you.
      If you did this in my network I would find out how you did it, then take you outside and beat you.

      ;D

Re: monitor rm command ?
by vitoco (Hermit) on Sep 02, 2009 at 13:23 UTC

    Guessing that you don't know which files exactly are, because you are using wildcards or -rf options, you can take a snapshot of the directory tree before the rm, starting from the working directory, and compare it with another one taken when deletion is complete.

    You can use standard commands from the shell like find and sort, or, of course, use perl.

        Or something a little more cross-platform like SGI::FAM, though it might need a little TLC to make properly.
Re: monitor rm command ?
by ambrus (Abbot) on Sep 03, 2009 at 10:57 UTC

    Try the -v switch of the rm command, or even the -i switch if you want to be prompted before every file.