Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Linux

Theo dõi hoạt động file app real-time trên Linux bằng một lệnh đơn giản

Theo dõi hoạt động file app real-time trên Linux bằng một lệnh đơn giản

Đăng ngày 19 tháng 4 năm 2026, 2:00 PM EDT

Kinh nghiệm xuất bản tech của Afam từ 2018, khi làm việc cho Make Tech Easier. Qua nhiều năm, anh xây dựng uy tín xuất bản guide, review, tips, và bài giải thích chất lượng cao, bao quát Windows, Linux, và công cụ open source. Công trình anh xuất hiện trên top website bao gồm Technical Ustad, Windows Report, Guiding Tech, Alphr, và Next of Windows.

Anh có bằng Cử nhân Khoa học Máy tính và là người ủng hộ mạnh mẽ privacy và bảo mật dữ liệu, với vài tips, video, tutorial trên chủ đề này trên kênh YouTube Fuzo Tech.

Khi không làm việc, anh thích dành thời gian với gia đình, đạp xe, hoặc chăm vườn.

Khi setup Linux, nó typically runs as expected (chạy như mong đợi). Đâu đó trong background, nó tạo file, update config, build cache. Tuy nhiên, tôi rarely peek under the hood (hiếm khi nhìn bên dưới capo) thấy các process này nếu không có gì broken.

Lần này, tôi muốn làm khác. Tôi quyết định watch (theo dõi) mọi thứ khi nó xảy ra trong background. Vậy tôi dùng command inotifywait để monitor real-time activity. Tôi surprised (ngạc nhiên) bởi sức mạnh của command này. Nó reveal (tiết lộ) cách ngay cả action đơn giản nhất tạo ra vài background operation.

Tính năng Linux theo dõi mọi thứ

Hoạt động file dễ monitor hơn bạn nghĩ

Theo dõi hoạt động file app real-time trên Linux bằng một lệnh đơn giản

Inotify là kernel-level subsystem added vào Linux version 2.6.13. Feature này capture real-time file system events và report chúng khi chúng occur. Nó đơn giản là kernel notify bạn về thay đổi.

Nó constantly track (theo dõi liên tục) file creation, modification, deletion, access, và moves để report event. Điều tôi thích về inotify là nó chỉ add minimal overhead (chi phí tối thiểu) trong typical use. Vì nó simply reporting signals kernel đã generate; nó không chạy background scanner.

Bạn không thể directly interact (tương tác trực tiếp) với inotify, và đó là lý do bạn cần inotify-tools. Nó cung cấp 2 user-space utility làm kernel feature usable từ terminal.

Component

What it does

inotify

Kernel subsystem generate file system events

inotifywait

Stream events live đến terminal

inotifywatch

Đếm bao nhiêu lần mỗi event type xảy ra trong khoảng thời gian

Trong các component, inotifywait là option cho phép access real-time monitoring.

Một lệnh cho bạn watch app live

Từ zero đến real-time file tracking trong giây

Theo dõi hoạt động file app real-time trên Linux bằng một lệnh đơn giản

Để bắt đầu, bạn chỉ cần một lệnh cài inotify-tools vì inotify đã trong 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

Sau khi cài, confirm ready bằng inotifywait --version, rồi chạy inotifywait -m ~/Documents Quan trọng include flag -m để command không exit sau event đầu. Ngay lập tức, bạn thấy thay đổi trong directory, trường hợp này "Documents". Event format: watched directory/event type/file triggered. Ví dụ: /home/user/Documents/ MODIFY notes.txt.

Khi có event, dùng flag để report useful hơn.

Flag

Purpose

-m

Chạy liên tục thay vì exit sau một event

-r

Watch tất cả subdirectory recursively

-e

Filter event type cụ thể, vd -e create,modify,delete

--format

Control output structure cho readability hoặc logging

--timefmt

Thêm timestamp cho mỗi event

Đây là basic command bạn dùng; chỉ cần point chúng đến thứ real.

Thực tế tôi thấy khi dùng trên app thật

Hành vi file bạn bình thường không nhận ra

Để xem gì xảy ra trên máy, tôi dùng app cách tôi thường làmながら pointing inotifywait đến directory cụ thể. Điều thực sự surprise tôi không phải gì nó show, mà sheer amount (số lượng lớn) background activity.

Tôi bắt đầu với text editor. Monitor Documents folder, tôi save file. Tôi expect một event, nhưng nhận được sequence. Nó show temporary file created, theo sau moved_from và moved_to event. Trong tất cả, text editor không bao giờ touch original file trực tiếp. Khi tạo file, nó write vào throwaway file trước rồi swap in. Cách này, original file intact ngay cả khi có chuyện mid-write.

Tôi switch sang ~/.mozilla/firefox/ directory, monitor Firefox. Launch browser, instant burst writes. Trong giây, có places.sqlite cho bookmark và history, rồi sessionstore.jsonlz4 cho open tab. Tuy nhiên, điều interesting là constant writes ngay khi browser idle. Đây dường như normal browser behavior. Tôi notice similar idle writes với Chrome. Background activity đáng kể Firefox quan trọng vì continuously flush session data to disk, ensure tab recover sau crash.

Tuy nhiên, trong tất cả, cài package gây noise nhất. Tôi chạy apt install watch /var/lib/dpkg/, thấy lock-frontend file xuất hiện trước. Cơ chế quan trọng đảm bảo package operation không chạy concurrent. Sau đó, just so many writes (rất nhiều ghi) xảy ra across package database.

Consistent (nhất quán) across 3 directory tôi observe là never có single clean file operation. Chúng all come with layers (lớp).

Khi nào điều này genuinely useful (thực sự hữu ích) trong đời thực

Điều này bắt đầu như experiment, nhưng chỉ vài session trước inotifywait trở thành tool tôi naturally reach for (tự nhiên伸手拿). Nó important tool cho debug config issue. Watch config directory và note overwritten files khi chúng happen देता clarity khi setting reset liên tục.

Tôi cũng reach for nó khi evaluate software tôi không familiar. Tôi không còn cần guess nơi software store data, vì tôi now watch (giờ theo dõi) nó touch gì. Easy way catch app viết ngoài expected directory.

Tuy nhiên, nếu bạn dùng inotifywait, be ready for (chuẩn bị) very noisy output. Limitation thứ 2: dù nó show gì changed, nó không explain why. That said, nó become one of most useful command tôi dùng manage Linux.