3

I am new to bash scripting and I have to create a script that will run on all computers within my group at work (so it's not just checking one computer). We have a spreadsheet that keeps certain file information, and I am working to automate the updating of that spreadsheet. I already have an existing python script that gathers the information needed and writes to the spreadsheet.

What I need is a bash script (cron job, maybe?) that is activated anytime a user deletes a file that matches a certain extension within the specified file path. The script should hold on to the file name before it is completely deleted. I don't need any other information besides the name.

Does anyone have any suggestions for where I should begin with this? I've searched a bit but not found anything useful yet.

It would be something like:

 for folders and files in path:
      if file ends in .txt and is being deleted:
            save file name
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3938558
  • 63
  • 2
  • 8
  • 1
    if you're on linux, then look into inotify, which can trigger actions based on events in the filesystem. – Marc B Oct 10 '14 at 20:02

1 Answers1

2

To save the name of every file .txt deleted in some directory path or any of its subdirectories, run:

inotifywait -m -e delete --format "%w%f" -r "path" 2>stderr.log | grep '\.txt$' >>logfile

Explanation:

  • -m tells inotifywait to keep running. The default is to exit after the first event

  • -e delete tells inotifywait to only report on file delete events.

  • --format "%w%f" tells inotifywait to print only the name of the deleted file

  • path is the target directory to watch.

  • -r tells inotifywait to monitor subdirectories of path recursively.

  • 2>stderr.log tells the shell to save stderr output to a file named stderr.log. As long as things are working properly, you may ignore this file.

  • >>logfile tells the shell to redirect all output to the file logfile. If you leave this part off, output will be directed to stdout and you can watch in real time as files are deleted.

  • grep '\.txt$' limits the output to files with .txt extensions.

Mac OSX

Similar programs are available for OSX. See "Is there a command like “watch” or “inotifywait” on the Mac?".

Community
  • 1
  • 1
John1024
  • 109,961
  • 14
  • 137
  • 171
  • thank you! also, the explanation is very very helpful! as a new bash scripter, those one-liners are difficult to figure out. – user3938558 Oct 13 '14 at 15:51
  • I'm trying to figure out these keywords that seem to be used as extensions in several one-liners on bash (such as -name, -type, etc). I don't know where to look for a list of these, or if they are solely dependent on the current command. Could you point me in the right direction? Thank you!! – user3938558 Oct 14 '14 at 15:42
  • @user3938558 The first place to look for those "extensions" (also called "options") is in the man page for that command. From the command line, run `man inotifywait` and scroll down and you will see documentation for all the options that we used here. (Also see `man grep` for the `grep` command.) If you saw a one-line that used the options `-name` and `-type`, my guess is that you were looking at a `find` command. In that case, type `man find`. Although some are written better than others, man-pages all have similar format. Once you get used to the format, you will find them very helpful. – John1024 Oct 14 '14 at 17:15
  • okay thank you. i'll look at those. I'm finding it difficult to move from the very basic tutorials to slightly more complicated uses that I need in a short time! thank you for your help. – user3938558 Oct 14 '14 at 17:29