1

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 
3
  • Also posted on Stack Overflow. Please don't post the same question on multiple Stack Exchange sites. Commented Mar 8, 2013 at 15:57
  • The ${root_dir/\//_} substitution requires bash/zsh/ksh -- probably won't work with plain sh. Commented Mar 9, 2013 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. Commented Mar 9, 2013 at 8:28

2 Answers 2

1

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...

1

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 
5
  • 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? Commented Mar 8, 2013 at 7:07
  • Please put your code up in the question so you can format it properly Commented Mar 8, 2013 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. Commented Mar 8, 2013 at 15:33
  • @terry If you're satisfied with glenn's script, remove the echo to actually perform the moves. Commented Mar 8, 2013 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. Commented Mar 8, 2013 at 16:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.