Jump to content

Bash help


Guest anticuchos
 Share

Recommended Posts

Guest anticuchos

Hi!

 

I need help writing a rename script that will not disturb double digit numbers in the file name.

 

Lets say I have the following files in a folder:

 

mandriva01.jpg

suse22.jpg

fedora40.jpg

 

And I want to rename all of them to:

 

linux01.jpg

linux22.jpg

linux40.jpg

 

How would I do it? I am having trouble with the double digit numbers.

 

Here's the base script that will only work with single digit.

#!/bin/sh
for I in `ls`; do
if echo $I | grep -q '^[0-9]\.jpg'; then
mv $I linux$I
fi; done

What do I have to modify to make it double digits? I tried using if echo $I | grep -q '^[0-9][0-9]\.jpg' but didn't work.

 

:help: please. Thanks in advance.

Edited by anticuchos
Link to comment
Share on other sites

Try this. It takes any file that ends in xx.jpg (where xx is any two digits) and renames it to linux followed by the filename stripped of the first series of alpabetic chars up to the first digit.

 

It should work for the examples you gave - for example:

 

Original filename: mandriva01.jpg

Renamed file: linux01.jpg

 

There are probably more elegant ways to do this, but this should work.

 

#!/bin/sh
for I in `ls`; do
if echo $I | grep -q '[0-9][0-9]\.jpg$'; then
  mv $I linux`echo $I | sed 's/^[a-zA-Z]*//'` >/dev/null
fi; done

 

Note, however, that if the filename has any numeric digits embedded in the alpha characters before the final xx.jpg, this won't work right, as shown below:

 

Example: mand01driva12.jpg

Renamed file: linux01driva12.jpg

Edited by jboy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...