Guest anticuchos Posted October 31, 2005 Report Share Posted October 31, 2005 (edited) 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. please. Thanks in advance. Edited October 31, 2005 by anticuchos Quote Link to comment Share on other sites More sharing options...
jboy Posted October 31, 2005 Report Share Posted October 31, 2005 (edited) 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 October 31, 2005 by jboy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.