Can you help with rename files? (a bit offtopic, sorry)
Hi, I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png In my simple-mindedness I just opened mc, selected all the files and typed in the rename dialog replace *IMG* by IMG* The result though, as you for sure knew (but me only now) were files named like IMG07X55c_ - not what I expected :-( No need to say, but anyway: I have not the slightest idea of regular expressions and such sophisticated things, so that I kindly ask you for your help: how can I rename all files in a directory, cutting off the variable length part before "IMG" and keeping all the rest on the right side of IMG unchanged. Thanks for making me a little bit more enlightened! Daniel -- Daniel Bauer photographer Basel Málaga Twitter: @Marsfotografo (often explicit nudes) https://www.patreon.com/danielbauer https://www.daniel-bauer.com (nudes)
On zaterdag 23 december 2023 15:38:33 CET Daniel Bauer wrote:
Hi,
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png
In my simple-mindedness I just opened mc, selected all the files and typed in the rename dialog replace *IMG* by IMG*
The result though, as you for sure knew (but me only now) were files named like IMG07X55c_ - not what I expected :-(
No need to say, but anyway: I have not the slightest idea of regular expressions and such sophisticated things, so that I kindly ask you for your help:
how can I rename all files in a directory, cutting off the variable length part before "IMG" and keeping all the rest on the right side of IMG unchanged.
Thanks for making me a little bit more enlightened!
Daniel There's krename, that did the job for me in similar renaming actions.
-- Gertjan Lettink a.k.a. Knurpht openSUSE Board openSUSE Forums Team
Am 23.12.23 um 15:43 schrieb Knurpht-openSUSE:
On zaterdag 23 december 2023 15:38:33 CET Daniel Bauer wrote:
Hi,
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png
Ah, photos. Krename allow you to pick from the exif data the time of creation and with [exifExif.Photo.DateTimeOriginal] after some tinkering you can sort it to something like 20220105_151211_your_photo.png Peter
Am Samstag, 23. Dezember 2023, 15:38:33 CET schrieb Daniel Bauer:
Hi,
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png [...]
Besides the already mentioned KRename, you could use mmv that works without regular expressions, too. E.g.: mmv -n "*_IMG_*.png" "IMG_#2.png" This will only print the renamings. Remove the "-n" option or replace it by "-v" to really rename your files. HTH Jan -- The secret of success is sincerity, learn to fake it and you've got it made.
Daniel, et al -- ...and then Jan Ritzerfeld said... % Am Samstag, 23. Dezember 2023, 15:38:33 CET schrieb Daniel Bauer: % > % > I have lots of files named like % > 07X55c_IMG_0155.png % > where 07X55c is a random text of varable lengths which I want to cut so % > that the file will be renamed to IMG_0155.png % > [...] % % Besides the already mentioned KRename, you could use mmv that works without % regular expressions, too. E.g.: % mmv -n "*_IMG_*.png" "IMG_#2.png" % % This will only print the renamings. Remove the "-n" option or replace it by % "-v" to really rename your files. +1 for a handy tool like mmv. Definitely a good way to go if you have it installed. If you don't, there's good ol' shell scripting. Something like for F in *_IMG_[0-9]*.png do NEW=`echo $F | sed -e 's@.*_IMG_@IMG_@` # just strip everything before IMG NEW=`echo $F | sed -e 's@.*_\(IMG_[0-9]*.png\)@\1@'` # grab the pattern you want and keep only it echo mv "$F" "$NEW" # double-check the results and remove "echo " to actually do it done gives you two different ways to calculate the new name. I'm sure there are fancy bash-isms as well, but I'm a very vanilla Bourne guy and like using the standard tools instead :-) awk would be another way to do it, but substring-ing can get busy. % % HTH % Jan % -- % The secret of success is sincerity, learn to fake it and you've got it made. Happy Holidays! :-D -- David T-G See http://justpickone.org/davidtg/email/ See http://justpickone.org/davidtg/tofu.txt
Am 23.12.23 um 15:55 schrieb Jan Ritzerfeld:
Am Samstag, 23. Dezember 2023, 15:38:33 CET schrieb Daniel Bauer:
Hi,
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png [...]
Besides the already mentioned KRename, you could use mmv that works without regular expressions, too. E.g.: mmv -n "*_IMG_*.png" "IMG_#2.png"
This will only print the renamings. Remove the "-n" option or replace it by "-v" to really rename your files.
Thanks for the hints! Both solutions do it for me. In the concrete case I prefer the mmv way for now. For Krename I had to search a bit for how to use a wildcard. It must be used as a regular expression in search/replace and the box "includes RegExp" must be checked. Then it works, too. I wish you all beautiful Christmas, much Love and Peace! Daniel -- Daniel Bauer photographer Basel Málaga Twitter: @Marsfotografo (often explicit nudes) https://www.patreon.com/danielbauer https://www.daniel-bauer.com (nudes)
On 2023-12-23 15:38, Daniel Bauer wrote:
Hi,
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png
In my simple-mindedness I just opened mc, selected all the files and typed in the rename dialog replace *IMG* by IMG*
The result though, as you for sure knew (but me only now) were files named like IMG07X55c_ - not what I expected :-(
No, I can not predict the result without thinking somewhat hard, something that I refuse to do right now (unless bribed), but I did know that it would result in disaster :-p
No need to say, but anyway: I have not the slightest idea of regular expressions and such sophisticated things, so that I kindly ask you for your help:
how can I rename all files in a directory, cutting off the variable length part before "IMG" and keeping all the rest on the right side of IMG unchanged.
Use krename. Yes, I know you have been told twice already, so I will insist: use krename :-) One of the nice things it has is that will run a simulation so that you can see if you like the result without going ahead. Just the thing for us (yes, I confess to not knowing regular expressions besides the simple ones). -- Cheers / Saludos, Carlos E. R. (from 15.4 x86_64 at Telcontar)
On 12/23/23 08:38, Daniel Bauer wrote:
I have lots of files named like 07X55c_IMG_0155.png where 07X55c is a random text of varable lengths which I want to cut so that the file will be renamed to IMG_0155.png
In my simple-mindedness I just opened mc, selected all the files and typed in the rename dialog replace *IMG* by IMG*
If they are all in one directory, just cd to that directory and do: for fnmame in *IMG*.png; do mv "$fname" "${fname#*_}"; done or if in multiple subdirectories feed a while loop with find: while read -r fname; do mv "$fname" "${fname#*_}"; done < <(find the/path -type f -name "*IMG*.png") -- David C. Rankin, J.D.,P.E.
On 12/23/23 18:16, David C. Rankin wrote:
or if in multiple subdirectories feed a while loop with find:
while read -r fname; do mv "$fname" "${fname#*_}"; done < <(find the/path -type f -name "*IMG*.png")
And if for some reason you are using POSIX shell instead of bash, just pipe the find output to the loop instead of using process substitution, e.g. find the/path -type f -name "*IMG*.png" | while read -r fname; do mv "$fname" "${fname#*_}"; done -- David C. Rankin, J.D.,P.E.
participants (7)
-
Carlos E. R.
-
Daniel Bauer
-
David C. Rankin
-
David T-G
-
Jan Ritzerfeld
-
Knurpht-openSUSE
-
Peter McD