You are not logged in.

#1 2013-02-13 10:50:40

broken pipe
Member
Registered: 2010-12-10
Posts: 238

strip file name until character

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

#2 2013-02-13 11:03:43

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: strip file name until character

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

#3 2013-02-13 11:12:08

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: strip file name until character

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

#4 2013-02-13 11:17:13

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,786
Website

Re: strip file name until character

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.


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

#5 2013-02-13 11:20:31

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: strip file name until character

WorMzy wrote:
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. smile


"...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

#6 2013-02-13 11:21:03

jakobcreutzfeldt
Member
Registered: 2011-05-12
Posts: 1,041

Re: strip file name until character

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

Last edited by jakobcreutzfeldt (2013-02-13 11:29:35)

Offline

#7 2013-02-13 11:26:43

broken pipe
Member
Registered: 2010-12-10
Posts: 238

Re: strip file name until character

thank you guys!

for file in *; do mv $file ${file#*-}; done

did the trick! so much easier than i thought! :>

Offline

#8 2013-02-13 11:34:44

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: strip file name until character

jakobcreutzfeldt wrote:
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

#9 2013-02-13 11:40:17

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: strip file name until character

broken pipe wrote:

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. smile


"...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

#10 2013-02-14 05:16:42

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: strip file name until character

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. smile

Offline

Board footer

Powered by FluxBB