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!

Performing operations on multiple directories 1

Status
Not open for further replies.

ppopper

Technical User
Mar 30, 2006
5
US
Hi,
I am running solaris 8, Scripting in the Ksh. New to scripting. I have a script running on files within a directory. Just simple tarring and moving. I specifically cd to that directory. How can I cd to a directory above that and run the script in all the subdirectories, not just that one that I specifically cd to. I need to do this with out specifically naming the subdirectories.

I am not sure how to accomplish this within the ksh programming environment. Any help is much appreciated.

Thanks,

Phil
 
sth like this perhaps:

cd /path/to/parent/dir
for i in ${PWD}/*
do
if [ -d $i ]
then
cd $i
/path/to/your/tarscript
fi
done



HTH,

p5wizard
 
Thanks for responding,
I am not sure exactly what you are doing with the lines
for i in ${PWD}/*
do
if [ -d $i ]
then
cd $i
/path/to/your/tarscript
The for and the if lines are confusing?

for i in all the subdirectories ? What is i ?

if the directory i ? not sure what this id doing.

could you please explain further?

Thanks,

 
i is just a variable name...

for var in list
do
# variable var is set to each value in the list
# first iteration: first value in the list, steps to next
# value on each iteration
<execute your commands here>
done


"list" can be whatever list of strings you choose, I chose a list to be created by the shell's filename expansion mechanism.

try this to see what it expands to:
cd /parent/dir
echo ${PWD}/*


HTH,

p5wizard
 
Here a sample that have been using to clean-up directories of old files. read it very carefully until you understand what each lines of statements are doing. If you understand it then please give us a synopsis of the script. This is one way to test you if you really up to par with scripting.

#!/bin/ksh
###############################################################################
## ##
## directory Cleanup Utility............ ##
## ##
###############################################################################

###############################################################################
## SET ENVIRONMENT VARIABLES AND START IN SINGLE USER MODE ##
###############################################################################
$HOME/.profile
umask 0
TEST=FALSE
###############################################################################
## OBTAIN WHICH ENVIRONMENT(SERVER BOX) WE ARE IN. ##
###############################################################################

PL1=Name of your environment box here PROD (production) or DEV (development)


if [ `echo $PL1 | grep PROD | wc -l` = 1 ]; then
LAWSVR='SUN01'
else
LAWSVR='SUN02'
fi

###############################################################################
## OBTAIN PRODUCT LINES FOR CURRENT ENVIRONMENT. Create temp files ##
###############################################################################
DASH='###############################################################################'
PLINE="${HOME}/PLIST.tmp"
PL="${HOME}/PL.tmp"
TMPLOG="${HOME}/TMPLOG.tmp"
FOUND="${HOME}/OLDFILE.tmp"
TMPFILE2="CleanedFiles.txt"
Z=0

PL=Starting directory here

###############################################################################
# Next statement is to convert upper to lower - we are using lower char for dir
##############################################################################
cat "${PL}" | tr "[:upper:]" "[:lower:]" | while read wrkDIRS
do

###############################################################################
# bin1 & bin2 are excluded
##############################################################################
[ "${wrkDIRS}" = "bin1" ] || [ "${wrkDIRS}" = "bin2" ] || \
find $LAWDIR/${wrkDIRS}/work -type d -print | grep -v ARCHIVED >> "${PLINE}"

done

echo "\n For files ${1:-14} days and older:" >> $TMPLOG
echo " " >> $TMPLOG

###############################################################################
## Check the main or subdirectories for files older than n number of days ##
## then create a file listing. ##
###############################################################################
cat "${PLINE}" | while read wrkPL
do

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

echo "\n Searching Directory: ${wrkPL}" >> $TMPLOG
Q=0
X=0
text=' '
blk=' '


## PROCESS FILES WITHIN DIRECTORY THAT IS OLDER THAN n DAYS default is 120 days.

find . -type f -mtime +${1:-14} -print | cut -c 3-500 | grep -v '/' > "${FOUND}"

## Report the old files if any.

cat "${FOUND}" | while read wrkFILES
do
text=$text$blk$blk${wrkFILES}
X=`expr $X + 1`

if [ "${X}" -eq 4 ]; then

echo " ${text}" >> $TMPLOG
X=0
text=' '
fi

Q=`expr $Q + 1`
Z=`expr $Z + 1`
done

if [ "${X}" -ge 1 ]; then

echo " ${text}" >> $TMPLOG
X=0
text=' '
fi
fi

echo " " >> $TMPLOG

if [ "${Q}" -eq 0 ]; then

echo "Nothing to purged." >> $TMPLOG
else
echo "\nFile(s) to Purged: ${Q}" >> $TMPLOG
echo " " >> $TMPLOG

fi
done

echo "\n\n${DASH}" >> $TMPLOG
echo "\nTotal File(s) to Purged: ${Z}" >> $TMPLOG
echo "\n\n${DASH}" >> $TMPLOG

###############################################################################
## DELIVER CLEANUP REPORT ##
###############################################################################
###############################################################################
## USING EMAIL ATTACHMENT INSTEAD ##
###############################################################################
cd ${HOME}
unix2dos -ascii $TMPLOG $TMPFILE2
( cat <<-MSGTXT
Work file(s) clean-up script has just ended. Please see attached file.

Thanks.

MSGTXT
uuencode $TMPFILE2 $TMPFILE2
) | mailx -s "CleanWorkDIR.ksh has just completed in ${LAWSVR}" MyEmail@XYZ.org
###############################################################################
## Perform cleanup of temp files. ##
###############################################################################
rm -f "${PL}"
rm -f "${PLINE}"
rm -f "${FOUND}"
rm -f "${TMPFILE2}"
rm -f "${TMPLOG}
 
How can I cd to a directory above that
cd ..

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi I am trying to make the name of every directory unique for the variable name TODAY. Please see my script below. I want the name of the current directory it is compressing and the date like ACD2206__Prod_Rev03_Processed_062606

For the life of me I can not get it to work.

Below is my script

Thanks for the help

#!/usr/bin/ksh

# readinto the array the names of all the subdirectories

#loop through each subdirectory and tar up those files


cd /export/home/data/datalog
for i in ${PWD}/*
do
if [ -d $i ]
then
cd $i

# also transfer the files when in the directory



# move to the directory to tar up
# cd /export/home/data/datalog/ACD2206_Prod_Rev02/Processed
# cd /export/home/data/datalog/ACD2206_Prod_Rev03_Processed
# make the name of the tar file todays date
TODAY="$i` date +%m%d%y`.tar"

compress *.std*

# tar up the directory
tar -cvf $TODAY *.Z*


# copy the tared file over to the hp directory
cp $TODAY /export/home/data/backup
#cp $TODAY /net/hpserv01/disk3/data

#cd /export/home/data/datalog/ACD2206_Prod_Rev03_Processed

#rm $TODAY
#rm *.Z*

!

fi
done
 
I can't see anything obviously wrong with that script; how exactly is it failing?

Annihilannic.
 
Perhaps this ?
TODAY="$(basename $i) $(date +%m%d%y).tar"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Without the space I imagine?

[tt]TODAY="$(basename $i)$(date +%m%d%y).tar"[/tt]

Annihilannic.
 
Additionally, I can think of an escape route if a dir is not accessible:

cd /export/home/data/datalog
for i in ${PWD}/*
do
if [ -d $i ]
then
cd $i || continue
...

This would continue on to the next dir if your process cannot cd into a dir in the list. Otherwise the "cd" would fail, leaving the script in its current dir and reTARing that dir.


HTH,

p5wizard
 
RUNIT=`find * -type f -print | xargs "$@"`

$RUNIT command

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Thanks, for the help,
One question, What is basename? I want to pull the name of the directory form the variable $i which is the name of the directory I am taring. Is basename just an added name consisting of anything?

Thanks,

Phil
 
Given the full path to a filename, sach as /var/log/messages for example, basename returns the filename component only, i.e. messages. Basically it chops everything off up to the last "/" character.

Annihilannic.
 
It's probably worth mentioning here that dirname does the opposite; it returns the directory component only.

Annihilannic.
 
That works great, thanks. Would there be away to identify that nothing is in a directory and skip tarring it?

Thanks!!
 
...
TODAY="$(basename $i)$(date +%m%d%y).tar"
[ -z "$(ls *.std* 2>/dev/null ] && continue
compress *.std*
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top