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)
Thank you, @Canina 🙂 Yes, I am running Debian trixie "testing" but before posting this question I ...
Pr. Sunflower‭ wrote 7 months ago

Thank you, @Canina 🙂

Yes, I am running Debian trixie "testing" but before posting this question I tried rename and rename.ul but I ran through some trouble with them.

I have perl already installed, I called rename.ul but it didn't do crap and didn't display any error message in CLI.

Anyway, @r~~ 's answer is a one-liner and uses mv instead of rename so I went with them and it worked 😊

Thank you for your help! 🙏

Canina‭ wrote 7 months ago

Pr. Sunflower‭ That's odd; I tried my example in a Trixie VM and got the same results that I showed in my answer. Well, whatever works for you. Hopefully this will be useful for someone else.