Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:51:47+00:00 2026-06-12T08:51:47+00:00

I’ve got a directory with a few thousand files in it, named things like:

  • 0

I’ve got a directory with a few thousand files in it, named things like:

filename.ext
filename (1).ext
filename (2).ext
otherfile.ext
otherfile (1).ext
etc.

Most of the files with bracketed numbers are duplicates of the original, but in some cases they’re not.

How can I keep my original files, delete the duplicates, but not lose the files that are different?

I know that I could rm *\).ext, but that obviously doesn’t make sure that files match the original.

I’m using OS X, so I have a md5 program that functions sort of like md5sum in Linux, though it puts the hash at the end of the line instead of the beginning. I was thinking I could use an awk script to take the output of md5 *.ext | awk 'some script', find duplicates by md5, and delete them, but the command line is too long (bash: /sbin/md5: Argument list too long).

And I don’t know what to write in the script. I was thinking of storing things in an array with this:

awk '{a[$NF]++} a[$NF]>1{sub(/).*/,""); sub(/.*(/,""); system("rm " $0);}'

But that always seems to delete my original.

What am I doing wrong? How do I do it right?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:51:49+00:00Added an answer on June 12, 2026 at 8:51 am

    Your awk script deletes original files because when you sort your files, . (period) sorts after (space). SO the first file that’s seen is numbered, not the original, and subsequent checks (including the one against the original) compare files to the first numbered one.

    Not only does rm *\).txt fail to match the original, it loses files that may not have an original in the first place.

    I wouldn’t do this quite this way. Rather than checking every numbered file and verifying whether it matches an original, you can go through your list of originals, then delete the numbered files that match them.

    Instead:

    $ for file in *[^\)].txt; do echo "-- Found: $file"; rm -v $(basename "$file" .txt)\ \(*\).txt; done
    

    You can expand this to check MD5’s along the way. But it’s more code, so I’ll break it into multiple lines, in a script:

    #!/bin/bash
    
    shopt -s nullglob              # Show nothing if a fileglob matches no files
    
    for file in *[^\)].ext; do
      md5=$(md5 -q "$file")        # The -q option gives you only the message digest
      echo "-- Found: $file ($md5)"
      for duplicate in $(basename "$file" .ext)\ \(*\).ext; do
         if [[ "$md5" = "$(md5 -q "$duplicate")" ]]; then
            rm -v "$duplicate"
         fi
      done
    done
    

    As an alternative, you can probably get away with doing this a little more simply, with less CPU overhead than calculating MD5 digests. Unix and Linux have a shell tool called cmp, which is like diff without the output. So:

    #!/bin/bash
    
    shopt -s nullglob
    
    for file in *[^\)].ext; do
      for duplicate in $(basename "$file" .ext)\ \(*\).ext; do
        if cmp "$file" "$duplicate"; then
          rm -v "$file"
        fi
      done
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.