Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2004
    Posts
    5

    Unanswered: Flatenning a directory structure

    Hi

    I'm a little new to unix shells and was wandering if somebody could help me out with the following problem:

    I've got a directory structure something like so:

    Dir1
    -File1
    -File2
    -File3
    Dir2
    -File4
    -File5
    -Dir3
    --File6
    --File7


    And I would like to write a script that will copy all the files to a single directory and name the files based on their original path. So I'd have:

    NewDir
    -Dir1_File1
    -Dir1_File2
    -Dir1_File3
    -Dir2_File4
    -Dir2_File5
    -Dir2_Dir3_File6
    -Dir2_Dir3_File7

    I know how to do this using VB scripts, but how can I do it using a Unix shell?

    ps. I've had to use hyphens to indent my files and dirs, as the INDENT tag doesn't allowing nesting and any leading whitespace you try to add manually is simply removed by this annoying editor!
    Last edited by badmoon; 12-21-04 at 09:51.

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Wink

    Try this:
    Code:
    #!/bin/ksh
    old_path=/MyPath/MyFiles
    for f in $(find $old_path -type f)
    do
      nf=$(echo $f|tr '/' '_')
      echo $nf
    done
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Dec 2004
    Posts
    5
    It just says "FIND: Invalid switch" if I try that.

    I don't know if it's because I'm actually in a windows o/s but using cygwin to get a bash shell.

    When I even just type:

    find .

    at the bash prompt I get the following error:

    FIND: Parameter format not correct

    Typing bash --version tells me I'm using the following version:

    GNU bash, version 2.05b.0(9)-release (i686-pc-cygwin)
    Copyright (C) 2002 Free Software Foundation, Inc.

    Please help

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    I executed the following code on WinXP with cygwin:
    Code:
    #!/bin/bash
    old_path=/cygdrive/c/Data/DBA
    for f in $(find $old_path -type f)
    do
      nf=$(echo $f|tr '/' '_')
      echo $nf
    done

    Don't know why yours does not work!


    These are the partial results:

    $ ./Scripts/bin/test.ksh
    _cygdrive_c_Data_DBA_.bash_profile.txt
    _cygdrive_c_Data_DBA_.profile.txt
    _cygdrive_c_Data_DBA_Admin_db.mdb
    _cygdrive_c_Data_DBA_afiedt.buf
    _cygdrive_c_Data_DBA_alert68.log
    _cygdrive_c_Data_DBA_CallExtProcPLSQL.htm
    _cygdrive_c_Data_DBA_CallExtProcPLSQL_files_back_b utton.gif
    _cygdrive_c_Data_DBA_CallExtProcPLSQL_files_header _oracle.gif
    _cygdrive_c_Data_DBA_CallExtProcPLSQL_files_home_b utton.gif
    _cygdrive_c_Data_DBA_ccmt.mdb
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Dec 2004
    Posts
    5
    I'm on XP too. Maybe I've done something wrong in setting up cygwin:

    I have this directory containing the following files:

    add.exe
    alex-cygwin1.dll
    ar.exe
    awk.exe
    basename.exe
    bash.exe
    bison.exe
    cat.exe
    chmod.exe
    cmp.exe
    cp.exe
    cw_proj.bsh
    cygiconv-2.dll
    cygintl-1.dll
    cygintl-2.dll
    cygpath.exe
    cygpcre.dll
    cygpopt-0.dll
    cygwin1.dll
    date.exe
    diff.exe
    dircolors.exe
    dirname.exe
    doc
    dos2unix.exe
    echo.exe
    ee-gasp.exe
    fecho.exe
    find.exe
    flex.exe
    grep.exe
    less.exe
    libfl.a
    ls.exe
    make.exe
    makebfp.bat
    makeheader.bat
    makexbbfp.bat
    mkdir.exe
    mount.exe
    mult
    mv.exe
    printf.exe
    Ps2DllCheck.exe
    PS2DllLk.exe
    rm.exe
    rmdir.exe
    sed.exe
    sh.exe
    sleep.exe
    sort.exe
    tail.exe
    test
    test.exe
    todds.exe
    touch.exe
    tr.exe
    umount.exe
    uniq.exe
    unix2dos.exe
    vssver.scc
    whoami.exe
    windiff.exe

    I had no idea I had cygwin until I found this directory.

    Once I discovered this, I changed my PATH environment variable to contain this directory.
    I then went to a command prompt, cd'd to the above dir and typed bash. The prompt then changed to:

    bash-2.05b$

    I then typed:
    find . -print

    only to get the message:
    FIND: Parameter format not correct

    (However, most of the other commands work like ls, cp, mv)

    Is there anything wrong my method? Are you using the same version of bash I'm using? If you type bash --version, does it say:

    GNU bash, version 2.05b.0(9)-release (i686-pc-cygwin)
    Copyright (C) 2002 Free Software Foundation, Inc.

    Perhaps it has a problem with find because it's also a windows command.

    Any ideas?

  6. #6
    Join Date
    Dec 2004
    Posts
    5
    I've found out what it was: As I suspected earlier, it was because "find" is also a windows command so all I had to do was specify the full path of the unix find command, to ensure the unix version is run instead the windows one. So it works now, thanks for your help.

    ps. This still doesn't explain how you got it working in XP
    Last edited by badmoon; 12-22-04 at 10:02.

  7. #7
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Use the \cygwin\cygwin.bat script to start bash interpreter and avoid Windows command confusion!
    Last edited by LKBrwn_DBA; 12-22-04 at 12:18.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •