Results 1 to 13 of 13
-
01-10-2005, 08:08 PM #1Junior Guru Wannabe
- Join Date
- Feb 2004
- Location
- Delray Beach, FL
- Posts
- 77
Very simple shell script needed for automatisation, please help
Hello!
I need a script which would rename bunch of files. For example:
file1
file2
....
file20
need to be renamed
newname1
newname2
.....
newname20
Part of the filename needs to be changed and keep thee numbers, I know can be done with sed, dont get it.
Numbers are not always from 1, it can be
file45
file46 etc.
Can somebody help me please?
-
01-10-2005, 09:00 PM #2Web Hosting Master
- Join Date
- Dec 2003
- Posts
- 941
Hi igorkl
Feel free to contact me at my email or AIM
ThanksPrimary email: advanced dot programmer at gmail dot com ..
-
01-10-2005, 11:32 PM #3Web Hosting Master
- Join Date
- Nov 2001
- Location
- Ann Arbor, MI
- Posts
- 2,979
man rename
If you're using RH linux, this program is in util-linux.-Mark Adams
www.bitserve.com - Secure Michigan web hosting for your business.
Only host still offering a full money back uptime guarantee and prorated refunds.
Offering advanced server management and security incident response!
-
01-11-2005, 01:58 AM #4Web Hosting Master
- Join Date
- Dec 2002
- Posts
- 1,304
I was kinda bored tonight, and so I decided to code you up an all-bash way to do this.. no external libraries / apps needed.
HOWEVER.. its very inefficient and slow. .but it works!!!! It uses arrays to construct the filenames, and arrays are kind of slow in bash. This script takes almost 5 seconds to execute. Keep in mind it doesnt check for filenames like file.1, just file1 as you have described. Feel free to hack it up
Code:#!/bin/bash OLDPREFIX='file' NEWPREFIX='newname' #construct prefix array cp=0 for i in `ls $OLDPREFIX*`; do prefix[$cp]=$(echo $i | tr -d [:digit:]) cp=$(( $cp + 1 )) done #construct suffix array cs=0 for i in `ls $OLDPREFIX*`; do suffix[$cs]=$(echo $i | tr -d [:alpha:]) cs=$(( $cs + 1 )) done #rewrite prefix array with new name cn=0 for i in `echo ${prefix[@]}`; do newname[$cn]=$( echo $i | sed "s/$OLDPREFIX/$NEWPREFIX/g" ) cn=$(( $cn + 1 )) done #finally, rename the files cr=0 for i in `seq $(echo ${#newname[@]})`; do mv "${prefix[$cr]}${suffix[$cr]}" "${newname[$cr]}${suffix[$cr]}" cr=$(( $cr + 1 )) done
"The only difference between a poor person and a rich person is what they do in their spare time."
"If youth is wasted on the young, then retirement is wasted on the old"
-
01-14-2005, 02:10 PM #5Junior Guru Wannabe
- Join Date
- Feb 2004
- Location
- Delray Beach, FL
- Posts
- 77
[b]innova[b]
Thank you for the script, I use the elements from it.
now I can read the filenames,
#!/bin/bash
echo "server-two56.rmvb" | tr -d [:alpha:]
#/tmp>bash aa
-56.
This cuts everything but "-" (dash) and "." . How do I cut it too?
-
01-14-2005, 02:49 PM #6Web Hosting Master
- Join Date
- Dec 2002
- Posts
- 1,304
Hi,
My example was not very complete for handling all types of filenames.. I assumed at the time that your names were simply things like:
file1
file2
Now, I am going to assume once more that your file extension is not going to change. Using your example:
server-two56.rmvb
I dont think 'tr' will work so well for this. Reason being, instead of an example like 'file1' where we dont have a delimiter, in this case we do: the period before the extension.
Here are some ideas to get you started:
filename=$(echo 'server-two56.rmvb' | cut -d '.' -f1)
echo $filename # server-two56
fileext=$(echo 'server-two56.rmvb' | cut -d '.' -f2)
echo $fileext # rmvb
This example is actually a lot easier to deal with than the first, because having a file extension makes it easy to get the prefix separated.
Now, if you want to do further processing on the 'server-two56' portion, you can use 'tr' as in the original or 'cut' as in my last example. Possibilities include:
field1 #server
field2 #two56
OR
field1 #server-two
field2 #56
I am not sure which part you want to change. .if you want to provide more info I would be happy to help.
A little disclaimer: As you can tell, this isnt all that easy to do in pure bash-shell. I am thinking though that there may be an easier way to do it without using arrays. I am also sure that a perl god could probably code a one-liner to do all the work that this script does
There are plenty of further optimizations I think that could be utilized as well. If you are interested, check out bash's string functions, which can replace external calls to sed, possibly cut/tr as well. I dont know them as well offhand, and I didnt have my reference available at the time of the post.
Nevertheless, its an interesting exercise. I learned most of what I know from the examples in the 'Advanced bash-scripting guide'."The only difference between a poor person and a rich person is what they do in their spare time."
"If youth is wasted on the young, then retirement is wasted on the old"
-
01-14-2005, 08:21 PM #7Web Hosting Guru
- Join Date
- Mar 2001
- Location
- California
- Posts
- 332
This should be simple enough..
Code:#!/bin/sh for X in `ls file[1-9]*` do Y=`echo $X | sed s/file/newname/` mv $X $Y done
Last edited by RutRow; 01-14-2005 at 08:27 PM.
-
01-15-2005, 01:19 AM #8Junior Guru Wannabe
- Join Date
- Feb 2004
- Location
- Delray Beach, FL
- Posts
- 77
Originally posted by RutRow
This should be simple enough..
Code:#!/bin/sh for X in `ls file[1-9]*` do Y=`echo $X | sed s/file/newname/` mv $X $Y done
Thank you
-
01-15-2005, 02:21 AM #9Web Hosting Master
- Join Date
- Nov 2001
- Location
- Ann Arbor, MI
- Posts
- 2,979
Not sure why you didn't try rename.
rename file newname file*-Mark Adams
www.bitserve.com - Secure Michigan web hosting for your business.
Only host still offering a full money back uptime guarantee and prorated refunds.
Offering advanced server management and security incident response!
-
01-15-2005, 05:56 PM #10Web Hosting Master
- Join Date
- Dec 2002
- Posts
- 1,304
Rename is not a standard utility.
"The only difference between a poor person and a rich person is what they do in their spare time."
"If youth is wasted on the young, then retirement is wasted on the old"
-
01-16-2005, 04:39 PM #11Junior Guru Wannabe
- Join Date
- Feb 2004
- Location
- Delray Beach, FL
- Posts
- 77
Okay, now I am stucked in phase 2 in this script.
How do I do double condition?
Now I have:
if [ $b -le $a -a $a -le $c ] ; then
do something ;
else
or do something different ;
fi
How do I insert checking one or more conditions?
need to check
if [ $b -le $a -a $a -le $c ] [$f -le $a -a a -le $f]
and do something if all of this is true.
Is this only way to do this:
if []
if[]
if []
?
-
01-16-2005, 05:50 PM #12Web Hosting Guru
- Join Date
- Mar 2001
- Location
- California
- Posts
- 332
You can use && between the sets of brackets.... if I am reading your question correctly.
Code:if [ $b -le $a -a $a -le $c ] && [$f -le $a -a a -le $f]
-
01-16-2005, 07:08 PM #13Web Hosting Master
- Join Date
- Dec 2002
- Posts
- 1,304
To handle multiple conditions, either use the 'elif' statement, or perhaps a 'case' statement would work also.
Example elifs:
if [ some test ] && [ other test ]
then
do something
elif [ some other test ] && [ other test ]
then
do something else
elif [ some other test ] && [ whatever ]
then
do something else
else #none of the conditions apply
do something #when none of the prior conditions work
fi"The only difference between a poor person and a rich person is what they do in their spare time."
"If youth is wasted on the young, then retirement is wasted on the old"