Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on Rename multiple files which have a variable suffix

Parent

Rename multiple files which have a variable suffix

+1
−0

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-*"
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+3
−0

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
$
History
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comment threads

Be aware that there are 2 rename commands with some divergences in naming conventions. Fedora/Red Hat... (1 comment)
Thank you, @Canina 🙂 Yes, I am running Debian trixie "testing" but before posting this question I ... (2 comments)
Be aware that there are 2 rename commands with some divergences in naming conventions. Fedora/Red Hat...
Arpad Horvath‭ wrote 7 months ago · edited 7 months ago

Be aware that there are 2 rename commands with some divergences in naming conventions. Fedora/Red Hat has a rename command without regexp. But you can install there the one in the answer like this: https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename/68259926#68259926

(It is strange for me with commas, for me slashes are more familiar: s/-......$//g )