Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have text file contain two columns:

current directory name                               the new name of the last entry

d_7154/d_7161/                                       'Main Integration'
d_7154/d_7161/d_9247/                                'Flows & Methodologies'
d_7154/d_7161/d_9247/d_8986/                         'General Guidelines'
d_7154/d_457/d_691/d_705/                            'Projects T to Z'
d_7154/d_457/d_691/d_6056/d_11253/d_11254/           'Templates'
d_7154/d_457/d_691/d_6056/d_11253/d_11254/d_11255/   'General Templates'
d_7154/d_457/d_691/d_6056/d_11253/d_11254/d_11256/   'Team Template'
...
...

Is there a recipe to rename the tree? The main problem that if I change name of directory on middle of the tree the rest of list become not relevant.

share|improve this question
    
Reversing the order of the list will solve problems with the parent directory being renamed. –  Carpetsmoker Mar 6 '13 at 14:58
    
@Carpetsmoker I tried but the numeric sort reorder the list NOT by the longest path but by usual numeric order. –  Roman Kaganovich Mar 6 '13 at 15:12

1 Answer 1

up vote 1 down vote accepted

Postorder directory traversal is what you want, algorithmically.

To do that in TCSH or any other shell is probably not going to be pleasant.

Is this a one time action? If so, print out your tree as you did with the new names, into a file and reorder the list manually so that the deepest directories are first, and rename them in the script:

#!/bin/sh
mv d_7154/d_457/d_691/d_6056/d_11253/d_11254/d_11256   "d_7154/d_457/d_691/d_6056/d_11253/d_11254/Team Template"  
mv d_7154/d_457/d_691/d_6056/d_11253/d_11254/d_11255   "d_7154/d_457/d_691/d_6056/d_11253/d_11254/General Templates"  
mv d_7154/d_457/d_691/d_6056/d_11253/d_11254           "d_7154/d_457/d_691/d_6056/d_11253/Templates"  
mv d_7154/d_457/d_691/d_705                            "d_7154/d_457/d_691/Projects T to Z"  
mv d_7154/d_7161/d_9247/d_8986                         "d_7154/d_7161/d_9247/General Guidelines"  
mv d_7154/d_7161/d_9247                                "d_7154/d_7161/Flows & Methodologies"  
mv d_7154/d_7161                                       "d_7154/Main Integration"  
...  
...  
share|improve this answer
    
it is long enough list, so I prefer to automate this job. In addition I didn't understood how you modified " the name of the last entry" column? –  Roman Kaganovich Mar 6 '13 at 15:21
    
Even if it's a long list, if it's only going to be done once, making a single script like this will be fastest. Unless there's a lot of repetition (like the same 10 subdirectories repeated many times, but then you can make a simple script for those and repeat it). If there's not a lot of repetition, I can't imagine any form of automation would take less time to write than a brute-force script like this one. –  SuperMagic Mar 6 '13 at 15:31
    
The new name column is simply the parent path for the directory to be renamed prepended to the new name, Make sure to enclose in quotes or escape all special characters or it'll cause all kinds of problems. Ideally, avoid special characters (spaces, ampersands, etc.) in your new files names. Makes life a bit easier. –  SuperMagic Mar 6 '13 at 15:33
    
I think I found simple but not too elegant solution based on your idea. I prepared second column as in your answer and used cp instead mv. After the cp was complete I run "find . -name "d_*" -exec rm -rf {} \;" So at the end I have only renamed directories. –  Roman Kaganovich Mar 6 '13 at 16:03
    
If it works, it works. In these sorts of run-once situations, elegance is nice but not a requirement. –  SuperMagic Mar 6 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.