You are not logged in.

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

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

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,033

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,838

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: England
Registered: 2010-06-16
Posts: 5,043

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: ASUS P8Z77-V PRO // Processor: Intel Core i7-3770K 3.4GHz // GFX: nVidia GeForce GTX 970 Ti // RAM: 32GB (4x 8GB) Corsair DDR3 (@ 2133MHz) // Storage: 1x 3TB Seagate SATAII 5x 1TB Samsung SATAII, 2x 120GB Corsair SSD

Online

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

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

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,033

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: 216

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,838

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,838

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: 3,424
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