AIX shell script loop and move

Posted on 2006-05-20
Last Modified: 2013-12-26
I need a shell script for AIX unix that will let me specifiy a starting directory and then loop through all the sub-directories and files and move them according to the sub-directory they were located in.  For example.

If I start in a directory named "tommy" and I have three sub-directories named "a", "b", and "c" then I want to move all the files located in "a" to a specific folder name "newfoldera", and so forth.  Keep in mind, one of the sub-directories could also have a sub-directory in itself so the script would need to be recursive. Thanks in advance for the help on this!
Question by:Quetysis
    LVL 65

    Accepted Solution

    I guess a number of ways

    imagine we had a variable

    ND = "/mydir/new"

    firstly, there is cp and with cp you can specify -r

    cd $SD
    cp -r * $ND

    I dont have my unix box handy, I dont even know if its available on AIX Unix

    another is find, trying to remember my syntax

    cd $SD
    find . -print | cpio -pmvd $ND

    another is tar

    cd $SD
    tar cvf /tmp/flist *
    cd $ND
    tar xvf /tmp/flist

    for testing purposes, dont delete until u know its fine
    then when u do, just issue

    cd $SD
    rm -rf *

    LVL 51

    Assisted Solution

    find tommy -print | sed -ne '/a\//s#\(.*\)/\(a/\)\(.*\)#mkdir \1/newfolder\2;mv & \1/newfolder\2\3#p'

    if that returns what you want, simply add
    to execute it

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now