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 to rename directories under the root directory with prefix "ms_PF" and then rename their subdirectories with the modified name of the main directory with an extra "_", and then copy all directories and their subs to root as follows:

root
   maindir1
   maindir2
           subdir1
           subdir2
           subdir3

maindir1 and maindir2 to be renamed with prefix "ms_PF_" and copied direct to root, because maindir2 has subdirectories they will carry the new name of maindir2 which is ms_PF_maindir2 plus and underscor (_) then all subs will be copied to root as follows:

ms_PF_maindir1
ms_PF_maindir2_subdir1
ms_PF_maindir2_subdir2
ms_PF_maindir2_subdir3

Assistance in this matter is highly appreciated. My attempted solution is something like this, but it actually needs fixing:

root_dir="/dwn/icbu_dwn/LL"
prefix="ms_PF_"
do_it () {
awk '{ print "mv $root_dir $prefix ${root_dir/\//_}" }' | sh
}
find "$root_dir" -depth -type d | do_it
share|improve this question
    
    
The ${root_dir/\//_} substitution requires bash/zsh/ksh -- probably won't work with plain sh. –  glenn jackman Mar 9 '13 at 0:37
    
Thanks Glenn for your comment, and my apologies for posting the same question on another Stack Exchange site as my intention was to resolve a pressing issue that was unwillingly accepted. Is there another method or version of ${root_dir/\//_} that would work with plain sh? Your input is really precious to me. Many thanks. –  terry Mar 9 '13 at 8:28
add comment

2 Answers

This will do the renaming in the right order. Note the trailing slash in the first pattern limits the matches to directories.

cd root
for dir in maindir*/*/ maindir*; do
    echo mv $dir ms_PF_${dir/\//_}
done

outputs

mv maindir2/subdir1/ ms_PF_maindir2_subdir1/
mv maindir2/subdir2/ ms_PF_maindir2_subdir2/
mv maindir2/subdir3/ ms_PF_maindir2_subdir3/
mv maindir1 ms_PF_maindir1
mv maindir2 ms_PF_maindir2
share|improve this answer
    
Thank you Glenn for your promptness and sincere help. In fact, I ran your script as it is with modifying root and maindir values, but got "${root/...}: Bad substitution". Any suggestions? –  terry Mar 8 '13 at 7:07
    
Please put your code up in the question so you can format it properly –  glenn jackman Mar 8 '13 at 13:38
    
Sorry Glenn for the mess I made in your solution. However, I put my attempted script up in the question as you asked; hope it makes sense in concept, but it does need to be looked into. I do appreciate your help, thank you. –  terry Mar 8 '13 at 15:33
    
@terry If you're satisfied with glenn's script, remove the echo to actually perform the moves. –  Gilles Mar 8 '13 at 16:00
    
Thanks Gilles, I've actually done so getting the same msg: ${dir/...}: Bad substitution. Due to having to do this task, I've been reading a lot as my main expertise is in mobile tech! So any assistance is highly appreciated. –  terry Mar 8 '13 at 16:22
add comment

If it is only a few directories, do it by hand (anything else will be more work in writing/debugging than in doing).

If it is a largeish set, use find(1) to create a list of the affected files, and use your favorite editor to transform that into a script doing the moving. Check it for mistakes, and fire away.

If even larger/repetitive, I'd do essentially the same as the above, but do the "edit the path into the new name" by sed(1) on-the-fly. Perhaps even called directly from find. Would make a nice one-liner to scare newbies witless...

share|improve this answer
add comment

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.