You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量统计目录内文件名跨文件引用情况的技术实现问询

Hey, I totally get the pain of manually searching 400 files—what a slog! Here are two efficient, script-based solutions to automate this task, depending on your operating system and comfort level with code.

Bash Script (Linux/macOS)

This is a fast, terminal-native solution perfect for Unix-like systems. It uses standard command-line tools to count unique file references:

#!/bin/bash

# Replace with your target directory path (e.g., "./my_project_files")
TARGET_DIR="./"

# Get all filenames in the directory (excludes subdirectories by default)
find "$TARGET_DIR" -maxdepth 1 -type f | xargs basename | while read -r FILE; do
    # Search for the filename across all files, exclude the file itself, count unique matches
    COUNT=$(grep -r "$FILE" "$TARGET_DIR" --include="*" --exclude="$FILE" | cut -d: -f1 | sort -u | wc -l)
    echo "$FILE: $COUNT"
done

How it works:

  • find grabs all file names in your target directory (remove -maxdepth 1 if you want to include subdirectories)
  • grep -r searches recursively for the filename, skipping the file itself to avoid self-reference counts
  • cut -d: -f1 extracts the path of each matching file
  • sort -u removes duplicate file paths so each file is only counted once
  • wc -l tallies the number of unique files that reference the target filename

Python Script (Cross-Platform)

If you need something that works on Windows, Linux, or macOS, this Python script is flexible and easy to adjust:

import os
from collections import defaultdict

# Replace with your target directory path
target_dir = "./"

# Get all files in the directory (exclude subdirectories)
files = [f for f in os.listdir(target_dir) if os.path.isfile(os.path.join(target_dir, f))]

# Store each filename and the set of files that reference it (sets automatically handle duplicates)
file_reference_counts = defaultdict(set)

# Loop through each file to check for references to other filenames
for current_file in files:
    file_path = os.path.join(target_dir, current_file)
    try:
        with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read()
            # Check each filename against the current file's content
            for filename in files:
                if filename == current_file:
                    continue  # Skip self-references (remove this line to include them)
                if filename in content:
                    file_reference_counts[filename].add(current_file)
    except Exception as e:
        print(f"Warning: Could not read {current_file} - {e}")

# Print the final results
print("Filename | Number of unique files referencing it")
print("-----------------------------------------------")
for filename, referenced_in in file_reference_counts.items():
    print(f"{filename:<20} | {len(referenced_in)}")

# Uncomment below to include files with zero references
# for filename in files:
#     count = len(file_reference_counts.get(filename, set()))
#     print(f"{filename:<20} | {count}")

How it works:

  • Uses os.listdir to gather all files in the target directory
  • defaultdict(set) tracks which files reference each filename—sets ensure we don't count the same file multiple times
  • Reads each file's content, checks for matches with other filenames, and updates the reference set
  • Handles encoding errors gracefully with errors='ignore' to avoid crashing on non-UTF-8 files

Quick Tweaks for Your Use Case

  • Special characters/space in filenames: The Bash script uses while read -r to handle these correctly—stick with that version instead of an array-based approach.
  • Include subdirectories: For Bash, remove -maxdepth 1 from the find command. For Python, replace the files list with a loop using os.walk to traverse nested directories.
  • Include self-references: Remove the if filename == current_file: continue line in Python, or remove --exclude="$FILE" from the Bash grep command.

内容的提问来源于stack exchange,提问作者jraw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:48:52