Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
bash: Truncate filenames, adding incrementing number for duplicates

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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda