2

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?

2 Answers2

2

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.

tshiono
  • 21,248
  • 2
  • 14
  • 22
1

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for the suggestion. Here's the output (running on my orginal file list posted above): `DumbFil1.txt DumbFile1.txt GrossFi1.txt LongFil1.doc UglyFil1.doc` – user1455209 Feb 03 '22 at 00:39
  • Sorry, I left out the initial value of `num` – Barmar Feb 03 '22 at 00:40
  • That's a little cleaner, but unfortunately still only outputting the first of each type: `DumbFil1.txt GrossFi1.txt LongFil1.doc UglyFil1.doc` – user1455209 Feb 03 '22 at 00:49
  • I don't understand how your original code works, because `$newname` doesn't have the extension, so how does the `until` condition work? – Barmar Feb 03 '22 at 00:50
  • Must admit I'm just using that code "as borrowed" without a proper understanding of it. – user1455209 Feb 03 '22 at 00:58