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!

Parsing all subdirectories and running commands

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
Hey everyone,

I need some help. I am trying to write a small UNIX script that will go through all the subdirectories within a main directory and perform a command line function in each (call the function “resize-image”). In addition it is important that if the script encounters an error it skips that subdirectory and moves on to the next.

Just to give you an idea of what this is, below is a basic directory structure. Within the directory “test” is about 600 subdirectories

/disk/building/test/
/19587
/245
/89578
/87597
/ect ect ect

Thanks in Advance!!!

~CzarJ
 
man find
pay attention to the -type d and -exec primaries.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I've written this as a start, but it doesn't want to work:

------------------------------------------------------
#!/usr/bin/sh

for myfile in `ls`; do
if [ -d $myfile ] ; then
echo "Working on directory : $myfile"
# resize *.img .....
fi
done
------------------------------------------------------

Any hints???
 
it doesn't want to work
Any error message ? No expected behaviour ?
Please help us to help you, read carefully one of the faqs below.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The script doesn't do anything. If I change the script as follows this is what I get:

script:
------------------------------------------------------
#!/usr/bin/sh

echo "Start"
for myfile in `ls`; do
if [ -d $myfile ] ; then
echo "Working on directory : $myfile"
# resize *.img .....
fi
done
echo "finish"
------------------------------------------------------


output:
------------------------------------------------------
Start
Finish
------------------------------------------------------

Besides actually looking to make sure the files were not "resized" the resize command echo's several lines while it is working.

The appropriate subdirectories exist, the appropriate files are in them and the command works, but the script doesn't actually do anything other than echo those two words. I suppose I should try something different but i am not a UNIX person and am not too familiar with the coding.

thanks,

CzarJ
 
It works:
Working on directory : .7jiya
Working on directory : .X11-unix
Working on directory : .yxSya
Working on directory : 16768
Working on directory : 16824
Working on directory : 1sziUa
Working on directory : 22544
Working on directory : 2Wz6Ea
Working on directory : 2YxgUa
Working on directory : 3DoBMa
Working on directory : B93Fia
Working on directory : CxhZaa
Working on directory : DcoEEa
Working on directory : ECC
Working on directory : EMC
Working on directory : EpuSUa
Working on directory : Krn0Ua
Working on directory : LHniaa
Working on directory : OS1o7a
Working on directory : OryVUa
Working on directory : P3vKMa
Working on directory : Pqnaqa
Working on directory : Q1xe7a
Working on directory : R_kCMa
Working on directory : USl17a
Working on directory : VLm4Ma
Working on directory : _bliaa
Working on directory : bos
Working on directory : ekzX7a
Working on directory : fwupdate
Working on directory : inutmpT_vqEa
Working on directory : jOn0Ea
Working on directory : lost+found
Working on directory : mknfs.28124
Working on directory : phoenix.snapOut
Working on directory : s0kXMa
Working on directory : sOxSEa
Working on directory : sas
Working on directory : t2oDqa
Working on directory : u1nAqa
Working on directory : v49HEa
Working on directory : vPwhqa
Working on directory : vSsvaa
Working on directory : vgdata
Working on directory : z9wJia

 
Depending of your current directory, you may consider add this line after echo "Start":
cd /disk/building/test

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Check this out...

PLINE="${HOME}/PLIST.tmp"

find /disk/building/test -type d -print | grep -v ARCHIVED >> "${PLINE}"

cat "${PLINE}" | while read wrkDir
do

cd $wrkDir
if [ "`pwd`" = "${wrkDir}" ]; then

RESIZE HERE

done

done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top