0

Trying to find a script, or app, that batch removes filename clutter following proper formatting.

For example this Exercise [2009] previous version.png into this Exercise [2009].png

I need the final character in all files to be "]" or the year/date, while preserving the filename extension.

I've tried a few scripts such as:

rename ’s/_[^_]*//‘ *.png

or

for file in *.png; do mv "$file" "$(echo "$file" | sed ’s/\.[^.]*$//‘)”; done

which either don't work, or simply remove the file extension after the last "."

I'm basically not sure if I'm changing the right characters in the script, or if these are even the best ones to use.

2 Answers 2

0

Like this, using Perl's rename:

$ rename 's/(.*\]).*/$1.png/' *.png 
8
  • Sorry but I don't understand "remove -n" or how it relates to "dry-run". Also I need this to work on multiple files at the same time. Feb 27, 2023 at 17:07
  • Check edited post Feb 27, 2023 at 17:15
  • Thanks, but I'm getting this error: -bash: syntax error near unexpected token `(' Feb 27, 2023 at 18:05
  • You should have a copy/paste error Feb 27, 2023 at 18:14
  • Can you copy the command you run + error on hastebin.com please ? Feb 27, 2023 at 18:15
0

Here's a solution I've found elsewhere that works on any specified character or characters. It finds the last occurrance of the characters you specify in 3 parts of the command, removes it plus everything else up to the file extension, then readds the specified characters.

find . -name '*1984*' -type f -exec sh -c 'mv "$1" "$(echo $1 | sed -E "s/1984(.*)(\.[^\.]+)$/1984\2/")"' _ {} \;

This particular example keys off the year "1984", so that "Exercise 1984 previous version.png" becomes "Exercise 1984.png".

Special characters such as "]" require a "" before it in each of the 3 parts

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 28, 2023 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .