Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script: working in subdirectories

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
Hi All:

I need a script that will move in and out of directories and perform a command. I have a directory structure that looks like this:

scan_data/
scan_data/TEST-IT/
scan_data/TEST-IT/001
scan_data/TEST-IT/002
...
scan_data/
scan_data/WIP-FR/
scan_data/WIP-FR/001
scan_data/WIP-FR/007
...

There are dozens of XXX-XXX directories with several 00X subdirectories within them. I need to be able to move into each subdirectory and run a command "gentos.dat."

Any help would be appreciated!

CzarJ

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Code:
for dir in `find scan_data -type d`
do
   cd ${dir} && gentos.dat
done

Regards,
Chuck
 
Err...

Just to be pedantic, Chuck's script will only work if the 'find' command is returning the full, unqualified, path - otherwise the second 'cd' will fail as it will be starting from the wrong place
So if Chuck's script is amended to
Code:
for dir in `find /full/path/to/scan_data -type d`
do
   cd ${dir} && gentos.dat
done
you'll be fine.

It looks like you only want to work in the subdirectories which are called 001, 002, 007, etc. If this is the case change the code to
Code:
for dir in `find /full/path/to/scan_data -type d -name  "[0-9][0-9][0-9]"`
do
   cd ${dir} && gentos.dat
done

Ceci n'est pas une signature
Columb Healy
 
As an alternate solution that doesn't require putting the full path:
Code:
for dir in `find scan_data -type d`
do
   [COLOR=red]([/color]cd ${dir} && gentos.dat[COLOR=red])[/color]
done
The parentheses make the cd and command occur in a separate subprocess that doesn't affect the main process or the other subprocesses. Since all cds will happen from the script's main directory relative paths will work fine.

This has the added effect of not leaving your script in the last subdirectory visited.
 
Worked great, thanks everyone!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top