LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need to move several directories (https://www.linuxquestions.org/questions/linux-newbie-8/need-to-move-several-directories-646444/)

craftereric 06-02-2008 12:15 PM

need to move several directories
 
I have the current directory structure in /tmp:
utilityserver/REPLACE_COMPUTER_NAME/properties/
guiserver/REPLACE_COMPUTER_NAME/properties/
newserver/REPLACE_COMPUTER_NAME/properties/

(and so on - more directories with the same structure)

From /tmp, I would to rename the above directories as follows:
utilityserver/server1/properties/
guiserver/server1/properties/
newserver/server1/properties/

(and so on)

Is there a find -exec option that will do this? If not, what is the easiest way to do this?

beadyallen 06-02-2008 02:43 PM

There might be a better way to do it, but you can run multiple commands from -exec if you run 'sh -c "...Your commands here...'.
So for example, you could have something like this (untested):
Code:

find . [how to identify directories] -exec sh -C 'olddir={};\
    curhost=$( hostname )
    new=$( echo $olddir | sed "s/REPLACE_COMPUTER_NAME/$curhost/" ); \
    mv $olddir $new ' \;

It should give you the idea anyway.

craftereric 06-02-2008 03:29 PM

this worked - small revision
 
This worked beautifully with one small correction. Make the -C should be -c (lowercase). Thanks for the help.

Code:

find . [how to identify directories] -exec sh -c 'olddir={};\
    curhost=$( hostname )
    new=$( echo $olddir | sed "s/REPLACE_COMPUTER_NAME/$curhost/" ); \
    mv $olddir $new ' \;

Quote:

Originally Posted by beadyallen (Post 3172398)
There might be a better way to do it, but you can run multiple commands from -exec if you run 'sh -c "...Your commands here...'.
So for example, you could have something like this (untested):
Code:

find . [how to identify directories] -exec sh -C 'olddir={};\
    curhost=$( hostname )
    new=$( echo $olddir | sed "s/REPLACE_COMPUTER_NAME/$curhost/" ); \
    mv $olddir $new ' \;

It should give you the idea anyway.


beadyallen 06-02-2008 04:57 PM

Quote:

one small correction. Make the -C should be -c (lowercase).
Hence the 'untested' bit :) Glad it worked.


All times are GMT -5. The time now is 04:15 PM.