2

How can I create a script for monitoring file transferring including the source location and the destination location ? ( example : Test.file from /home/Desktop to /home/Documents ) .

Jacob Vlijm
  • 82,471
  • 12
  • 195
  • 299
Lenos Adamou
  • 23
  • 1
  • 6
  • Could you be a bit more specific please. Do you want to see progress of the command or just see when it's complete? Do you need any checks that the original file and the copy are identical? – Arronical Feb 16 '17 at 09:28
  • You can't monitor the commands, unless the commands were done in terminal, using the `script` command. You can monitor changes in files/directories though using `inotifywait`. – Jacob Vlijm Feb 16 '17 at 09:46
  • @JacobVlijm I tried to use inotifywait but I couldn't make it. Can you help me with it ? I want to monitor the media/user/ (USB) directory changes. But sometimes the message 'Permission Denied' . I want all the logs to be saved to a text file. I've been told that I need to make a script about this. I'm a new user of Ubuntu and I have tried a lot to find a solution about this without success. Any help would be really important. – Lenos Adamou Feb 17 '17 at 07:44
  • @Arronical I want to check in daily basis through a text file all the changes in the media/user/ directory. – Lenos Adamou Feb 17 '17 at 07:45
  • @LenosAdamou absolutely, but I will be home late today, and I'd like to be on my own system. Would tomorrow or late tonight be all right? – Jacob Vlijm Feb 17 '17 at 08:17
  • @JacobVlijm If you can help me with it , it would be really important . I'm sure many other users have the same problem. There aren't any solutions available on the Internet . Work on it when you will be available.. – Lenos Adamou Feb 17 '17 at 08:29
  • @JacobVlijm Any luck ? – Lenos Adamou Feb 20 '17 at 07:54
  • Hello Jacob . Yes , give me the answer if you can. If you find a solution about the source in the future you can inform me. – Lenos Adamou Feb 20 '17 at 10:02
  • @JacobVlijm Thanks Jacob for the support you gave to me. I'm back in office tomorrow and I will try it. It seems that this is the tool I need. Thanks in advance. – Lenos Adamou Feb 21 '17 at 19:37

1 Answers1

4

The answer below need inotify-tools, which might not be on your system. Run

sudo apt install inotify-tools

How to set up a watch script to keep log of what happens in a directory

As mentioned in comments, you can not "intercept" copy or move commands, unless you run them in terminal and use the script command to record what is happening.

You can however keep an eye on what happens inside a directory, with inotifywait

Basic inotifywait script to keep record of your directory

A simple script would be:

#!/bin/bash
DIR="/path/to/directory/to/watch"
inotifywait -m -r -e move -e create "$DIR" | while read f

do
    # remove 'echo changed' after the test
    echo changed
    echo $f >> /path/to/logfile.txt
done

Simply set the paths to the directory in the script, save it as some_script.sh and run it. More advanced would of course be to time stamp events, analyze output etc, but this is the main idea. Instead of "echoing" the output, you could use the -o option, see man inotifywait.

Explanation

To log continuesly, you need to set the option -m:

from man inotifywait:

-m, --monitor
    Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs. 

To log recursively, you need to set the option -r:

-r, --recursive
    Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Symbolic links are not traversed. Newly created subdirectories will also be watched.

Furthermore, you need to specify event(s) to trigger:

EVENTS
       The following events are valid for use with the -e option:

       access A  watched  file  or  a file within a watched directory was read
              from.

       modify A watched file or a file within a watched directory was  written
              to.

       attrib The metadata of a watched file or a file within a watched direc‐
              tory was modified.  This includes timestamps, file  permissions,
              extended attributes etc.

       close_write
              A  watched file or a file within a watched directory was closed,
              after being opened in writeable mode.  This does not necessarily
              imply the file was written to.

       close_nowrite
              A  watched file or a file within a watched directory was closed,
              after being opened in read-only mode.

       close  A watched file or a file within a watched directory was  closed,
              regardless  of  how  it  was opened.  Note that this is actually
              implemented  simply  by  listening  for  both  close_write   and
              close_nowrite, hence all close events received will be output as
              one of these, not CLOSE.

       open   A watched file or a file within a watched directory was opened.

       moved_to
              A file or directory was moved into a  watched  directory.   This
              event  occurs  even  if the file is simply moved from and to the
              same directory.

       moved_from
              A file or directory was moved from a  watched  directory.   This
              event  occurs  even  if the file is simply moved from and to the
              same directory.

       move   A file or directory was moved from or to  a  watched  directory.
              Note  that  this is actually implemented simply by listening for
              both moved_to and moved_from, hence all  close  events  received
              will be output as one or both of these, not MOVE.

       move_self
              A  watched  file  or  directory was moved. After this event, the
              file or directory is no longer being watched.

       create A file or directory was created within a watched directory.

       delete A file or directory within a watched directory was deleted.

       delete_self
              A watched file or directory was deleted.  After this  event  the
              file  or  directory  is no longer being watched.  Note that this
              event can occur even if it is not explicitly being listened for.

       unmount
              The filesystem on which a watched file or directory resides  was
              unmounted.   After this event the file or directory is no longer
              being watched.  Note that this event can occur even if it is not
              explicitly being listened to.

You need to prepend each of your events, to be triggered, with -e:

-e move -e create

Of course you can set any event trigger from the list.

The result

A short test of my watch script gives us an output like:

/home/jacob/Bureaublad/test/Untitled Folder/ MOVED_FROM CV.pdf
/home/jacob/Bureaublad/test/Untitled Folder/ MOVED_TO CV.pdf
/home/jacob/Bureaublad/test/Untitled Folder/ MOVED_TO pscript_3.py
/home/jacob/Bureaublad/test/Untitled Folder/ MOVED_TO,ISDIR numpy
/home/jacob/Bureaublad/test/Untitled Folder/ CREATE Untitled Document 3
/home/jacob/Bureaublad/test/Untitled Folder/ CREATE,ISDIR Untitled Folder
Jacob Vlijm
  • 82,471
  • 12
  • 195
  • 299