Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

ดู File Activity ของแอป Real-time บน Linux ด้วยคำสั่งเดียว

ดู File Activity ของแอป Real-time บน Linux ด้วยคำสั่งเดียว

เผยแพร่เมื่อวันที่ 19 เมษายน 2569 เวลา 14:00 น. EDT

Afam มีประสบการณ์ tech publishing ตั้งแต่ 2018 ทำงานที่ Make Tech Easier ตลอดปีสร้าง reputation สำหรับ high-quality guides, reviews, tips, และ explainer articles ครอบคลุม Windows, Linux, และ open source tools งาน featured บน top websites รวม Technical Ustad, Windows Report, Guiding Tech, Alphr, และ Next of Windows

มีปริญญาตรี Computer Science และเป็น strong advocate สำหรับ data privacy และ security มี tips, videos, tutorials บน Fuzo Tech YouTube channel

ตอนไม่ได้งาน ชอบใช้เวลากับครอบครัว ปั่นจักรยาน หรือดูแลสวน

ตอน setup Linux มัน typically runs as expected ที่ไหนซึ่ง background มัน create files, update configurations, และ build caches แต่ผม rarely peek under the hood ดู processes เหล่านี้ถ้าไม่มีอะไร broken

ครั้งนี้ผมอยากทำต่างออกไป ผมตัดสินใจ watch everything as it happened in background ดังนั้นผมใช้คำสั่ง inotifywait เพื่อ monitor real-time activity ผม surprised ด้วยความ powerful ของคำสั่งนี้ มันเปิดเผยว่า even simplest actions สามารถสร้าง several background operations

Linux feature ที่ track ทุกอย่าง

File activity ง่ายกว่าที่คุณคิด

ดู File Activity ของแอป Real-time บน Linux ด้วยคำสั่งเดียว

Inotify คือ kernel-level subsystem ที่เพิ่มเข้า Linux ใน version 2.6.13 Feature นี้ capture real-time file system events และ report พวกมัน as they occur มัน simply คือ kernel notifying you ของ changes

มัน constantly tracks file creation, modification, deletion, access, และ moves เพื่อ report events สิ่งที่ผมชอบในการใช้ inotify คือมัน only adds minimal overhead ระหว่าง typical use เพราะมัน simply reporting signals ที่ kernel already generates มันไม่ได้ run background scanner

คุณ can't directly interact กับ inotify และนั่นคือทำไมต้อง inotify-tools มัน provide two user-space utilities ที่ทำให้ kernel feature usable จาก terminal

Component

What it does

inotify

Kernel subsystem ที่ generate file system events

inotifywait

Streams those events live ไปที่ terminal

inotifywatch

Counts how many times each event type occurs over a set period

Of these components inotifywait คือ option ที่ allow access to real-time monitoring

คำสั่งเดียวที่ให้คุณดู apps live

จาก zero ถึง real-time file tracking ในไม่กี่วินาที

ดู File Activity ของแอป Real-time บน Linux ด้วยคำสั่งเดียว

เพื่อเริ่ม คุณต้องแค่ one command ติดตั้ง inotify-tools เพราะ inotify อยู่ใน kernel แล้ว

Distro

Command

Ubuntu/Debian

sudo apt install inotify-tools

Fedora

sudo dnf install inotify-tools

Arch Linux

sudo pacman -S inotify-tools

openSUSE

sudo zypper install inotify-tools

ติดตั้งเสร็จ confirm ready โดยรัน inotifywait --version และรัน inotifywait -m ~/Documents สำคัญต้อง include -m flag เพื่อให้ command ไม่ exit หลัง first event ทันทีคุณเริ่มเห็น changes ใน directory ในกรณีนี้ "Documents" directory Events ที่เห็น follow format: watched directory/event type/file that triggered the event ตัวอย่าง /home/user/Documents/ MODIFY notes.txt

เมื่อมี event คุณใช้ flags เล็กน้อยทำให้ report useful มากขึ้น

Flag

Purpose

-m

Keep running continuously แทน exit หลัง one event

-r

Watch all subdirectories recursively

-e

Filter specific event types เช่น -e create,modify,delete

--format

Control output structure สำหรับ readability หรือ logging

--timefmt

Add timestamps ให้แต่ละ event

นี่คือ basic commands ที่จะใช้ คุณแค่ต้อง point ไปที่ something real

สิ่งที่ผมเห็นจริงตอนใช้กับ real apps

File behaviors ที่คุณปกติไม่สังเกต

เพื่อดูสิ่งที่เกิดขึ้นบนคอมพิวเตอร์ ผมใช้ apps ตามปกติขณะ point inotifywait ไป specific directories สิ่งที่ surprising จริงไม่ใช่สิ่งที่มัน show แต่ sheer amount ของ background activity

เริ่มกับ text editor ขณะ monitor Documents folder ผม save file คาดหวัง one event แตได้ sequence แสดง temporary file ถูก created ตามด้วย moved_from และ moved_to events ในทั้งหมดนี้ text editor ไม่เคย touch original file โดยตรง ตอนสร้าง file มันเขียนไป throwaway file ก่อน swap เข้าไป วิธีนี้ original file intact แม้มีอะไรเกิดขึ้น mid-write

ผม switch ไป ~/.mozilla/firefox/ directory monitor Firefox เปิด browser มี instant burst of writes ภายในวินาทีมี places.sqlite สำหรับ bookmarks และ history แล้ว sessionstore.jsonlz4 สำหรับ open tabs แต่สิ่งน่าสนใจคือ constant writes แม้ browser idle นี่ appears normal browser behavior ผมสังเกต idle writes คล้ายกับ Chrome substantial background activity ใน Firefox important เพราะมัน continuously flushes session data to disk ensure tabs recovered after crash

แตของทั้งหมด package installations ทำ noise มากที่สุด ผมรัน apt install ขณะ watch /var/lib/dpkg/ และเห็น lock-frontend file appear first นี่คือ important mechanism ensure package operations ไม่ run concurrently หลังนั้นแค่ writes เยอะมาก across package database

สิ่งที่ consistent across three directories ที่ observed คือ never a single clean file operation ทุกอย่างมาพร้อม layers

เมื่อสิ่งนี้กลายเป็น genuinely useful ในชีวิตจริง

เริ่มเป็น experiment แตแค่ few sessions inotifywait กลายเป็น tool ที่ผม naturally reach for มัน important tool สำหรับ debugging configuration issues Watch config directory และ note overwritten files as they happen ให้ clarity ตอน setting keeps resetting itself

ผมยัง reach for it ตอน evaluate software ที่ไม่คุ้นเคย ไม่ต้อง guess ว่า software เก็บ data ที่ไหน เพราะตอนนี้ watch what it touches ได้ มัน easy way catch apps ที่ write outside expected directories

แต่ถ้าใช้ inotifywait พร้อมสำหรับ very noisy output limitation ที่สองคือแม้ show what changed มันไม่ explain why นั้น said มันกลายเป็น one of the most useful commands ผมใช้ manage Linux