vman
vman

Reputation: 321

How to track file creation and modification

We have put together a perl script that essentially looks at the argument that is being passed to it checks if is creating or modifying a file then it saves that in a mysql database so that it is easily accessible later. Here is the interesting part, how do I make this perl script run before all of the commands typed in the terminal. I need to make this dummy proof so people don't forget to run it.

Sorry I didn't formulate this question properly. What I want to do is prepend to each command such that each command will run like so "./run.pl ls" for example. That way I can track file changes if the command is mv or it creates an out file for example. The script pretty much takes care of that but I just don't know how to run it seamlessly to the user.

I am running ubuntu server with the bash terminal.

Thanks

Upvotes: 0

Views: 327

Answers (2)

arash kordi
arash kordi

Reputation: 2558

If I understood correctly you need to execute a function before running every command, something similar to preexec and precmd in zsh.

Unfortunately bash doesn't have a native support for this but you can do it using DEBUG trap.

Here is a sample code applying this method.

This page also provide some useful information.

Upvotes: 3

amon
amon

Reputation: 57600

You can modify the ~/.bashrc file and launch your script there. Do note that each user would (and should) still have the privelege to modify this file, potentially removing the script invocation.

The /etc/bash.bashrc file is system-wide and only changeable by root.

These .bashrcs are executed when a new instance of bash is created (e.g. new terminal).

It is not the same as sh, the system shell, that is dash on Ubuntu systems.

Upvotes: 1

Related Questions