简体   繁体   中英

Delete Files with mac terminal

I'm trying to use find in the terminal to delete some files that PyCharm has created seemingly at random. It's created duplicates of files like:

manage.py
manage 2.py
manage 3.py
index.js
index 2.js

So the pattern I'm trying to match is a string followed by a space and then a number with different endings. Currently I'm trying:

find pathToMyFiles -type -f -regex '.+?\w+ [0-9](.py|.js)$' 

This fails to identify any files in the directory even though it captures my test strings when I go to regexer or any of the other regex test sites, am I missing something that's mac terminal exclusive? I know this script will not delete any files, I'm just trying to make sure I'm correctly identifying them first.

Instead of regular expressions, you could combine two -name statements:

$ find -name '* [[:digit:]].py' -o -name '* [[:digit:]].js'
./manage 2.py
./manage 3.py
./index 2.js

This finds anything that ends in a blank and a digit with extension .py or .js .

If you want to use your regex, you have to escape a few things differently (see default regular expression syntax ):

$ find -regex '.+\w+ [0-9]\.\(py\|js\)$'
./manage 2.py
./manage 3.py
./index 2.js

() for capture groups, | for alternation and . for literal period all have to be escaped. .+? is not a valid combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM