如何删除视频目录中多余SRT字幕,保留独立非英文字幕文件?
Your original command works too broadly—it deletes every non-English subtitle across all directories, regardless of whether that directory has an English .en.srt file or not. To match your exact rules, we need to handle each directory individually, checking for the presence of .en.srt first before deciding what to delete.
Here's a targeted solution using find and a small bash script to process each directory:
find /share/CACHEDEV4_DATA/Movies -type f -name "*.srt" -execdir bash -c ' # Ensure extended globbing is enabled for pattern matching shopt -s extglob # Check if this directory contains any English subtitles if compgen -G "*.en.srt" > /dev/null; then # Delete all .srt files EXCEPT .en.srt rm -v !(*.en.srt) else # Count how many non-English subtitles exist here total_subs=$(ls -1 *.srt | wc -l) if [ "$total_subs" -gt 1 ]; then # Warn about multiple non-English subs (you can adjust this to auto-delete extras if needed) echo "⚠️ Multiple non-English subtitles found in $(pwd) - review or clean manually." # Uncomment below to keep the first subtitle and delete the rest # ls -1 *.srt | tail -n +2 | xargs rm -v fi fi ' \;
How This Works:
- Directory-specific processing: The
-execdirflag runs the bash script directly in each directory containing.srtfiles, so we evaluate each folder's subtitle situation independently. - Check for English subs:
compgen -G "*.en.srt"silently checks if any.en.srtfiles exist (avoids messy error output fromls). - Delete non-English subs when English exists: Using
!(*.en.srt)(with extended globbing) targets all.srtfiles except the English ones for deletion. - Preserve single non-English subs: If no
.en.srtis found, we count the subtitles. If only one exists, we leave it alone. If multiple are present, we warn you (you can uncomment the line to auto-delete extras if that's what you want).
Why Your Original Command Failed:
Your original find command operates globally—it looks for every .srt that isn't .en.srt across your entire Movies directory, and deletes them. It doesn't care if a directory has no English subtitles at all, so it would delete valid single non-English subs that you wanted to keep.
Notes:
- If your system doesn't have extended globbing enabled by default, the
shopt -s extglobline ensures it's turned on for the script. - The warning for multiple non-English subs is optional—if you want to automatically keep one (e.g., the first one listed) and delete the rest, uncomment the line with
tail -n +2.
内容的提问来源于stack exchange,提问作者R David Paine III

