You are not logged in.
Pages: 1
Hi all,
i have a bunch of files (>> 100) which i would like to batch rename.
Every file starts with a random sequence followed by the character "-", filename.jpg
e.g.
45sdfm4rf-holiday01.jpg
dfgfdlfkssdfqwe-garden01.jpg
the result should be
holiday01.jpg
garden01.jpg
any ideas? i tried a loop with sed but i couldn't get it managed to rename the files probably.
thanks in advance!
Offline
This should do the trick:
for f in *.jpg; do \
rename $(echo $f | grep -o "[a-z0-9]*-") "" $f ; \
done
You might have to work with the grep regular expression. As written, it assumes that the random part is only lower case letters and numbers.
Last edited by jakobcreutzfeldt (2013-02-13 11:04:11)
Offline
If you're using bash, this should work:
for file in *; do mv $file ${file#*-}; done
Test it on a few first, though.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
If you're using bash, this should work:
for file in *; do mv $file ${file#*-}; done
Test it on a few first, though.
Make sure each file will have a unique name after the process before you start, or use -n with mv to prevent accidental overwrites.
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline
skanky wrote:If you're using bash, this should work:
for file in *; do mv $file ${file#*-}; done
Test it on a few first, though.
Make sure each file will have a unique name after the process before you start, or use -n with mv to prevent accidental overwrites.
Good point - seconded.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
If you're using bash, this should work:
for file in *; do mv $file ${file#*-}; done
Test it on a few first, though.
Neat trick. How exactly does that ${file#*-} syntax work? Is it specifically for prefixes?
Edit: nevermind, I found it: https://www.gnu.org/software/bash/manua … -Expansion
Last edited by jakobcreutzfeldt (2013-02-13 11:29:35)
Offline
thank you guys!
for file in *; do mv $file ${file#*-}; done
did the trick! so much easier than i thought! :>
Offline
skanky wrote:If you're using bash, this should work:
for file in *; do mv $file ${file#*-}; done
Test it on a few first, though.
Neat trick. How exactly does that ${file#*-} syntax work? Is it specifically for prefixes?
Edit: nevermind, I found it: https://www.gnu.org/software/bash/manua … -Expansion
Sorry, should have linked to that in the first place.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
thank you guys!
for file in *; do mv $file ${file#*-}; done
did the trick! so much easier than i thought! :>
Just a note, # will match and remove the shortest match (eg asfadsf-text-file.txt -> text-file.txt) if you want the longest, use ## (dfsaf-adsf-textfile.txt -> textfile.txt).
I used to forget that stuff all the time. The/A bash guide is well worth a peruse, even if it's just to get an idea that these things are in it.
Incidentally, I'd save that as a commented script file for future use (if you feel energetic, you could do stuff like add in the delimiter as a parameter, etc.). I'd make WorMzy's caveat prominent in it, too.
Oh and, please mark the thread as solved.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Also, the "vidir" application from the "moreutils" package can be extremely helpful in these situations. If you already know Vim then it allows you to be able to easily rename all those files without having to learn Bash scripting.
Offline
Pages: 1