Solved

Need a rucursive renaming script

Posted on 2000-04-11
10
280 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:mboss
  • 6
  • 4
10 Comments
 
LVL 20

Expert Comment

by:tfewster
ID: 2704214
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 \;



0
 

Author Comment

by:mboss
ID: 2704259
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)?
0
 

Author Comment

by:mboss
ID: 2704300
Also, what does the "\" in the command line?

Just trying to learn.
0
 

Author Comment

by:mboss
ID: 2704368
the last comment should read

what does the "\" do in the command line?
0
 
LVL 20

Accepted Solution

by:
tfewster earned 300 total points
ID: 2704371
You could do "find /DISK2 -name "*DISK1*" -exec /path/to/mvdtod {} DISK1 DISK2 \;

This would stop you getting the error messages my original idea gives you when it tries to excute "mv abc abc" , as it will only try to rename files that actually meet the name criteria!

Is this a one-off, or do you want it tidied up (to your original specification) so someone else can just run it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Expert Comment

by:tfewster
ID: 2704413
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.
0
 

Author Comment

by:mboss
ID: 2704796
Adjusted points from 250 to 300
0
 

Author Comment

by:mboss
ID: 2704797
Everything is working fine.  I'm increasing points to 300, and accepting your answer.

Thanks again

mboss
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2704926
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?
0
 

Author Comment

by:mboss
ID: 2704964
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unix scripting question 1 89
Linux :how to provide sudo access to the user 13 88
Convert OverPunch 25 62
unix in java example 9 53
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now