查找要读取或写入的文件

我想看看哪些文件被读取或写入。

是否有任何程序或命令? 我记得几年前我使用Windows时使用这种方法来搜捕病毒和恶意软件隐藏位置。

该程序是lsof (“列表打开文件”)

  • 如果您只是打开一个终端并键入lsof ,您将获得所有打开文件的大量列表,而是通过执行以下操作将其限制为一个命令:

     lsof -c gnome-terminal 
  • 您还可以通过键入将搜索限制为特定目录

     lsof -c gnome-terminal -a +D /tmp 
  • 或列出一个特定目录中的所有打开文件,包括打开它的应用程序:

     lsof /dev/urandom 

请记住,某些进程是由超级用户root启动的,您可能需要在sudo前放置sudo以获取有关此类进程的打开文件的更多信息。

要缩小搜索范围,可以使用grep特定行,即:

 lsof /dev/urandom | grep chrome 

  • 输出的FD (文件描述符)列为您提供有关程序打开文件的目的的信息(不一定是此刻发生的事情):

    • r表示文件已打开以供阅读

    • w表示文件已打开以进行写入

    • u意思是打开文件进行读写


有关更多详细信息,请参阅手册页 ( man lsof )。 此外,如果您需要查找任何文件和目录, Linux Filesystem Hierarchy Standard非常有用。

作为一个完整的过度杀戮选项,但实时工作,您可以使用inotify:

 sudo inotifywait -m -r / 

请注意,这会占用大量内存,并且需要很长时间才能完成设置。 正如联机帮助页所说:

  -r, --recursive Watch all subdirectories of any directories passed as arguments. Watches will be set up recursively to an unlimited depth. Sym‐ bolic links are not traversed. Newly created subdirectories will also be watched. Warning: If you use this option while watching the root direc‐ tory of a large tree, it may take quite a while until all ino‐ tify watches are established, and events will not be received in this time. Also, since one inotify watch will be established per subdirectory, it is possible that the maximum amount of ino‐ tify watches per user will be reached. The default maximum is 8192; it can be increased by writing to /proc/sys/fs/ino‐ tify/max_user_watches. 

这也没有告诉您哪些进程正在弄乱文件,但它可能有助于识别发生的变化。 使用“-e open”可以帮助减少非常繁忙的系统上的一些噪音。