Announcement: Upgrade to macOS Mojave

With features like Dark Mode, Stacks, and four new built-in apps, macOS Mojave helps you get more out of every click. 
Find out how to upgrade to macOS Mojave > https://support.apple.com/macos/mojave

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: Problem using terminal to delete duplicates

I cannot figure this out even after extensive research.


I have a large set of folders that contain file duplicates with (1) appended to the file name. Too many to want to do this manually.


The challenge is the ( ) characters are special characters in terminal.


The pattern is:

Folders

filename.extension

filename(1).extension

etc.


Some of the sub folders have (data) in the folder names and some of the file names have the parens, i.e, (data) in the file names. I need to recursively delete all, and only, files that end in (1) (or (1).ext, or (1).)


I have tried a couple duplicate finders, but they either require manual selection or randomly pick one or the other to keep.


How can I do this?

(I can use any OS X version for this)

MacBook Pro, macOS Sierra (10.12.6), MacBook Pro 11,2 16GB RAM, SSD

Posted on

Reply
Question marked as Solved
Answer:
Answer:

You need a regular expression then. Try this from the directory where you want to delete:

find -E . -regex '.*\([0-9]\).*'


if this returns the files you want to delete, then you can do the following:


find -E . -regex '.*\([0-9]\).*' -delete

Posted on

Feb 28, 2018 8:30 PM in response to etresoft In response to etresoft

That was the first thing I tried, I should have mentioned it.

(Assume (text) means any text inside the brackets, except (1))


It finds only folders with names that include:

foldername (text)

(text) foldername, or files with

(text) filename, or

filename (text)


but not a single file with:

filename(1).ext.


It will see for instance,

filename (1964)

Filename (part1)

Filename (France/1999)

The problem, I assume is when there is no spaces and ( touches the file name. If there is a space before the (, it is OK.

I can navigate down to a single folder, with only 50 items in it, 25 of which are the problem duplicates, and get no result.


thanks!

Feb 28, 2018 8:30 PM

Reply Helpful
Question marked as Solved

Mar 1, 2018 8:09 AM in response to StarsEnd In response to StarsEnd

You need a regular expression then. Try this from the directory where you want to delete:

find -E . -regex '.*\([0-9]\).*'


if this returns the files you want to delete, then you can do the following:


find -E . -regex '.*\([0-9]\).*' -delete

Mar 1, 2018 8:09 AM

Reply Helpful

Mar 1, 2018 9:00 AM in response to etresoft In response to etresoft

This did it. I need to learn how to use these kinds of expressions.


It was not quite 00% accurate, but running BBEdit with some search/process lines filters, I found about a dozen folders that I moved out, so I could process those manually, then ran your script to delete the remaining folders. Worked great!


Well done, thanks!

Mar 1, 2018 9:00 AM

Reply Helpful
User profile for user: StarsEnd

Question: Problem using terminal to delete duplicates