求Linux脚本:暂停指定时长后统计当前目录已删除文件总大小
Solution for
watchdir Script Got it, let's build this watchdir script together. The key here is that deleted files still held open by processes don't show up in regular directory listings, but we can track them via the /proc filesystem. Here's the step-by-step implementation:
Step 1: Script Implementation
Save this as watchdir, then make it executable with chmod +x watchdir:
#!/bin/bash # Validate input: check if exactly one numeric argument is provided if [ $# -ne 1 ] || ! [[ $1 =~ ^[0-9]+$ ]]; then echo "Usage: $0 <seconds>" exit 1 fi # Display sleep message and wait for the specified time echo "# sleeping for a while ..." sleep "$1" # Initialize total size counter total_deleted_size=0 # Get absolute path of current directory to match against deleted files current_dir=$(pwd) # Iterate over all running processes for proc_dir in /proc/[0-9]*; do # Skip if process directory no longer exists (process exited mid-iteration) [ -d "$proc_dir" ] || continue # Check each file descriptor the process has open for fd_link in "$proc_dir"/fd/*; do # Read the target of the file descriptor symbolic link target=$(readlink "$fd_link" 2>/dev/null) # Check if the target is a deleted file in our current directory if [[ "$target" == "$current_dir"/*" (deleted)" ]]; then # Strip the "(deleted)" suffix to get the original file path original_file=${target% (deleted)} # Get the file size (ignore errors if file was already released) file_size=$(stat -c%s "$original_file" 2>/dev/null) # Add to total if size is valid if [[ -n "$file_size" ]]; then total_deleted_size=$((total_deleted_size + file_size)) fi fi done done # Output the total size of deleted files echo "removed files total size: $total_deleted_size"
Step 2: How It Works
Let's break down the key parts:
- Input Validation: Makes sure you pass exactly one numeric value (the number of seconds to sleep). If not, it shows a clear usage message and exits.
- Sleep: Uses
sleep "$1"to pause for the specified duration, matching your example output. - Tracking Deleted Files:
- We loop through all process directories in
/proc(each numbered folder corresponds to a running process). - For each process, we check its file descriptors (
/proc/<pid>/fd/), which are symbolic links to files the process has open. - A deleted file held open by a process will have a link target ending with
(deleted). We verify if this file was originally in the current directory. - We use
statto get the file size and accumulate the total, skipping any errors if the file was released mid-check.
- We loop through all process directories in
- Output: Finally, we print the total size of all such deleted files in bytes.
Notes & Considerations
- Permissions: To access file descriptors of processes owned by other users, run the script as
root(viasudo). Otherwise, it will only track deleted files from your own processes. - Race Conditions: If a process exits or releases the deleted file while the script runs, we skip those cases with
2>/dev/nulland[ -d "$proc_dir" ]checks to avoid errors. - Human-Readable Size: If you want output in KB/MB instead of bytes, modify the final echo line to use
numfmt:echo "removed files total size: $(numfmt --to=iec "$total_deleted_size")"
Example Usage
$ ./watchdir 60 # sleeping for a while ... removed files total size: 2345
内容的提问来源于stack exchange,提问作者Lava Matter
相关产品推荐
相关产品推荐

