Link to home
Start Free TrialLog in
Avatar of mboss
mboss

asked on

Need a rucursive renaming script

I'm trying to rename files that were copied from one moiunt point (DISK1) to another (DISK2).  There are many subdirectories that reside under the mount points.  Each directory has files with the a similar naming convention (ie. xxxDISK1xxx).  

The problem is, if I copy all these file to a new mount point (say DISK2).  I have to rename all the files by hand (xxxDISK1XXX -> xxxDISK2xxx).

Ideally, if I can get a script that can take the old name, new name and the full path of the directory affected, that would change the names of all the file, it would be great.

(ie.  rename_all.sh -o DISK1 -n DISK2 -d /home/test/DISK2)

Speediness (but also correctness) will be reward with more points.

Please write back with comments if you are unclear about anything.

Also please post as comments first untill I accept.  So the question doesn't get locked.

Thanks

mboss
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Assuming the subdirectories don't have names like ../abcDISK1efg/... !

To do the actual rename, create a little shell script like this (e.g. called mvdtod)
mv $1 `echo $1 | sed s/$2/$3/`

To use it, do: cd /DISK2; find . -exec /path/to/mvdtod {} DISK1 DISK2 \;



Avatar of mboss
mboss

ASKER

That works great!!  Thanks for the quick response.

Is there a way to do this by specifying the directory I want (not being in the directory)?
Avatar of mboss

ASKER

Also, what does the "\" in the command line?

Just trying to learn.
Avatar of mboss

ASKER

the last comment should read

what does the "\" do in the command line?
ASKER CERTIFIED SOLUTION
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The "\" or "escape" tells the shell not to interpret the following character, so the ";" terminates the "-exec " construct, rather than being interpreted as the end of the command line...

man find:

-exec cmd               True if the executed cmd returns a zero value
                               as exit status.  The end of cmd must be
                               punctuated by a semicolon (semicolon is
                               special to the shell and must be escaped).
                               Any command argument {} is replaced by the
                               current path name.
Avatar of mboss

ASKER

Adjusted points from 250 to 300
Avatar of mboss

ASKER

Everything is working fine.  I'm increasing points to 300, and accepting your answer.

Thanks again

mboss
Thanks! I was thinking about a neater script to do the copy from PATH1 to PATH2 & do the appropriate file renaming at the same time - Is that any use to you, or was this a one-off disk reorganisation?
Avatar of mboss

ASKER

No, the files aren't being copied, they're being restored from tape.  But thanks anyway.  I will probably have another script question either end of day today or tomorrow.  I look forwarded to hearing your ideas about that one.  It will be a little more tricky....I think :)

mboss