Home > database >  bash: Truncate filenames, adding incrementing number for duplicates
bash: Truncate filenames, adding incrementing number for duplicates

Time:02-03

I would like to shorten all filenames in a given location, and where the truncation produces duplicates, include an incrementing number in the filename. I am most of the way there, thanks to this solution: bash: Truncate Filenames, keeping them unique

I've made a small modification allowing it to cover multiple file extensions (as the original was just written for jpegs). My script:

num=1
length=8
for file in *
do
    newname=$file
    extension=${file: -4}
    until [[ ! -f $newname ]]
    do
        (( sublen = length - ${#num} ))
        printf -v newname '%.*s%d' "$sublen" "$file" "$num"
        (( num   ))
    done
    mv "$file" "$newname""$extension"
done

Original file list:

DumbFilename.txt
DumbFilename2.txt
DumbFilename3.txt
GrossFilename.txt
GrossFilename2.txt
GrossFilename3.txt
LongFilename.doc
LongFilename2.doc
LongFilename3.doc
UglyFilename.doc
UglyFilename2.doc
UglyFilename3.doc

Output from the above code:

DumbFil1.txt
DumbFil2.txt
DumbFil3.txt
GrossFi4.txt
GrossFi5.txt
GrossFi6.txt
LongFil7.doc
LongFil8.doc
LongFil9.doc
UglyFi10.doc
UglyFi11.doc
UglyFi12.doc

My only issue is that the numbers increment in one long sequence. I only want it to increment for each instance of a duplicate, like this:

DumbFil1.txt
DumbFil2.txt
DumbFil3.txt
GrossFi1.txt
GrossFi2.txt
GrossFi3.txt
LongFil1.doc
LongFil2.doc
LongFil3.doc
UglyFil1.doc
UglyFil2.doc
UglyFil3.doc

How do I go about this?

CodePudding user response:

Would you please try the following:

length=8
for file in *; do
    newname=$file
    extension=${file: -4}
    for (( num=1; ; num   )); do
        (( sublen = length - ${#num} ))
        printf -v newname '%.*s%d%s' "$sublen" "$file" "$num" "$extension"
        [[ ! -f $newname ]] && break
    done
    mv -- "$file" "$newname"
done

BTW your original script checks the existence of the $newname without appending the extension, which will break on some conditions.

CodePudding user response:

Put the truncated filename in a variable. On the next iteration, check if the current truncated filename is the same as the previous one. If it's different, reset num to 1 to start the sequence over.

length=8
num=1
last_truncated=
for file in *
do
    newname=$file
    extension=${file: -4}
    until [[ ! -f $newname ]]
    do
        (( sublen = length - ${#num} ))
        truncated=${newname:0:$sublen}
        # reset $num with new prefix
        if [[ $truncated != $last_truncated ]]
        then num=1
        fi
        newname=$truncated$num
        (( num   ))
    done
    last_truncated=truncated
    mv "$file" "$newname""$extension"
done
  •  Tags:  
  • Related