If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Flatenning a directory structure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-21-04, 08:33
badmoon badmoon is offline
Registered User
 
Join Date: Dec 2004
Posts: 5
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 08:51.
Reply With Quote
  #2 (permalink)  
Old 12-21-04, 09:53
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
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
Reply With Quote
  #3 (permalink)  
Old 12-21-04, 11:37
badmoon badmoon is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 12-21-04, 13:45
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
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
Reply With Quote
  #5 (permalink)  
Old 12-22-04, 05:38
badmoon badmoon is offline
Registered User
 
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?
Reply With Quote
  #6 (permalink)  
Old 12-22-04, 08:59
badmoon badmoon is offline
Registered User
 
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 09:02.
Reply With Quote
  #7 (permalink)  
Old 12-22-04, 11:11
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Use the \cygwin\cygwin.bat script to start bash interpreter and avoid Windows command confusion!
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Last edited by LKBrwn_DBA; 12-22-04 at 11:18.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On