Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created August 24, 2012 13:30
Show Gist options
  • Select an option

  • Save jcsalterego/3450547 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/3450547 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Removes *.MP3 dupes of existing *.M4A files
#
##########################################################
# When you really want to delete files, set REAL=1
REAL=0
##########################################################
# Destination directory is optional (default: current)
destdir=$1
[ -z "$destdir" ] && destdir="."
# delimit by newline
IFS=$'\n'
if [ "$REAL" == "1" ]; then
echo "*** Entering REAL mode. Will delete files!";
else
echo "*** Entering FAKE mode. Will show which files to delete.";
fi
# Get all the M4As in the destination directory
m4as=$(find "$destdir" -name '*.m4a')
# Loop through all the M4As
for m4a in $m4as; do
# Construct the MP3 equivalent filename
mp3="$(echo $m4a | sed -e 's/\.m4a$/\.mp3/')";
# Check if the MP3 file exists
if [ -f "$mp3" ]; then
# If it exists, remove it
echo "> rm '$mp3'";
# Only remove it if we are in REAL=1 mode
if [ "$REAL" == "1" ]; then
rm -v "$mp3";
fi
fi
done
$ find test -name '*.m*'
test/both m4a and mp3 1.m4a
test/both m4a and mp3 1.mp3
test/both m4a and mp3 2.m4a
test/both m4a and mp3 2.mp3
test/inner/directory/both m4a and mp3 1.m4a
test/inner/directory/both m4a and mp3 1.mp3
test/inner/directory/both m4a and mp3 2.m4a
test/inner/directory/both m4a and mp3 2.mp3
test/inner/directory/just m4a 1.m4a
test/inner/directory/just m4a 2.m4a
test/inner/directory/just mp3 1.mp3
test/inner/directory/just mp3 2.mp3
test/just m4a 1.m4a
test/just m4a 2.m4a
test/just mp3 1.mp3
test/just mp3 2.mp3
$ cd test
test $ bash ../remove-mp3-dupes.sh
*** Entering FAKE mode. Will show which files to delete.
> rm './both m4a and mp3 1.mp3'
> rm './both m4a and mp3 2.mp3'
> rm './inner/directory/both m4a and mp3 1.mp3'
> rm './inner/directory/both m4a and mp3 2.mp3'
test $ emacs ../remove-mp3-dupes.sh
# Edit REAL=1...
test $ bash ../remove-mp3-dupes.sh
*** Entering REAL mode. Will delete files!
> rm './both m4a and mp3 1.mp3'
./both m4a and mp3 1.mp3
> rm './both m4a and mp3 2.mp3'
./both m4a and mp3 2.mp3
> rm './inner/directory/both m4a and mp3 1.mp3'
./inner/directory/both m4a and mp3 1.mp3
> rm './inner/directory/both m4a and mp3 2.mp3'
./inner/directory/both m4a and mp3 2.mp3
test $ find . -name '*.m*'
./both m4a and mp3 1.m4a
./both m4a and mp3 2.m4a
./inner/directory/both m4a and mp3 1.m4a
./inner/directory/both m4a and mp3 2.m4a
./inner/directory/just m4a 1.m4a
./inner/directory/just m4a 2.m4a
./inner/directory/just mp3 1.mp3
./inner/directory/just mp3 2.mp3
./just m4a 1.m4a
./just m4a 2.m4a
./just mp3 1.mp3
./just mp3 2.mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment