Mac终端使用find命令匹配PyCharm生成的重复文件问题
find Command to Match Duplicate PyCharm Files on Mac Hey there! Let's get that find command working properly on your Mac terminal. The issue comes down to a couple of Mac-specific quirks with BSD find (which is what macOS uses) and a small syntax error in your original command. Here's how to fix it:
1. Fix the -type Parameter Error
First, your original command has a typo: -type -f should be -type f (no extra hyphen after -type). That's a quick fix but essential for find to correctly target files instead of throwing an invalid parameter error.
2. Account for BSD find's Regex Behavior
Unlike many online regex testers which use extended regular expressions (ERE) by default, macOS's find uses basic regular expressions (BRE) out of the box. This means characters like +, |, and () need special treatment unless you explicitly enable ERE.
Option 1: Use -E to Enable Extended Regex (Simpler)
Add the -E flag right after find to switch to ERE, which matches the regex syntax you tested online. Then adjust the regex to properly match file suffixes (remember to escape the . since it's a regex wildcard that matches any character):
find pathToMyFiles -type f -E -regex '.+ [0-9]+\.(py|js)$'
Let's break down the regex:
.+: Matches the main part of the filename (e.g.,manage,index)[0-9]+: Matches a space followed by one or more digits (covers2,3,10, etc., not just single digits)\.(py|js)$: Escapes the.to match the actual file extension dot, then targets.pyor.jsfiles, with$ensuring we match the end of the filename.
Option 2: Use Basic Regex (With Escapes)
If you prefer not to use -E, you'll need to escape all the ERE-specific characters in your regex to make them work in BRE:
find pathToMyFiles -type f -regex '.\+ [0-9]\+\.\(py\|js\)$'
Here, \+, \|, and \(/\) are escaped to function as their ERE equivalents in basic regex mode.
3. Test Before Deleting
To verify the command is targeting the right files, add -print to the end to list matches without making any changes:
find pathToMyFiles -type f -E -regex '.+ [0-9]+\.(py|js)$' -print
This will output all the duplicate files PyCharm generated, so you can confirm it's working before moving on to deletion.
内容的提问来源于stack exchange,提问作者Michael Porter

