如何创建一个脚本来监控文件传输,包括源位置和目标位置?(例如:Test.file 从 /home/Desktop 到 /home/Documents)。
答案1
以下答案需要inotify-tools
,但您的系统上可能没有。运行
sudo apt install inotify-tools
如何设置监视脚本来记录目录中发生的事情
正如评论中提到的,您无法“拦截”复制或移动命令,除非您在终端中运行它们并使用该script
命令记录正在发生的事情。
你能但是要注意目录内部发生的情况,通知等待
基本 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
请参阅手册 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