LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script to move and rename sub directories ! (https://www.linuxquestions.org/questions/linux-newbie-8/script-to-move-and-rename-sub-directories-4175413089/)

papampi 06-24-2012 03:11 AM

script to move and rename sub directories !
 
Hi
I need to make some changes to a series of directories and sub directories and I need help !
I have a series of directories : "folder a", "folder b", "folder c", ....
and in these directories there are sub directories "folder 1", "folder 2" ,....
I need to rename "folder 1" in "folder a" to "folder a -folder 1" and move it to main directory. so there wont be any sub directories .
the directory tree is :
Code:

main directory
  -folder a
    -- folder 1
    -- folder 2
    ....
  -folder b
    -- folder 1
    -- folder 2
....

I want it to be :
Code:

main directoy
 - "folder a-folder 1"
 - "folder a-folder 2"
 - "folder b-folder 1"
 ....

what is the shell script that can do this for me ?

pixellany 06-24-2012 03:41 AM

first, fix the file names so they don't have spaces in them---Windows somehow tolerates this, but in a real OS like Unix or Linux, spaces in file names = trouble.

Start by defining the individual commands---eg, if you are in "folder_a", you could issue:
Code:

mv folder_1 ../folder_a-folder_1
but, we need it to be generic, so use pwd to get the current folder:
Code:

mv folder_1  ../$(pwd)-folder_1
Once you have the right commands, then put it inside a loop

papampi 06-24-2012 03:53 AM

Thanks for your help !
I know how to manually use mv .
the problem is that in the main folder there is over 500 folders where in each folder is some sub-folders with deferent names !
and do that manually takes lots of time .

pixellany 06-24-2012 03:59 AM

As I said, simply put the commands into a loop---for example, in a directory, you could do:
Code:

for filename in *; do
    mv $filename ../$(pwd)-$filename
done

Depending on the directory structure, this might have to go inside another loop---but first get it working for the simple case

pixellany 06-24-2012 04:05 AM

PS;
If you know that directories and sub-directories contain only files (or only directories), then looping thru them is easier. Otherwise, the code will need to test each item to see what it is.

papampi 06-24-2012 05:22 AM

thanks will test when I get home !

papampi 06-25-2012 01:33 AM

this script do the job for me
thanks to Paul Pedant
Code:

AWK='''
BEGIN { FS = "/"; Mv = "mv \047%s\047 \047%s - %s\047\n" ; }
NF == 3 { printf (Mv, $0, $2, $3); }
'''
find . -type d | awk "${AWK}" | bash

The awk filters out just the ones with two levels, and for each of them makes a mv command to do the rename. This is piped to a shell to actually do the mv commands. The "backslash 0 4 7" is an escape for a single quote, which you will need to enclose your path names in the mv commands because you have blanks in the file names.

For testing, just leave off the bash part and check the commands visually to ensure they do what you want.


All times are GMT -5. The time now is 03:05 AM.