web-dev-qa-db-ja.com

ファイルのコピーと貼り付けのためにディレクトリを監視するにはどうすればよいですか?

ソースの場所と宛先の場所を含むファイル転送を監視するためのスクリプトを作成するにはどうすればよいですか? (例:/ home/Desktopから/ home/DocumentsへのTest.file)。

2
Lenos Adamou

以下の答えにはinotify-toolsが必要です。これはシステムにない可能性があります。走る

Sudo apt install inotify-tools

監視スクリプトをセットアップして、ディレクトリで発生したことのログを保持する方法

コメントで述べたように、ターミナルでコマンドを実行してscriptコマンドを使用して何が起こっているのかを記録しない限り、コピーまたは移動コマンドを「インターセプト」することはできません。

あなたはcanしかし inotifywait でディレクトリ内で何が起こるかを監視します

ディレクトリの記録を保持する基本的なinotifywaitスクリプト

簡単なスクリプトは次のとおりです。

#!/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

スクリプトでディレクトリへのパスを設定し、some_script.shとして保存して実行します。もちろん、イベントをタイムスタンプしたり、出力を分析したりすることもできますが、これが主なアイデアです。出力を「エコー」する代わりに、-oオプションを使用できます。 man inotifywait を参照してください。

説明

継続的にログを記録するには、オプション-mを設定する必要があります。

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. 

再帰的にログを記録するには、オプション-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.

さらに、トリガーするイベントを指定する必要があります:

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.

トリガーする各イベントの前に、-eを追加する必要があります。

-e move -e create

もちろん、リストから任意のイベントトリガーを設定できます。

結果

監視スクリプトの短いテストでは、次のような出力が得られます。

/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
4
Jacob Vlijm