How to move files from multiples folder to their parent directoy

Discussion in 'Site & Server Administration' started by nura235, Jun 23, 2009.

  1. #1
    hi

    i m using linux server.

    and want to move 100s of file to their parent directory.

    my current directory look like this.

    301/name 1/*.jpg
    302/name 2/*.jpg
    303/name 3/*.jpg
    .
    .
    .
    .
    860/name 4/*.jpg

    Parent folder name is in serial like 301, 302, 303 and so on till 860.
    But subfolder are different names containing jpg files.

    I want to move those jpg file from name1, name2, to 301, 302 and so on
    and i also dont want to delete those subfolder bcoz i have to copy their name to database.

    i just want to look like this after moving files.

    301/*.jpg, name 1
    302/*.jpg, name 2
    303/*.jpg, name 3
    .
    .
    .
    .
    860/*.jpg, name 4/

    i somehow got this thread.
    http://www.howtoforge.com/forums/sho...d.php?p=187161

    but cant able to figure it out, as i m newbie in it.

    so any help would be appreciated.
     
    nura235, Jun 23, 2009 IP
  2. vasyl

    vasyl Peon

    Messages:
    138
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Create file mover.sh in the folder with 301, 302, 303, .., 860


    #!/bin/sh
    for i in `ls`
    do
    
    if [ -d $i ]
    then
    
    cd $i;
            for j in `ls`
            do
    
                    if [ -d $j ]
                    then
                            cd $j;
                            mv *.jpg ..;
                            cd ..;
                    fi
            done
    cd ..;
    fi
    
    done
    
    Code (markup):
    Do chmod u+x mover.sh
    And run ./mover.sh
     
    vasyl, Jun 24, 2009 IP
  3. nura235

    nura235 Well-Known Member

    Messages:
    529
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    110
    #3
    As i m new to this i m asking so something not goes wrong.

    1. i have to copy the code to a text file and rename it to mover.sh

    2. should i have to place it to one up directory, where the folder 301, 302, 303, 860 are located.

    and finally how to run this.

    is blank folder still available after running this code ?


    After running

    it is not working giving error on syntax 3 rd line do
     
    nura235, Jun 24, 2009 IP
  4. vasyl

    vasyl Peon

    Messages:
    138
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    should do the task you described - recurse into folders inside 310, 302, 303, ... and move jpegs inside that folders up one level.

    What blank folder do you mean? It doesn't remove any folder just moving jpegs - is it right?

    About the error - what OS (cat /etc/issue) are you using? Script tested on Centos 5.3 and should work almost on every shell.
     
    vasyl, Jun 24, 2009 IP
  5. nura235

    nura235 Well-Known Member

    Messages:
    529
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    110
    #5


    using centos-5-i386-hostinabox575

    Error:
    -bash: ./mover.sh: /bin/sh^M: bad interpreter: No such file or directory
     
    nura235, Jun 24, 2009 IP
  6. Mega B

    Mega B Well-Known Member

    Messages:
    3,454
    Likes Received:
    66
    Best Answers:
    1
    Trophy Points:
    190
    #6
    @nura235 i cannot help you with your problem but you can upload all of the files to my FileHosting site in my signature.So you dont loose them and its free.
     
    Mega B, Jun 24, 2009 IP
  7. vasyl

    vasyl Peon

    Messages:
    138
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The problem is because of ^M symbol at the end of the line. This symbol inserted by text editor in some MS Windows box. You should delete ^M on the end of the each line or create this file in UNIX using vi editor for example. Or just download this file using wget or some other tool from http://polkan.net/download/mover.sh
     
    vasyl, Jun 24, 2009 IP
  8. nura235

    nura235 Well-Known Member

    Messages:
    529
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    110
    #8
    it goes well with your provided download file, however i have changed it to know the problem.

    it not working with folders whose name with space eg: freedom fighter and tom jerry

    however it work well with folder name in one word.
    what to edit in this to work well.
     
    nura235, Jun 25, 2009 IP
  9. vasyl

    vasyl Peon

    Messages:
    138
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Its not good idea to have folders with pace in name under unix - acceptable but not usual like wearing saucepan on the head...

    Do you see these folders echoed?

    Try to use cd \"$i\" and cd \"$j\" instead of just cd $i and cd $j
     
    vasyl, Jun 25, 2009 IP
  10. nura235

    nura235 Well-Known Member

    Messages:
    529
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    110
    #10

    yes i can these folder echoed.

    it not working with cd \"$i\" and cd \"$j\"


    line 8 is for j in `ls`
    line 17 is cd ..;

    it giving error in line 8 and line17 cd:"1" no such file and directory
    mv: cannot stat '*.jpg: no such file and directory
     
    nura235, Jun 26, 2009 IP