Looks like no one’s replied in a while. To start the conversation again, simply ask a new 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 Feb 28, 2018 5:05 PM

Reply
Question marked as Best reply

Posted on Mar 1, 2018 8:09 AM

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

4 replies

Feb 28, 2018 8:30 PM 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!

Mar 1, 2018 9:00 AM 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!

Problem using terminal to delete duplicates

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.