Jesse Evers
Jesse Evers

Reputation: 63

Delete .mp3 file if .m4a file with same name exists in directory

I ended up with many of my songs in both .m4a and .mp3 format. The duplicate .mp3s are in the same folders as their corresponding .m4as, and I'd like to delete the .mp3s. I'm trying to write a bash script to do that for me, but I'm very new to bash scripting and unsure of what I'm doing wrong. Here's my code:

#!/bin/bash
for f in ~/music/artist/album/* ; do
    if [ -f f.m4a ] && [ -f f.mp3 ] ; then
        rm f.mp3
        echo "dup deleted"
    fi
done

I'd really appreciate it if someone could figure out what's going wrong here. Thanks!

Upvotes: 1

Views: 511

Answers (2)

Dmitry Alexandrov
Dmitry Alexandrov

Reputation: 1773

#!/bin/bash
# No need to loop through unrelated files (*.txt, directories, etc), right?
for f in ~/music/artist/album/*.m4a; do
    f="${f%.*}"
    if [[ -f ${f}.mp3 ]]; then
        rm -f "${f}.mp3" && echo >&2 "${f}.mp3 deleted"
    fi
done

Upvotes: 4

Gilles Quénot
Gilles Quénot

Reputation: 185053

#!/bin/bash
for f in ~/music/artist/album/* ; do
    f="${f%.*}" # remove extension in simple cases (not tar.gz)
    if [[ -f ${f}.m4a && -f ${f}.mp3 ]] ; then
        rm -f "${f}.mp3" && echo "dup deleted"
    fi
done

Upvotes: 0

Related Questions