sryscad
sryscad

Reputation: 353

Remove double prefix from filenames

I have files nested in folders. In case the filename starts with a string that is repeated twice, I would like to change the filename to a name such that this string appears only once.

For example:

How can I do this using command-line in Linux?

Upvotes: 1

Views: 378

Answers (1)

sryscad
sryscad

Reputation: 353

I combined the regex of @Barmar and the shell script of @U880D to remove any duplicate prefix in the filenames recursively. There was a problem with using find and perl-rename, so I had to do it like this:

#!/bin/bash
shopt -s globstar;
for DIR in ${PWD}/**/; do
    cd "${DIR}"
    for FILENAME in *.mp3 ; do
        NEWFILENAME=$(echo "${FILENAME}" | perl -pe 's/^(.*)\1/\1/')
        if [ "${FILENAME}" != "${NEWFILENAME}" ] ; then
            mv "${FILENAME}" "${NEWFILENAME}"
        fi
    done
done

Upvotes: 1

Related Questions