http://www.perlmonks.org?node_id=978830


in reply to How to let perl knows if somebody copies a file

I don't know what nautilus is, but for bash, create an alias called cp that calls a Perl script which calls the real cp and does whatever else you want it to do.

Replies are listed 'Best First'.
Re^2: How to let perl knows if somebody copies a file
by rovf (Priest) on Jun 28, 2012 at 10:47 UTC

    This works, but only under very restrictive circumstances. First, the users have to include the alias (or shell function) in their .bashrc; secondly, it would not work in the scripts invoked by the users (unless they are bash scripts and also the bash script also reads .bashrc, not a very common situation). Finally, it works reliably only if the user consistently uses cp to copy the file, and not some other command (such as cat or some Perl program using File::Copy). So, in the end, it depends very much on the cooperation of the users....

    -- 
    Ronald Fischer <ynnor@mm.st>

      First, the users have to include the alias (or shell function) in their .bashrc;

      Ah, I thought there was a file like /etc/profile for per-interactive-shell startup.

      Finally, it works reliably only if the user consistently uses cp to copy the file

      Which is what the OP asked.

        I thought there was a file like /etc/profile for per-interactive-shell startup.
        Using /etc/profile for type of work has the drawback, that this file is not sourced always, but only if the bash is invoked as login-shell, and the --noprofile option is not used.

        Which is what the OP asked.
        Not sure of this. The OP said f somebody uses the copy command in bash, which is different from saying if somebody uses the command 'cp' from the command line. For example, the user might invoke a shell script, which contains a command to copy the file (maybe cp, maybe a different one), and this would then not necessarily be monitored.

        Honestly, I don't know the deeper purpose for the OP's request. If he just want to spare his users the task to explicitly run his Perl scripts after each copying process, he could simply provide a script for doing copying+inspecting, and ask the users to run his script instead of using the original cp commmand. Of course, any user is then free to put an alias to this script in his or her .bashrc, and this would work in a similar way for ksh or zsh too.

        On the other hand, if the OP wants to enforce this type of check, because he suspects that the users, on purpose, might not use his script, all alternatives discussed here are very easy to bypass.
        -- 
        Ronald Fischer <ynnor@mm.st>