I am looking to do a command line search for duplicate downloads within my "Downloads" folder.

Let say I download "test.mp3", but then accidentally download it again, the 2nd download will show up as "test(1).mp3". I want to search for any file that has "(1)" at the end of the file name but before the file extension. What would the command to do this be?

Thank you for the help!

  • ls ~/Download/*\([1-9]\).mp3 ? – JJoao Apr 24 '17 at 16:52

I think this is what you're looking for:

for f in ~/Downloads/*; do
    if [[ $f == *\([1-9]\).* ]]; then
        echo "$f"
  • Edit: @steeldriver pointed out that globs can do the above, so the simpler solution is this:

    printf '%s\n' ~/Downloads/*\([1-9]\).*

I prefer plain Bash, but you could do this in one line using find:

find ~/Downloads/ -name '*\([1-9]\).*'
  • Awesome thank you that works! Do you happen to know the find command to do it as well? I was trying at first before asking here to do it in find but couldn't figure it out. – blockysquirtle Apr 21 '17 at 2:03
  • For a single digit, does regex offer any advantage over a simple glob (echo ~/Downloads/*\([1-9]\).* or find ~/Downloads -name '*\([1-9]\).*')? – steeldriver Apr 21 '17 at 4:08
  • @steeldriver Wow, those are way smoother! I'll put those in my answer, thanks! – wjandrea Apr 21 '17 at 4:42

Alternatively we can calculate the checksums and print the duplicated (this is independent of the download duplicated name conventions).

md5sum ~/Download/*.mp3 | awk 'a[$1]{print $2 }{ a[$1]=$2}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.