Home > Software design >  Shell: Rename a file
Shell: Rename a file

Time:10-14

In my directory I have thousands of PDF files. I want to write a shell script where goes through all the files and trims the last 16 characters and and saves back to the directory without keeping the old filename.

Now: KUD_1234_Abc_DEF_9055_01.pdf

New: KUD_1234.pdf

How can I solve that.

Thank you all

CodePudding user response:

To the importance of analyzing and describing a problem properly to find a proper solution.

Here I implement exactly what you ask for:

#!/usr/bin/env sh

for oldname
do
  # Capture old file name extension for re-use, by trimming-out the leading
  # characters up-to including the dot
  extension=${oldname##*.}

  # Capture the old name without extension
  extensionless=${oldname%.*}

  # Compose new name by printing the old file name
  # up to its length minus 16 characters
  # and re-adding the extension
  newname=$(
    printf '%.*s.%s\n' $((${#extensionless}-16)) "$extensionless" "$extension"
  )

  # Demonstrate rename as a dummy 
  echo mv -- "$oldname" "$newname"
done

Works for your sample case:

mv -- KUD_1234_Abc_DEF_9055_01.pdf KUD_1234.pdf

Will collide not rename this:

mv -- KUD_1234_ooh_fail_666_02.pdf KUD_1234.pdf

Will not work with names shorter than 16 characters:

mv -- notwork.pdf notwork.pdf

Will probably not do what you expect if name has no dot extension:

mv -- foobar foobar.foobar

CodePudding user response:

This should work for you (please backup data before trying):

find -type f | sed -E 's|^(. )(.{16})(\.pdf)$|\1\2\3\ \1\3|g' | xargs -I f -- bash -c "mv f"

However, it's much easier to do it with python:

import os
os.chdir("/home/tkhalymon/dev/tmp/empty")
for f in os.listdir("."):
    name, ext = os.path.splitext(f)
    os.rename(f, f"{name[:-16]}{ext}")
  • Related