A powerful bash script that helps identify unreferenced files in your codebase. This tool is particularly useful for maintaining clean codebases by finding files that are no longer being used or imported anywhere in your project.
The find-unreferenced.sh script analyzes your codebase to identify files of a specific type that are not referenced anywhere in your project. It performs the following steps:
- Scans your codebase directory for all files of the specified type
- Excludes common build/dependency directories (node_modules, dist, automation, ng-mobile)
- Filters out test files (.spec. files)
- Searches the entire codebase for references to these files
- Generates detailed reports showing which files are unreferenced
- Code cleanup: Identify dead code and unused components
- Bundle optimization: Find unreferenced assets that increase bundle size
- Refactoring: Safely remove unused files during code refactoring
- Code auditing: Get insights into which parts of your codebase are actively used
This script works on Unix-like systems including:
- macOS (tested)
- Linux distributions
- Windows with WSL (Windows Subsystem for Linux)
The script requires the following tools to be installed:
- ripgrep (
rg) - Fast text search tool - python3 - For data processing and analysis
brew install ripgrep python3sudo apt update
sudo apt install ripgrep python3# For newer versions with dnf
sudo dnf install ripgrep python3
# For older versions with yum
sudo yum install ripgrep python3git clone https://github.com/jcmillett/unused-file-finder.git
cd unused-file-finderchmod +x find-unreferenced.sh./find-unreferenced.sh <codebase-dir> <output-dir> <file-type><codebase-dir>: Path to your project's source code directory<output-dir>: Directory where analysis results will be saved<file-type>: File extension to analyze (without the dot)
./find-unreferenced.sh /path/to/my-project ./jsp-results jsp./find-unreferenced.sh ~/projects/my-app ./js-results js./find-unreferenced.sh /home/user/website ./css-results css./find-unreferenced.sh . ./temp-results jsxThe script generates four detailed report files in your specified output directory:
all-files.txt- Complete list of all found files with full pathsfilenames.txt- Just the filenames (without paths)references.txt- All found references to the files in your codebaseunreferenced-files.txt- List of files that have no references (these may be safe to remove)
📁 Codebase directory: ./src
🗂️ Output directory: ./results
📄 File type: ts
🔍 Finding all .ts files...
📄 Found 45 .ts files
🪓 Extracting just file names...
🔎 Searching codebase for references to .ts files...
📎 Found 127 references to .ts files
📊 Comparing and identifying unreferenced .ts files...
Duplicate file names found: 0
Searched File Names: 45
Referenced Files: 38
Unreferenced Files: 7
Unreferenced .ts files:
• old-util.ts
• deprecated-service.ts
• temp-component.ts
• unused-helper.ts
• legacy-model.ts
• test-data.ts
• backup-config.ts
✅ Analysis complete!
📄 All .ts files: ./results/all-files.txt
📝 Filenames: ./results/filenames.txt
🔗 References found: ./results/references.txt
🗑️ Unreferenced files: ./results/unreferenced-files.txt
The script automatically excludes the following directories from analysis:
node_modules/- Package dependenciesdist/- Build outputautomation/- Automation scriptsng-mobile/- Angular mobile builds
- Test files matching the pattern
*.spec.*are automatically excluded from the unreferenced files list
- Dynamically imported at runtime
- Used in configuration files
- Required by external tools
- Referenced in a way the script doesn't detect