如何记录对特定文件夹的访问权限并更改其内容?

我需要跟踪文件夹访问时间,并想知道对它进行了哪些更改。

如何触发这些事件? 有没有办法在打开文件夹时运行特定的.sh文件?

我假设您需要知道文件夹在(例如) nautilus中打开的(时钟)时间,而不是访问文件夹所需时间

使用窗口列表

您可以从命令wmctrl -l获取窗口列表,并查看文件夹的名称是否出现在列表中。 然而,要检查的循环至少需要一瞬间才能注意到文件夹已打开。

你要安装wmctrl

 sudo apt-get install wmctrl 

在下面的示例中,脚本在第一次访问文件夹时运行命令,然后退出。

如何使用:

  • 将脚本粘贴到空文件中
  • 将其另存为access_time.py
  • 您的命令(引号之间)更改脚本""的head部分
  • 使用以下命令运行它:

     python3   

    或者,如果你使它可执行:

       
 #!/usr/bin/env python3 import subprocess import sys #--- replace "" with your command (between quotes): command = "" #--- foldername = sys.argv[1] while True: try: test = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8") except subprocess.CalledProcessError: pass if foldername in test: subprocess.call(["/bin/bash", "-c", command]) break 

编辑

但是,您可以将其设置为“一体化”,因此您不需要其他脚本。 下面的脚本在$ HOME目录中创建一个文件,其中包含访问文件夹的时间:

 #!/usr/bin/env python3 import subprocess import sys import os import time home = os.environ["HOME"] foldername = sys.argv[1] #--- the path your file is saved to (change if you want to, but use full paths) path = home #--- while True: try: test = subprocess.check_output(["wmctrl", "-l"]).decode("utf-8") except subprocess.CalledProcessError: pass if foldername in test: with open(path+"/time_data.txt", "a") as out: out.write("the folder "+foldername+" was opened "+time.ctime()+"\n") break 
  • 像第一个选项一样使用它(但显然你不需要设置命令)
  • 在文件名前放置一个点以使其成为隐藏文件(按Ctrl + H切换可见性):

    如果你想要,改变:

     with open(path+"/time_data.txt", "a") as out: 

    成:

     with open(path+"/.time_data.txt", "a") as out: 

    (记住缩进!)

编辑2

根据您的评论和聊天中的讨论,我了解到您实际上正在寻找一种工具来记录对文件夹的访问(例如通过nautilus)并更改其内容。
作为额外选项,一个全面的日志脚本记录在两个不同的线程中:

  • 所有场合的文件夹都是由例如nautilus访问的,记录在文件access_log.txt
  • 文件夹窗口关闭的所有场合也都记录在access_log.txt
  • 所有添加到(递归)或从目录中删除的文件都会记录到文件directory_log.txt

这些事件记录在两个不同的文件中,因为日志具有不同的刷新时间。 实时“记录”具有大量子目录的大型目录所发生的事情并不是您希望每5秒钟左右完成的事情。 结果是:

  • 访问日志(我设置)的精度为0.5秒
  • 目录日志(添加/删除文件)的准确度为10分钟。 活动将在发生后10分钟内报告,时间戳精确度为10分钟。

    我在~800 GB的(网络)目录上测试了它。 如果您的目录小得多,则目录日志周期也可以(更小)。 我在20 GB目录上测试了它,其(目录日志)周期为10秒。

示例输出access_log.txt:

 ---------------Thu Feb 19 21:01:09 2015--------------- folder opened ---------------Thu Feb 19 21:01:27 2015--------------- folder closed 

示例输出directory_log.txt:

 ---------------Thu Feb 19 21:14:24 2015--------------- + /home/jacob/Afbeeldingen/Downloads/2023.pdf - /home/jacob/Afbeeldingen/Downloads/History-journal - /home/jacob/Afbeeldingen/Downloads/google-earth-stable_current_i386.deb 

剧本:

  • 像上面的脚本一样设置它有一个重要的区别

    • 而不是使用文件夹名称作为参数 ,在脚本的头部设置完整路径 + foldername(参见脚本中的示例)
  • 然后运行它的命令是:

     python3 /path/to/script.py 
 #!/usr/bin/env python3 import subprocess import os import time import difflib from threading import Thread home = os.environ["HOME"] # The folder to watch: folder = "/home/jacob/Afbeeldingen" # the path your log files are saved to (change if you want to, but use full paths): path = home #--- for f in os.listdir(path): if f.startswith("dr_check_"): os.remove(path+"/"+f) dr_data = path+"/directory_log.txt" access_data = path+"/access_log.txt" for f in [dr_data, access_data]: if not os.path.exists(f): subprocess.Popen(["touch", f]) foldername = folder.split("/")[-1] def check_windowlist(foldername): while True: try: if foldername in subprocess.check_output(["wmctrl", "-l"]).decode("utf-8"): return "folder opened\n" else: return "folder closed\n" break except subprocess.CalledProcessError: pass def check_directory(directory, outfile): with open(outfile, "wt") as out: for root, dirs, files in os.walk(directory): for f in files: out.write(root+"/"+f+"\n") def run_accesscheck(): while True: ch1 = check_windowlist(foldername) time.sleep(0.5) ch2 = check_windowlist(foldername) if ch1 != ch2: with open(access_data, "a") as out: out.write("-"*15+time.ctime()+"-"*15+"\n"+ch2+"\n") def run_directorycheck(): last = 1; outfile_name = "dr_check_"; last_outfile = "" while True: outfile = path+"/"+outfile_name+str(last)+".txt" check_directory(folder, outfile) if last != 1: changes = [] diff = difflib.ndiff( open(last_outfile).readlines(), open(outfile).readlines() ) for item in diff: if item.startswith("-") or item.startswith("+"): changes.append(item) if len(changes) > 0: with open(dr_data, "a") as out: out.write("-"*15+time.ctime()+"-"*15+"\n") for it in sorted(changes): out.write(it) out.write("\n") os.remove(last_outfile) last_outfile = outfile; last = last+1 time.sleep(600) Thread(target = run_directorycheck).start() Thread(target = run_accesscheck).start() 

如果你想使用Bash而不是Python:

 #!/bin/bash folder=$1 while true; do command=$(wmctrl -l | grep -o "$folder") if [[ "$folder" == "$command" ]]; then ./myscript.sh break; fi done 

编辑:

我更改了一个脚本,因此您可以使用以下命令运行它:

 bash folderwatch.sh BackupSSD 

此外,您可以使脚本可执行,以便您可以在不使用sh或bash的情况下使用它,因为shell是在脚本的第一行中定义的,例如:

 chmod u+x folderwatch.sh ./folderwatch.sh BackupSSD 

sudo apt-get incron安装“inotify cron”系统

http://inotify.aiken.cz/?section=incron&page=about&lang=en

echo $USER | sudo tee --append /etc/incron.allow echo $USER | sudo tee --append /etc/incron.allow允许你玩游戏。

icrontab -e创建要观看的事件。 它打开nano

输入你的心愿。 例如,

/home/nodak/watched_dir IN_ACCESS /home/nodak/bin/personal.sh

保存并测试。

有关更多信息,请访问http://manpages.ubuntu.com/manpages/saucy/man5/incrontab.5.html

虽然它很简单,但看起来很简单,复杂机动的语法与普通的 bash并不完全相同,参见https://stackoverflow.com/questions/23706194/using-zenity-in-a-root-incron-job -to-显示消息到当前登录在用户