Comments on Rename multiple files which have a variable suffix
Parent
Rename multiple files which have a variable suffix
I compressed some JPEGs with curtail
it messed up the filenames. It was supposed to only add -min
at the end but ended up adding a random string after the extension 😠:
prs@PC:/DOWNLOADS/Pictures$ find . -type f
./IMG_20230917_093726_2-min.jpg-U2XlUA <<< To rename "[...]_2-min.jpg"
./IMG_20230917_093726_2.jpg - Don't do anything
./IMG_20230917_093738_3-min.jpg-H39QsD <<< To rename "[...]_3-min.jpg"
./IMG_20230917_093738_3.jpg - Don't do anything
./IMG_20230917_094057_1-min.jpg-AbxJMt <<< To rename "[...]_1-min.jpg"
./IMG_20230917_094057_1.jpg - Don't do anything
How can I remove the random string for each -min
file?
I already have a find
command that gets the -min
files while excluding the pictures I don't want to touch:
find . -name "*-min.jpg-*"
Post
You can use rename for this. Many distributions package it; for example, it's apt-get install rename in Debian.
In normal usage, it is used to apply an arbitrary regular expression substitution expression to the name of each file named on the command line, allowing you to do complex renamings relatively easily. (It even supports using backreferences with $1
through $9
syntax for when you want to move parts of the file name around.) It also supports a verbose mode (-v
) which I use below, or a dry-run mode (-n
) for when you want to see what it will do.
Example:
$ cd $(mktemp -d)
$ touch IMG_20230917_093726_2-min.jpg-U2XlUA IMG_20230917_093726_2.jpg IMG_20230917_093738_3-min.jpg-H39QsD IMG_20230917_093738_3.jpg IMG_20230917_094057_1-min.jpg-AbxJMt IMG_20230917_094057_1.jpg
$ ls -1
IMG_20230917_093726_2.jpg
IMG_20230917_093726_2-min.jpg-U2XlUA
IMG_20230917_093738_3.jpg
IMG_20230917_093738_3-min.jpg-H39QsD
IMG_20230917_094057_1.jpg
IMG_20230917_094057_1-min.jpg-AbxJMt
$ rename -v 's,-......$,,g' *-min.jpg-*
IMG_20230917_093726_2-min.jpg-U2XlUA renamed as IMG_20230917_093726_2-min.jpg
IMG_20230917_093738_3-min.jpg-H39QsD renamed as IMG_20230917_093738_3-min.jpg
IMG_20230917_094057_1-min.jpg-AbxJMt renamed as IMG_20230917_094057_1-min.jpg
$ ls -1
IMG_20230917_093726_2.jpg
IMG_20230917_093726_2-min.jpg
IMG_20230917_093738_3.jpg
IMG_20230917_093738_3-min.jpg
IMG_20230917_094057_1.jpg
IMG_20230917_094057_1-min.jpg
$
0 comment threads